home / deploy.py
ginigen-ai's picture
Upload 5 files
7131228 verified
"""
πŸš€ μ§€λ‹ˆμ  AI Hugging Face Space μžλ™ 배포 슀크립트
μ‚¬μš©λ²•:
1. pip install huggingface_hub
2. ν™˜κ²½λ³€μˆ˜μ— 토큰 μ„€μ • (λ³΄μ•ˆ!):
export HF_TOKEN=your_token_here
3. python deploy.py
⚠️ λ³΄μ•ˆ κ²½κ³ :
- 토큰을 이 νŒŒμΌμ— 직접 μž‘μ„±ν•˜μ§€ λ§ˆμ„Έμš”!
- ν™˜κ²½λ³€μˆ˜ λ˜λŠ” `huggingface-cli login` μ‚¬μš© ꢌμž₯
- 토큰이 λ…ΈμΆœλ˜μ—ˆλ‹€λ©΄ μ¦‰μ‹œ https://huggingface.co/settings/tokens μ—μ„œ revoke
"""
import os
import sys
from pathlib import Path
try:
from huggingface_hub import HfApi, create_repo
except ImportError:
print("❌ huggingface_hub μ„€μΉ˜ ν•„μš”: pip install huggingface_hub")
sys.exit(1)
# -------------------------------------------------------------
# μ„€μ •
# -------------------------------------------------------------
SPACE_NAME = "geniegen-ai" # μ›ν•˜λŠ” Space μ΄λ¦„μœΌλ‘œ λ³€κ²½
SPACE_SDK = "docker" # FastAPI + HTML을 μœ„ν•΄ docker SDK μ‚¬μš©
# μ—…λ‘œλ“œν•  파일 λͺ©λ‘ (이 μŠ€ν¬λ¦½νŠΈμ™€ 같은 디렉토리에 μžˆμ–΄μ•Ό 함)
FILES_TO_UPLOAD = [
"app.py",
"index.html",
"requirements.txt",
"README.md",
"Dockerfile",
]
def get_token():
"""ν™˜κ²½λ³€μˆ˜ λ˜λŠ” CLI λ‘œκ·ΈμΈμ—μ„œ 토큰 κ°€μ Έμ˜€κΈ°"""
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
if not token:
print("⚠️ HF_TOKEN ν™˜κ²½λ³€μˆ˜κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
print(" λ‹€μŒ 쀑 ν•˜λ‚˜λ₯Ό μˆ˜ν–‰ν•˜μ„Έμš”:")
print(" 1) export HF_TOKEN=hf_xxx (λ¦¬λˆ…μŠ€/λ§₯)")
print(" 2) set HF_TOKEN=hf_xxx (μœˆλ„μš° CMD)")
print(" 3) huggingface-cli login (λŒ€ν™”ν˜• 둜그인)")
sys.exit(1)
return token
def get_username(api: HfApi):
"""둜그인된 μ‚¬μš©μžλͺ… 쑰회"""
info = api.whoami()
return info["name"]
def deploy():
print("=" * 60)
print("πŸš€ μ§€λ‹ˆμ  AI Hugging Face Space 배포 μ‹œμž‘")
print("=" * 60)
# 1. 토큰 & API μ΄ˆκΈ°ν™”
token = get_token()
api = HfApi(token=token)
try:
username = get_username(api)
print(f"βœ… 둜그인 확인: {username}")
except Exception as e:
print(f"❌ 둜그인 μ‹€νŒ¨: {e}")
print(" 토큰이 μœ νš¨ν•œμ§€ ν™•μΈν•˜μ„Έμš”.")
sys.exit(1)
repo_id = f"{username}/{SPACE_NAME}"
print(f"πŸ“¦ λŒ€μƒ Space: {repo_id}")
# 2. Space 생성 (이미 있으면 μŠ€ν‚΅)
try:
create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk=SPACE_SDK,
token=token,
exist_ok=True,
private=False,
)
print(f"βœ… Space μ€€λΉ„ μ™„λ£Œ (SDK: {SPACE_SDK})")
except Exception as e:
print(f"❌ Space 생성 μ‹€νŒ¨: {e}")
sys.exit(1)
# 3. 파일 쑴재 확인
base_dir = Path(__file__).parent
missing = [f for f in FILES_TO_UPLOAD if not (base_dir / f).exists()]
if missing:
print(f"❌ λˆ„λ½λœ 파일: {missing}")
sys.exit(1)
# 4. 파일 μ—…λ‘œλ“œ
print(f"\nπŸ“€ 파일 {len(FILES_TO_UPLOAD)}개 μ—…λ‘œλ“œ 쀑...")
for filename in FILES_TO_UPLOAD:
filepath = base_dir / filename
try:
api.upload_file(
path_or_fileobj=str(filepath),
path_in_repo=filename,
repo_id=repo_id,
repo_type="space",
token=token,
commit_message=f"Add {filename}",
)
size_kb = filepath.stat().st_size / 1024
print(f" βœ“ {filename:<20} ({size_kb:.1f} KB)")
except Exception as e:
print(f" βœ— {filename} μ—…λ‘œλ“œ μ‹€νŒ¨: {e}")
# 5. μ™„λ£Œ μ•ˆλ‚΄
space_url = f"https://huggingface.co/spaces/{repo_id}"
print("\n" + "=" * 60)
print("πŸŽ‰ 배포 μ™„λ£Œ!")
print("=" * 60)
print(f"πŸ”— Space URL: {space_url}")
print(f"πŸ“Š λΉŒλ“œ μƒνƒœ: {space_url} νŽ˜μ΄μ§€μ—μ„œ 둜그 확인 κ°€λŠ₯")
print(f"⏱ Docker λΉŒλ“œλŠ” 2~5λΆ„ μ†Œμš”λ©λ‹ˆλ‹€")
print("=" * 60)
if __name__ == "__main__":
deploy()