|
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") |
|
|
|
|
|
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 |
|
|
|
|
|
try: |
|
import huggingface_hub |
|
except ImportError: |
|
print("π¦ Installing huggingface_hub...") |
|
subprocess.run([sys.executable, "-m", "pip", "install", "huggingface_hub"], check=True) |
|
import huggingface_hub |
|
|
|
|
|
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 = f"{username}/{space_name}" |
|
print(f"\nπ Space will be created at: https://huggingface.co/spaces/{full_space_name}") |
|
|
|
|
|
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 |
|
|
|
|
|
try: |
|
from huggingface_hub import HfApi, create_repo |
|
|
|
api = HfApi(token=token) |
|
|
|
|
|
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: |
|
|
|
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 |
|
|
|
|
|
if not os.path.exists(".git"): |
|
print("π Initializing git repository...") |
|
subprocess.run(["git", "init"], check=True) |
|
|
|
|
|
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) |
|
|
|
|
|
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 |
|
|
|
subprocess.run(["git", "remote", "add", "space", f"https://huggingface.co/spaces/{full_space_name}"], check=True) |
|
|
|
|
|
print("π Adding files to git...") |
|
subprocess.run(["git", "add", "."], check=True) |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
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() |