gemma / push_to_huggingface.py
w1r4
initial
42e7ec7
raw
history blame
4.56 kB
import os
import subprocess
import sys
def push_to_huggingface():
"""
Script to help push the Gemma-2 Multimodal Chat application to Hugging Face Spaces.
"""
print("\nπŸš€ Pushing Gemma-2 Multimodal Chat to Hugging Face Spaces\n")
# Check if git is installed
try:
subprocess.run(["git", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (subprocess.SubprocessError, FileNotFoundError):
print("❌ Git is not installed or not in PATH. Please install Git and try again.")
return False
# Check if huggingface_hub is installed
try:
import huggingface_hub
except ImportError:
print("πŸ“¦ Installing huggingface_hub...")
subprocess.run([sys.executable, "-m", "pip", "install", "huggingface_hub"], check=True)
import huggingface_hub
# Get Hugging Face username and space name
username = input("Enter your Hugging Face username: ").strip()
space_name = input("Enter a name for your Hugging Face Space (default: gemma-chat): ").strip() or "gemma-chat"
# Full space name
full_space_name = f"{username}/{space_name}"
print(f"\nπŸ” Space will be created at: https://huggingface.co/spaces/{full_space_name}")
# Get Hugging Face token
token = input("Enter your Hugging Face token (create one at https://huggingface.co/settings/tokens): ").strip()
if not token:
print("❌ Token is required to push to Hugging Face Spaces.")
return False
# Create or check if space exists
try:
from huggingface_hub import HfApi, create_repo
api = HfApi(token=token)
# Check if space already exists
try:
api.repo_info(repo_id=full_space_name, repo_type="space")
print(f"ℹ️ Space {full_space_name} already exists. Will push to existing space.")
except Exception:
# Create new space
print(f"πŸ†• Creating new Hugging Face Space: {full_space_name}")
create_repo(
repo_id=space_name,
token=token,
repo_type="space",
space_sdk="gradio",
private=False
)
except Exception as e:
print(f"❌ Error creating/checking space: {str(e)}")
return False
# Initialize git if not already initialized
if not os.path.exists(".git"):
print("πŸ”„ Initializing git repository...")
subprocess.run(["git", "init"], check=True)
# Configure git
print("πŸ”„ Configuring git...")
subprocess.run(["git", "config", "--local", "user.name", username], check=True)
email = input("Enter your email for git configuration: ").strip()
if email:
subprocess.run(["git", "config", "--local", "user.email", email], check=True)
# Add Hugging Face as remote
print("πŸ”„ Adding Hugging Face as remote...")
try:
subprocess.run(["git", "remote", "remove", "space"], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except subprocess.SubprocessError:
pass # Ignore if remote doesn't exist
subprocess.run(["git", "remote", "add", "space", f"https://huggingface.co/spaces/{full_space_name}"], check=True)
# Add files to git
print("πŸ”„ Adding files to git...")
subprocess.run(["git", "add", "."], check=True)
# Commit changes
print("πŸ”„ Committing changes...")
commit_message = input("Enter a commit message (default: Initial commit): ").strip() or "Initial commit"
subprocess.run(["git", "commit", "-m", commit_message], check=True)
# Configure git credentials
print("πŸ”„ Configuring git credentials...")
credential_helper = f"!f() {{ echo username={username}; echo password={token}; }}; f"
subprocess.run(["git", "config", "--local", "credential.helper", credential_helper], check=True)
# Push to Hugging Face
print("\nπŸš€ Pushing to Hugging Face Spaces...")
try:
subprocess.run(["git", "push", "--force", "space", "main"], check=True)
print("\nβœ… Successfully pushed to Hugging Face Spaces!")
print(f"🌐 Your application is now available at: https://huggingface.co/spaces/{full_space_name}")
return True
except subprocess.SubprocessError as e:
print(f"\n❌ Error pushing to Hugging Face Spaces: {str(e)}")
print("Please check your token and try again.")
return False
if __name__ == "__main__":
push_to_huggingface()