import argparse import json from pathlib import Path from huggingface_hub import HfApi ROOT = Path(__file__).resolve().parent CREDENTIALS_FILE = ROOT / "credentials.json" def read_token(): if not CREDENTIALS_FILE.is_file(): raise FileNotFoundError( f"Missing {CREDENTIALS_FILE}. Add your Hugging Face access token " 'as {"token": "hf_..."} in that file.' ) with CREDENTIALS_FILE.open(encoding="utf-8") as credentials_file: credentials = json.load(credentials_file) token = credentials.get("token") if not isinstance(token, str) or not token.strip(): raise ValueError( f"No token found in {CREDENTIALS_FILE}. Add it as " '{"token": "hf_..."} and try again.' ) return token.strip() def main(): parser = argparse.ArgumentParser( description="Create a Hugging Face Gradio Space and upload this app." ) parser.add_argument( "--name", default="devcoder-ai", help="Space name (default: devcoder-ai)", ) parser.add_argument( "--private", action="store_true", help="Create the Space as private (public by default).", ) args = parser.parse_args() api = HfApi(token=read_token()) account = api.whoami() username = account.get("name") if not username: raise RuntimeError("Could not determine the Hugging Face account name.") repo_id = f"{username}/{args.name}" if api.repo_exists(repo_id=repo_id, repo_type="space"): runtime = api.get_space_runtime(repo_id) requested_hardware = runtime.requested_hardware requested_hardware = getattr( requested_hardware, "value", requested_hardware ) if requested_hardware != "zero-a10g": api.request_space_hardware(repo_id, "zero-a10g") print(f"Updating Space: https://huggingface.co/spaces/{repo_id}") else: api.create_repo( repo_id=repo_id, repo_type="space", space_sdk="gradio", space_hardware="zero-a10g", private=args.private, ) print(f"Created Space: https://huggingface.co/spaces/{repo_id}") result = api.upload_folder( repo_id=repo_id, repo_type="space", folder_path=str(ROOT), ignore_patterns=[ ".gitignore", "credentials.json", "__pycache__/**", "*.py[cod]", ], commit_message="Deploy Qwen2.5 Coder 3B Gradio app", ) print(f"Uploaded app. Deployment status: https://huggingface.co/spaces/{repo_id}") print(f"Upload commit: {result.commit_url}") if __name__ == "__main__": main()