|
|
|
""" |
|
Hugging Face Space Uploader using the new commit endpoint |
|
""" |
|
import os |
|
import sys |
|
import json |
|
import base64 |
|
import requests |
|
from getpass import getpass |
|
|
|
def upload_files_commit(space_id, token, files_to_upload): |
|
"""Upload files using the commit endpoint""" |
|
|
|
api_url = f"https://huggingface.co/api/spaces/{space_id}/commit" |
|
|
|
|
|
headers = { |
|
"Authorization": f"Bearer {token}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
|
|
operations = [] |
|
for file_path in files_to_upload: |
|
|
|
with open(file_path, 'rb') as f: |
|
content = f.read() |
|
content_b64 = base64.b64encode(content).decode("ascii") |
|
|
|
|
|
operations.append({ |
|
"operation": "addOrUpdate", |
|
"path": file_path, |
|
"content": content_b64, |
|
"encoding": "base64" |
|
}) |
|
|
|
|
|
payload = { |
|
"operations": operations, |
|
"commit_message": "Fix AI responses and file upload handling" |
|
} |
|
|
|
|
|
response = requests.post(api_url, headers=headers, json=payload) |
|
|
|
|
|
if response.status_code == 200: |
|
return True, "Commit successful" |
|
else: |
|
return False, f"Error: {response.status_code} - {response.text}" |
|
|
|
def main(): |
|
print("=" * 60) |
|
print(" Hugging Face Space File Uploader (Commit Method)") |
|
print("=" * 60) |
|
|
|
|
|
print("\nPlease enter your Hugging Face details:") |
|
username = input("Username: ") |
|
token = getpass("Access Token: ") |
|
|
|
|
|
while True: |
|
space_name = input("Space Name (without username prefix): ") |
|
if "/" in space_name: |
|
print("Error: Please enter just the Space name without username or slashes") |
|
else: |
|
break |
|
|
|
|
|
space_id = f"{username}/{space_name}" |
|
|
|
|
|
print(f"\nValidating Space: {space_id}") |
|
validation_url = f"https://huggingface.co/api/spaces/{space_id}" |
|
headers = {"Authorization": f"Bearer {token}"} |
|
|
|
try: |
|
validation_response = requests.get(validation_url, headers=headers) |
|
if validation_response.status_code != 200: |
|
print(f"Error: Space '{space_id}' not found or not accessible.") |
|
print(f"Please check the Space name and your permissions.") |
|
print(f"Space URL would be: https://huggingface.co/spaces/{space_id}") |
|
return False |
|
else: |
|
space_info = validation_response.json() |
|
print(f"β
Space found: {space_info.get('title', space_id)}") |
|
print(f"URL: https://huggingface.co/spaces/{space_id}") |
|
except Exception as e: |
|
print(f"Error validating space: {e}") |
|
return False |
|
|
|
|
|
files_to_upload = [ |
|
"app/core/memory.py", |
|
"app/core/ingestion.py", |
|
"app/ui/streamlit_app.py" |
|
] |
|
|
|
|
|
missing_files = [f for f in files_to_upload if not os.path.exists(f)] |
|
if missing_files: |
|
print(f"Error: The following files don't exist locally: {missing_files}") |
|
return False |
|
|
|
|
|
print("\nPreparing to upload these files:") |
|
for file_path in files_to_upload: |
|
file_size = os.path.getsize(file_path) / 1024 |
|
print(f" - {file_path} ({file_size:.1f} KB)") |
|
|
|
|
|
confirm = input("\nProceed with upload? (y/n): ").lower() |
|
if confirm != 'y': |
|
print("Upload cancelled by user.") |
|
return False |
|
|
|
|
|
print("\nπ€ Uploading files in a single commit... ", end="", flush=True) |
|
success, message = upload_files_commit(space_id, token, files_to_upload) |
|
|
|
if success: |
|
print("β
Success!") |
|
print("\nβ
All files uploaded successfully!") |
|
print(f"View your Space at: https://huggingface.co/spaces/{space_id}") |
|
print("Note: It may take a few minutes for your Space to rebuild with the new changes.") |
|
return True |
|
else: |
|
print("β Failed!") |
|
print(f"Error: {message}") |
|
return False |
|
|
|
if __name__ == "__main__": |
|
try: |
|
|
|
try: |
|
import requests |
|
except ImportError: |
|
print("Installing required package: requests") |
|
import subprocess |
|
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"]) |
|
import requests |
|
|
|
|
|
success = main() |
|
if success: |
|
sys.exit(0) |
|
else: |
|
sys.exit(1) |
|
except KeyboardInterrupt: |
|
print("\nUpload cancelled by user.") |
|
sys.exit(1) |
|
except Exception as e: |
|
print(f"\nUnexpected error: {e}") |
|
sys.exit(1) |