2B / upload_with_hf_lib.py
37-AN
Update for Hugging Face Space deployment
2a735cc
#!/usr/bin/env python
"""
Upload files to Hugging Face Space using the official huggingface_hub library
"""
import os
import sys
from getpass import getpass
def main():
print("=" * 60)
print(" Upload to Hugging Face Space using huggingface_hub")
print("=" * 60)
try:
# Import huggingface_hub
from huggingface_hub import HfApi, login
# Get required information
print("\nPlease enter your Hugging Face details:")
username = input("Username: ")
token = getpass("Access Token: ")
# Login using the token
login(token=token, add_to_git_credential=True)
# Space name with validation
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
# Construct full space ID
space_id = f"{username}/{space_name}"
# Initialize the API
api = HfApi()
# Validate the user is logged in
try:
user_info = api.whoami()
print(f"\nAuthenticated as: {user_info['name']}")
except Exception as e:
print(f"Error authenticating: {e}")
return False
# Files to upload
files_to_upload = [
"app/core/memory.py",
"app/core/ingestion.py",
"app/ui/streamlit_app.py"
]
# Verify files exist locally
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
# Display summary before upload
print("\nPreparing to upload these files:")
for file_path in files_to_upload:
file_size = os.path.getsize(file_path) / 1024 # KB
print(f" - {file_path} ({file_size:.1f} KB)")
# Confirm upload
confirm = input("\nProceed with upload? (y/n): ").lower()
if confirm != 'y':
print("Upload cancelled by user.")
return False
# Upload each file
print("\nUploading files:")
success_count = 0
for file_path in files_to_upload:
try:
print(f"πŸ“€ Uploading {file_path}... ", end="", flush=True)
# Upload the file
api.upload_file(
path_or_fileobj=file_path,
path_in_repo=file_path,
repo_id=space_id,
repo_type="space",
commit_message=f"Fix: Improve {os.path.basename(file_path)} for better responses and file handling"
)
print("βœ… Success!")
success_count += 1
except Exception as e:
print("❌ Failed!")
print(f" Error: {str(e)}")
# Print summary
print(f"\nUpload summary: {success_count}/{len(files_to_upload)} files uploaded successfully.")
if success_count == len(files_to_upload):
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("\n⚠️ Some files failed to upload. Please check the errors above.")
print("You may need to upload the files manually through the web interface.")
print(f"Go to: https://huggingface.co/spaces/{space_id}/tree/main")
return False
except Exception as e:
print(f"\n❌ Error: {str(e)}")
return False
if __name__ == "__main__":
try:
# Check if huggingface_hub is installed
try:
import huggingface_hub
except ImportError:
print("Installing required package: huggingface_hub")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface_hub"])
import huggingface_hub
# Run the main function
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)