File size: 3,079 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
#!/usr/bin/env python
"""
Push fixes directly to Hugging Face using the Hub API for more reliable authentication
"""
import os
import sys
import tempfile
import shutil
from getpass import getpass
from huggingface_hub import HfApi, create_repo

def push_fixes():
    print("=" * 60)
    print("  Push 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 (just the name, not including your username): ")
    
    try:
        # Initialize the Hugging Face API
        api = HfApi(token=token)
        
        # Print user info to confirm authentication
        print("\nAuthenticating with Hugging Face...")
        user_info = api.whoami()
        print(f"Authenticated as: {user_info['name']} (@{user_info['fullname']})")
        
        # Space repository ID
        repo_id = f"{username}/{space_name}"
        print(f"\nPreparing to update Space: {repo_id}")
        print(f"Space URL: https://huggingface.co/spaces/{repo_id}")
        
        # Create a list of files to upload
        files_to_upload = [
            "app/core/memory.py",
            "app/core/ingestion.py", 
            "app/ui/streamlit_app.py"
        ]
        
        # Upload each file
        print("\nUploading files:")
        for file_path in files_to_upload:
            try:
                print(f"  - Uploading {file_path}...")
                api.upload_file(
                    path_or_fileobj=file_path,
                    path_in_repo=file_path,
                    repo_id=repo_id,
                    repo_type="space",
                    commit_message=f"Fix: Improve {os.path.basename(file_path)} for better AI responses and file uploads"
                )
                print(f"    Success!")
            except Exception as e:
                print(f"    Error uploading {file_path}: {str(e)}")
                return False
        
        print("\n✅ All files uploaded successfully!")
        print(f"View your Space at: https://huggingface.co/spaces/{username}/{space_name}")
        print("Note: It may take a few minutes for your Space to rebuild with the new changes.")
        return True
        
    except Exception as e:
        print(f"\n❌ Error: {str(e)}")
        return False

if __name__ == "__main__":
    # Check if huggingface_hub is installed
    try:
        import huggingface_hub
    except ImportError:
        print("Error: huggingface_hub package is not installed.")
        print("Installing huggingface_hub...")
        import subprocess
        subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface_hub"])
        print("huggingface_hub installed. Please run the script again.")
        sys.exit(1)
        
    # Run the push function
    if push_fixes():
        print("\nPush completed successfully.")
    else:
        print("\nPush failed. Please check the error messages above.")
        sys.exit(1)