Spaces:
Build error
Build error
| #!/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() | |