#!/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()