# -*- 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')