#!/usr/bin/env python3 """ Script to push Textilindo AI Assistant to Hugging Face Spaces """ import os import sys import subprocess import shutil from pathlib import Path def check_git_status(): """Check git status and setup""" print("šŸ” Checking Git status...") try: # Check if we're in a git repository result = subprocess.run(["git", "status"], capture_output=True, text=True) if result.returncode != 0: print("āŒ Not in a git repository. Initializing...") subprocess.run(["git", "init"], check=True) print("āœ… Git repository initialized") else: print("āœ… Git repository found") return True except subprocess.CalledProcessError as e: print(f"āŒ Git error: {e}") return False except FileNotFoundError: print("āŒ Git not found. Please install git.") return False def setup_git_lfs(): """Setup Git LFS for large files""" print("šŸ“ Setting up Git LFS...") try: # Install Git LFS subprocess.run(["git", "lfs", "install"], check=True) print("āœ… Git LFS installed") # Create .gitattributes for LFS gitattributes = """ # Large model files *.bin filter=lfs diff=lfs merge=lfs -text *.safetensors filter=lfs diff=lfs merge=lfs -text *.pt filter=lfs diff=lfs merge=lfs -text *.pth filter=lfs diff=lfs merge=lfs -text *.ckpt filter=lfs diff=lfs merge=lfs -text *.pkl filter=lfs diff=lfs merge=lfs -text *.h5 filter=lfs diff=lfs merge=lfs -text *.hdf5 filter=lfs diff=lfs merge=lfs -text *.tar.gz filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text """ with open(".gitattributes", "w") as f: f.write(gitattributes.strip()) print("āœ… .gitattributes created") return True except subprocess.CalledProcessError as e: print(f"āŒ Git LFS setup failed: {e}") return False def create_gitignore(): """Create comprehensive .gitignore""" print("šŸ“ Creating .gitignore...") gitignore_content = """ # Python __pycache__/ *.py[cod] *$py.class *.so .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg # Virtual environments venv/ env/ ENV/ .venv/ # IDE .vscode/ .idea/ *.swp *.swo *.sublime-* # OS .DS_Store Thumbs.db *.tmp *.temp # Model files (use LFS for these) models/ *.bin *.safetensors *.pt *.pth *.ckpt # Cache .cache/ __pycache__/ transformers_cache/ .huggingface/ # Logs *.log logs/ wandb/ # Environment variables .env .env.local .env.production .env.staging # Jupyter .ipynb_checkpoints/ # PyTorch *.pth *.pt # Data files (use LFS for large ones) data/*.jsonl data/*.json data/*.csv data/*.parquet """ with open(".gitignore", "w") as f: f.write(gitignore_content.strip()) print("āœ… .gitignore created") def prepare_dockerfile(): """Ensure we're using the correct Dockerfile""" print("🐳 Preparing Dockerfile...") # Copy the HF Spaces Dockerfile to the root if Path("Dockerfile_hf_spaces").exists(): shutil.copy("Dockerfile_hf_spaces", "Dockerfile") print("āœ… Dockerfile prepared for HF Spaces") return True else: print("āŒ Dockerfile_hf_spaces not found") return False def create_space_config(): """Create Hugging Face Space configuration""" print("āš™ļø Creating Space configuration...") # Create .huggingface directory hf_dir = Path(".huggingface") hf_dir.mkdir(exist_ok=True) # Create space configuration space_config = { "title": "Textilindo AI Assistant", "emoji": "šŸ¤–", "colorFrom": "blue", "colorTo": "purple", "sdk": "docker", "pinned": False, "license": "mit", "app_port": 7860, "hardware": "gpu-basic" } # Write to README.md frontmatter (already done above) print("āœ… Space configuration ready") def commit_and_push(): """Commit and push to repository""" print("šŸ“¤ Committing and pushing...") try: # Add all files subprocess.run(["git", "add", "."], check=True) print("āœ… Files staged") # Commit commit_message = "Initial commit: Textilindo AI Assistant for HF Spaces" subprocess.run(["git", "commit", "-m", commit_message], check=True) print("āœ… Changes committed") # Check if remote exists result = subprocess.run(["git", "remote", "-v"], capture_output=True, text=True) if not result.stdout.strip(): print("āš ļø No remote repository found.") print("Please add your Hugging Face Space repository as remote:") print("git remote add origin https://huggingface.co/spaces/your-username/your-space-name") return False # Push to remote subprocess.run(["git", "push", "origin", "main"], check=True) print("āœ… Pushed to remote repository") return True except subprocess.CalledProcessError as e: print(f"āŒ Git operation failed: {e}") return False def main(): """Main deployment function""" print("šŸš€ Textilindo AI Assistant - Push to Hugging Face Spaces") print("=" * 60) # Check if we're in the right directory if not Path("scripts").exists(): print("āŒ Scripts directory not found. Please run from the project root.") sys.exit(1) # Step 1: Check git status if not check_git_status(): sys.exit(1) # Step 2: Setup Git LFS if not setup_git_lfs(): sys.exit(1) # Step 3: Create .gitignore create_gitignore() # Step 4: Prepare Dockerfile if not prepare_dockerfile(): sys.exit(1) # Step 5: Create space configuration create_space_config() # Step 6: Commit and push if commit_and_push(): print("\nšŸŽ‰ Successfully pushed to Hugging Face Spaces!") print("\nšŸ“‹ Next steps:") print("1. Go to your Hugging Face Space") print("2. Check the build logs") print("3. Set environment variables if needed") print("4. Test the application") else: print("\nāŒ Failed to push. Please check the errors above.") print("\nšŸ’” Manual steps:") print("1. Add remote: git remote add origin ") print("2. Push: git push origin main") if __name__ == "__main__": main()