File size: 2,020 Bytes
0f8916c ff63864 0f8916c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # -*- coding: utf-8 -*-
"""
一鍵部署到 Hugging Face Spaces
用法: python deploy.py # 部署程式碼(不碰 DB)
"""
import configparser
import sys
from pathlib import Path
cfg = configparser.ConfigParser()
cfg.read('Myconfig.ini')
TOKEN = cfg['Hugging Face']['Token'].strip()
USERNAME = cfg['Hugging Face']['Username'].strip()
REPO_ID = f'{USERNAME}/pradsa'
CODE_FILES = [
'app.py', 'db.py', 'PRA.py', 'dsa.py', 'sync.py', 'CutOff.txt',
'requirements.txt', 'Dockerfile', 'start.sh',
'templates/index.html', 'templates/login.html', 'templates/register.html',
'templates/dashboard.html', 'templates/history.html', 'templates/patient.html',
'templates/analysis.html', 'templates/admin.html', 'templates/batch.html',
'templates/dsa_index.html', 'templates/dsa_batch.html', 'templates/dsa_history.html',
'templates/dsa_patient.html', 'templates/dsa_analysis.html',
'templates/combined_analysis.html',
]
def deploy():
from huggingface_hub import HfApi
api = HfApi(token=TOKEN)
for f in CODE_FILES:
if not Path(f).exists():
print(f' SKIP {f} (not found)')
continue
print(f' {f}')
api.upload_file(
path_or_fileobj=f,
path_in_repo=f,
repo_id=REPO_ID,
repo_type='space',
)
print(f'\nDone! https://{USERNAME}-pradsa.hf.space')
if __name__ == '__main__':
print(f'Deploying code to {REPO_ID}')
# Step 1: 下載雲端 DB → 覆蓋 Local
print('\n--- Step 1: Repo → Local ---')
import sync
sync.sync()
# Step 2: 推 code
print('\n--- Step 2: Push code ---')
deploy()
# Step 3: 推 Local DB 回 Repo(防止 rebuild 丟失)
print('\n--- Step 3: Local DB → Repo ---')
from huggingface_hub import HfApi
api = HfApi(token=TOKEN)
api.upload_file(path_or_fileobj='pra_data.db', path_in_repo='pra_data.db',
repo_id=REPO_ID, repo_type='space')
print(' DB restored to Repo')
|