#!/usr/bin/env python """ Deploy fixes for AI responses and file uploading to Hugging Face Spaces """ import os import subprocess import sys import time from getpass import getpass def deploy_fixes(): """Deploy fixes to Hugging Face Space.""" print("=" * 60) print(" Deploy AI Response and File Upload Fixes to Hugging Face Space") print("=" * 60) # Get credentials username = input("Enter your Hugging Face username: ") token = getpass("Enter your Hugging Face token: ") space_name = input("Enter your Space name: ") # Set environment variables os.environ["HUGGINGFACEHUB_API_TOKEN"] = token os.environ["HF_API_KEY"] = token # Create a commit message describing the changes commit_message = """ Fix AI responses and file uploading functionality - Improved AI responses with better prompt formatting and instructions - Enhanced file upload handling with better error recovery - Added support for more file types (docx, html, md, etc.) - Improved UI with progress tracking and better error messages - Fixed edge cases with empty files and error handling """ # Add the remote URL with credentials embedded remote_url = f"https://{username}:{token}@huggingface.co/spaces/{username}/{space_name}" try: print("\n1. Configuring Git repository...") # Configure git remote remotes = subprocess.run(["git", "remote"], capture_output=True, text=True).stdout.strip().split('\n') if "hf" not in remotes: subprocess.run(["git", "remote", "add", "hf", remote_url], check=True) print(" Added 'hf' remote.") else: subprocess.run(["git", "remote", "set-url", "hf", remote_url], check=True) print(" Updated 'hf' remote.") print("\n2. Pulling latest changes...") try: # Try to pull any changes subprocess.run(["git", "pull", "hf", "main"], check=True) print(" Successfully pulled latest changes.") except subprocess.CalledProcessError: print(" Warning: Could not pull latest changes. Will attempt to push anyway.") print("\n3. Staging changes...") # Stage all files subprocess.run(["git", "add", "app/core/memory.py", "app/core/ingestion.py", "app/ui/streamlit_app.py"], check=True) print("\n4. Committing changes...") # Commit changes try: subprocess.run(["git", "commit", "-m", commit_message], check=True) print(" Changes committed successfully.") except subprocess.CalledProcessError: # Check if there are changes to commit status = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True).stdout.strip() if not status: print(" No changes to commit.") else: print(" Error making commit. Will try to push existing commits.") print("\n5. Pushing changes to Hugging Face Space...") # Multiple push strategies push_success = False # Strategy 1: Standard push try: subprocess.run(["git", "push", "hf", "main"], check=True) push_success = True print(" Push successful!") except subprocess.CalledProcessError as e: print(f" Standard push failed: {e}") print(" Trying force push...") # Strategy 2: Force push try: time.sleep(1) # Brief pause subprocess.run(["git", "push", "-f", "hf", "main"], check=True) push_success = True print(" Force push successful!") except subprocess.CalledProcessError as e: print(f" Force push failed: {e}") print(" Trying alternative push approach...") # Strategy 3: Set upstream and force push try: time.sleep(1) # Brief pause subprocess.run(["git", "push", "-f", "--set-upstream", "hf", "main"], check=True) push_success = True print(" Alternative push successful!") except subprocess.CalledProcessError as e: print(f" Alternative push failed: {e}") if push_success: print("\n✅ Success! Your fixes have been deployed to Hugging Face Space.") print(f" View your Space at: https://huggingface.co/spaces/{username}/{space_name}") print(" Note: It may take a few minutes for changes to appear as the Space rebuilds.") return True else: print("\n❌ All push attempts failed. Please check the error messages above.") return False except Exception as e: print(f"\n❌ Unexpected error during deployment: {e}") return False if __name__ == "__main__": try: result = deploy_fixes() if result: print("\nDeployment completed successfully.") else: print("\nDeployment failed. Please try again or deploy manually.") sys.exit(1) except KeyboardInterrupt: print("\nDeployment cancelled by user.") sys.exit(1)