Download deploy_hf_space.py from bg5/zai2api2: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/bg5/zai2api2/resolve/main/deploy_hf_space.py
- Command line
-
hf download hf://spaces/bg5/zai2api2/deploy_hf_space.py
-
curl -L -o deploy_hf_space.py https://huggingface.co/spaces/bg5/zai2api2/resolve/main/deploy_hf_space.py
3.1 kB
| #!/usr/bin/env python3 | |
| """Create/update the Hugging Face Docker Space for this repo.""" | |
| from __future__ import annotations | |
| import os | |
| import secrets | |
| from pathlib import Path | |
| from huggingface_hub import HfApi | |
| ROOT = Path(__file__).resolve().parent | |
| DEFAULT_SPACE_ID = "bg5/zai2api2" | |
| def main() -> None: | |
| backup_token = os.getenv("BACKUP_TOKEN", "").strip() | |
| space_id = os.getenv("HF_SPACE_ID", DEFAULT_SPACE_ID).strip() or DEFAULT_SPACE_ID | |
| auth_token = os.getenv("AUTH_TOKEN", "").strip() | |
| generated_auth_token = False | |
| if not auth_token: | |
| auth_token = "zai2api-" + secrets.token_urlsafe(32) | |
| generated_auth_token = True | |
| api = HfApi(token=os.getenv("HF_TOKEN") or None) | |
| api.create_repo( | |
| repo_id=space_id, | |
| repo_type="space", | |
| space_sdk="docker", | |
| exist_ok=True, | |
| ) | |
| api.upload_folder( | |
| repo_id=space_id, | |
| repo_type="space", | |
| folder_path=str(ROOT), | |
| path_in_repo=".", | |
| ignore_patterns=[ | |
| ".git/*", | |
| ".local/*", | |
| "__pycache__/*", | |
| "node_modules/*", | |
| "captcha-provider/node_modules/*", | |
| "data/*", | |
| "*.log", | |
| ".generated-auth-token.txt", | |
| "proxy-3000.js", | |
| ], | |
| delete_patterns=[ | |
| "internal/captcha_test.go", | |
| "proxy-3000.js", | |
| ], | |
| commit_message="部署 zai2api 到 HF Space", | |
| ) | |
| api.add_space_secret( | |
| repo_id=space_id, | |
| key="AUTH_TOKEN", | |
| value=auth_token, | |
| description="API/admin key for zai2api", | |
| ) | |
| if backup_token: | |
| api.add_space_secret( | |
| repo_id=space_id, | |
| key="BACKUP_TOKEN", | |
| value=backup_token, | |
| description="Z.AI JWT token", | |
| ) | |
| api.add_space_variable(repo_id=space_id, key="PORT", value="7860") | |
| api.add_space_variable( | |
| repo_id=space_id, | |
| key="CAPTCHA_PROVIDER_URL", | |
| value="http://127.0.0.1:9876", | |
| ) | |
| api.add_space_variable(repo_id=space_id, key="BROWSER_BACKEND", value="cloak") | |
| api.add_space_variable(repo_id=space_id, key="POOL_SIZE", value="0") | |
| api.add_space_variable(repo_id=space_id, key="CHROMIUM_PATH", value="/usr/bin/chromium") | |
| api.add_space_variable(repo_id=space_id, key="LOG_LEVEL", value="info") | |
| api.add_space_variable(repo_id=space_id, key="DEBUG_LOGGING", value="false") | |
| for stale_key in ("TOKEN_BOUND_CAPTCHA", "FALLBACK_CAPTCHA_PROVIDER_URL"): | |
| try: | |
| api.delete_space_variable(repo_id=space_id, key=stale_key) | |
| except Exception: | |
| pass | |
| print(f"Space: https://huggingface.co/spaces/{space_id}") | |
| print(f"App: https://{space_id.replace('/', '-')}.hf.space") | |
| if generated_auth_token: | |
| token_path = ROOT / ".generated-auth-token.txt" | |
| token_path.write_text(auth_token + "\n", encoding="utf-8") | |
| print(f"AUTH_TOKEN saved to: {token_path}") | |
| else: | |
| print("AUTH_TOKEN configured from environment.") | |
| if __name__ == "__main__": | |
| main() | |