textilindo-ai-assistant / deploy_final.py
harismlnaslm's picture
Initial commit: Textilindo AI Assistant for HF Spaces
298d6c1
#!/usr/bin/env python3
"""
Final deployment script for Textilindo AI Assistant to Hugging Face Spaces
"""
import os
import subprocess
import sys
from pathlib import Path
def check_huggingface_cli():
"""Check if huggingface-cli is available"""
try:
result = subprocess.run(["huggingface-cli", "--version"], capture_output=True, text=True)
if result.returncode == 0:
print("βœ… Hugging Face CLI is available")
return True
else:
print("❌ Hugging Face CLI not found")
return False
except FileNotFoundError:
print("❌ Hugging Face CLI not found")
return False
def login_to_huggingface():
"""Login to Hugging Face"""
print("πŸ” Logging in to Hugging Face...")
try:
subprocess.run(["huggingface-cli", "login"], check=True)
print("βœ… Successfully logged in to Hugging Face")
return True
except subprocess.CalledProcessError:
print("❌ Failed to login to Hugging Face")
return False
def create_space():
"""Create a new Hugging Face Space"""
space_name = input("Enter your Hugging Face username: ").strip()
if not space_name:
print("❌ Username is required")
return None
space_repo = f"{space_name}/textilindo-ai-assistant"
print(f"πŸš€ Creating space: {space_repo}")
try:
subprocess.run([
"huggingface-cli", "repo", "create",
"textilindo-ai-assistant",
"--type", "space",
"--sdk", "gradio"
], check=True)
print(f"βœ… Space created successfully: https://huggingface.co/spaces/{space_repo}")
return space_repo
except subprocess.CalledProcessError:
print("❌ Failed to create space")
return None
def prepare_files():
"""Prepare files for deployment"""
print("πŸ“ Preparing files for deployment...")
# Check if all required files exist
required_files = [
"app.py",
"requirements.txt",
"README.md",
"configs/system_prompt.md",
"data/textilindo_training_data.jsonl"
]
missing_files = []
for file in required_files:
if not os.path.exists(file):
missing_files.append(file)
if missing_files:
print(f"❌ Missing required files: {missing_files}")
return False
print("βœ… All required files are present")
return True
def deploy_files(space_repo):
"""Deploy files to the space"""
print(f"πŸ“€ Deploying files to {space_repo}...")
# Clone the space repository
clone_url = f"https://huggingface.co/spaces/{space_repo}"
temp_dir = "temp_space"
try:
# Remove temp directory if it exists
if os.path.exists(temp_dir):
import shutil
shutil.rmtree(temp_dir)
# Clone the repository
subprocess.run(["git", "clone", clone_url, temp_dir], check=True)
print("βœ… Repository cloned successfully")
# Copy files to the cloned repository
files_to_copy = [
"app.py",
"requirements.txt",
"README.md",
"configs/",
"data/"
]
for file in files_to_copy:
if os.path.exists(file):
if os.path.isdir(file):
# Copy directory
subprocess.run(["cp", "-r", file, temp_dir], check=True)
else:
# Copy file
subprocess.run(["cp", file, temp_dir], check=True)
print(f"βœ… Copied {file}")
# Change to the cloned directory
os.chdir(temp_dir)
# Add all files to git
subprocess.run(["git", "add", "."], check=True)
# Commit changes
subprocess.run(["git", "commit", "-m", "Initial deployment of Textilindo AI Assistant"], check=True)
# Push to the space
subprocess.run(["git", "push"], check=True)
print("βœ… Files deployed successfully!")
return True
except subprocess.CalledProcessError as e:
print(f"❌ Deployment failed: {e}")
return False
finally:
# Clean up
if os.path.exists(temp_dir):
import shutil
shutil.rmtree(temp_dir)
os.chdir("..")
def main():
print("πŸš€ Textilindo AI Assistant - Hugging Face Spaces Deployment")
print("=" * 60)
# Check if we're in the right directory
if not os.path.exists("app.py"):
print("❌ app.py not found. Please run this script from the project root directory.")
return
# Prepare files
if not prepare_files():
return
# Check Hugging Face CLI
if not check_huggingface_cli():
print("πŸ“¦ Installing Hugging Face CLI...")
try:
subprocess.run([sys.executable, "-m", "pip", "install", "huggingface_hub"], check=True)
print("βœ… Hugging Face CLI installed")
except subprocess.CalledProcessError:
print("❌ Failed to install Hugging Face CLI")
return
# Login to Hugging Face
if not login_to_huggingface():
return
# Create space
space_repo = create_space()
if not space_repo:
return
# Deploy files
if deploy_files(space_repo):
print("\nπŸŽ‰ Deployment completed successfully!")
print(f"🌐 Your app is available at: https://huggingface.co/spaces/{space_repo}")
print("\nπŸ“‹ Next steps:")
print("1. Wait for the space to build (usually takes 2-5 minutes)")
print("2. Test your application")
print("3. Share the link with others!")
else:
print("\n❌ Deployment failed. Please check the error messages above.")
if __name__ == "__main__":
main()