| import json | |
| import os | |
| import sys | |
| from pathlib import Path | |
| from huggingface_hub import HfApi | |
| SPACE_NAME = os.environ.get("SPACE_NAME", "wode") | |
| PRIVATE = os.environ.get("PRIVATE", "false").lower() == "true" | |
| FOLDER = Path(os.environ.get("FOLDER", ".")).resolve() | |
| def main(): | |
| token = sys.stdin.read().strip() | |
| if not token: | |
| raise SystemExit("missing token on stdin") | |
| api = HfApi(token=token) | |
| me = api.whoami() | |
| username = me.get("name") or me.get("fullname") or me.get("email") or "unknown" | |
| repo_url = api.create_repo( | |
| repo_id=SPACE_NAME, | |
| repo_type="space", | |
| private=PRIVATE, | |
| exist_ok=True, | |
| space_sdk="gradio", | |
| ) | |
| full_repo_id = repo_url.repo_id if hasattr(repo_url, "repo_id") else str(repo_url).replace("https://huggingface.co/", "") | |
| api.upload_folder( | |
| repo_id=full_repo_id, | |
| repo_type="space", | |
| folder_path=str(FOLDER), | |
| ignore_patterns=["*.pyc", "__pycache__/*", ".git/*"], | |
| ) | |
| try: | |
| api.restart_space(repo_id=full_repo_id) | |
| except Exception: | |
| pass | |
| out = { | |
| "username": username, | |
| "repo_id": full_repo_id, | |
| "url": f"https://huggingface.co/spaces/{full_repo_id}", | |
| } | |
| print(json.dumps(out, ensure_ascii=False)) | |
| if __name__ == "__main__": | |
| main() | |