File size: 5,341 Bytes
2a735cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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)