| |
| """ |
| Upload Space lên Hugging Face. |
| |
| Cách dùng: |
| 1) pip install -U huggingface_hub |
| 2) Đặt token có quyền write: |
| Windows CMD: set HF_TOKEN=hf_xxx |
| PowerShell: $env:HF_TOKEN="hf_xxx" |
| macOS/Linux: export HF_TOKEN=hf_xxx |
| 3) Đặt repo Space: |
| Windows CMD: set SPACE_ID=your-username/phapdien-law-chatbot |
| macOS/Linux: export SPACE_ID=your-username/phapdien-law-chatbot |
| 4) python deploy_to_hf.py |
| """ |
|
|
| from __future__ import annotations |
|
|
| import os |
| from pathlib import Path |
|
|
| from huggingface_hub import HfApi, create_repo |
|
|
| SPACE_ID = os.environ.get("SPACE_ID", "").strip() |
| HF_TOKEN = os.environ.get("HF_TOKEN", "").strip() |
| ROOT = Path(__file__).resolve().parent |
|
|
| if not SPACE_ID or "/" not in SPACE_ID: |
| raise SystemExit("Thiếu SPACE_ID. Ví dụ: SPACE_ID=tenban/phapdien-law-chatbot") |
| if not HF_TOKEN: |
| raise SystemExit("Thiếu HF_TOKEN có quyền write trên Hugging Face.") |
|
|
| create_repo( |
| repo_id=SPACE_ID, |
| repo_type="space", |
| space_sdk="gradio", |
| private=False, |
| exist_ok=True, |
| token=HF_TOKEN, |
| ) |
|
|
| api = HfApi(token=HF_TOKEN) |
| api.upload_folder( |
| folder_path=str(ROOT), |
| repo_id=SPACE_ID, |
| repo_type="space", |
| ignore_patterns=[ |
| ".git/*", |
| "__pycache__/*", |
| "*.pyc", |
| "*.sqlite", |
| "*.sqlite-*", |
| "*.zip", |
| ".venv/*", |
| ], |
| ) |
|
|
| print(f"Đã upload Space: https://huggingface.co/spaces/{SPACE_ID}") |
|
|