civic-backup / scripts /deploy_to_hf.py
khiemdztv's picture
Sắp xếp lại repo + README hướng dẫn chạy/kiểm thử cho Ban Tổ chức
3ccc46c verified
Raw History Blame Contribute Delete
1.97 kB
#!/usr/bin/env python3
"""Script đẩy giao diện mới lên Hugging Face Space khiemdztv/civic-ai-demo."""
import argparse
import os
import sys
from pathlib import Path
try:
from huggingface_hub import HfApi
except ImportError:
print("Lỗi: Cần cài đặt huggingface_hub: pip install huggingface_hub", file=sys.stderr)
sys.exit(1)
REPO_ID = "khiemdztv/civic-ai-demo"
ROOT_DIR = Path(__file__).resolve().parent.parent
def main():
parser = argparse.ArgumentParser(description="Đẩy giao diện lên HF Space")
parser.add_argument(
"--type",
choices=["static", "gradio"],
default="static",
help="Loại giao diện: 'static' (static_space/) hoặc 'gradio' (hf_space/)"
)
parser.add_argument(
"--token",
default=os.environ.get("HF_TOKEN"),
help="Hugging Face Write Token (hoặc đặt biến môi trường HF_TOKEN)"
)
args = parser.parse_args()
target_dir = ROOT_DIR / ("static_space" if args.type == "static" else "hf_space")
if not target_dir.exists():
print(f"Lỗi: Không tìm thấy thư mục {target_dir}", file=sys.stderr)
sys.exit(1)
print(f"🚀 Chuẩn bị cập nhật Space [{REPO_ID}] với giao diện [{args.type}]...")
print(f"📁 Thư mục nguồn: {target_dir}")
api = HfApi(token=args.token)
try:
api.upload_folder(
folder_path=str(target_dir),
repo_id=REPO_ID,
repo_type="space",
commit_message=f"Update CIVIC AI to {args.type} UI",
)
print("✅ Đã cập nhật thành công lên https://huggingface.co/spaces/" + REPO_ID)
except Exception as e:
print(f"❌ Lỗi khi upload: {e}", file=sys.stderr)
print("Gợi ý: Nếu chưa đăng nhập HF, hãy truyền --token <HF_WRITE_TOKEN> hoặc chạy 'huggingface-cli login'.", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()