textilindo-ai-assistant / push_to_hf_space.py
harismlnaslm's picture
Initial commit: Textilindo AI Assistant for HF Spaces
298d6c1
#!/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 <your-hf-space-url>")
print("2. Push: git push origin main")
if __name__ == "__main__":
main()