| import os |
| import sys |
| import shutil |
| from huggingface_hub import HfApi, create_repo, file_exists |
|
|
| def deploy(token, space_name): |
| username = space_name.split("/")[0] |
| dataset_name = f"{username}/medical-qa-knowledge-base" |
| api = HfApi(token=token) |
| |
| print(f"π Deploying to Space: {space_name}") |
| print(f"π¦ Knowledge Base Dataset: {dataset_name}") |
| |
| |
| |
| |
| print(f"\n[1/3] Setting Secrets for Qdrant...") |
| |
| |
| qdrant_url = "https://18ab5ca3-4731-430f-baaf-2d35d36953ae.europe-west3-0.gcp.cloud.qdrant.io:6333" |
| qdrant_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.DQLeocbeR-S9XO-b2cca8UQL7m3OSZMOHIJGB0gJDhQ" |
| |
| try: |
| api.add_space_secret(repo_id=space_name, key="QDRANT_URL", value=qdrant_url) |
| api.add_space_secret(repo_id=space_name, key="QDRANT_API_KEY", value=qdrant_key) |
| api.add_space_secret(repo_id=space_name, key="VECTOR_DB_TYPE", value="qdrant") |
| print(" β
Secrets configured successfully!") |
| except Exception as e: |
| print(f"β οΈ Failed to set secrets (maybe already set?): {e}") |
|
|
| |
| |
| |
| print("\n[2/3] Preparing clean build in 'deploy_build/'...") |
| |
| build_dir = "deploy_build" |
| if os.path.exists(build_dir): |
| shutil.rmtree(build_dir) |
| os.makedirs(build_dir, exist_ok=True) |
| |
| |
| items_to_copy = [ |
| "api", |
| "src", |
| "frontend", |
| "scripts", |
| "requirements.txt", |
| "Dockerfile", |
| "evaluation" |
| ] |
| |
| for item in items_to_copy: |
| src = item |
| dst = os.path.join(build_dir, item) |
| if os.path.isdir(src): |
| shutil.copytree(src, dst, dirs_exist_ok=True) |
| elif os.path.isfile(src): |
| shutil.copy2(src, dst) |
| |
| |
| with open(os.path.join(build_dir, "requirements.txt"), "a") as f: |
| f.write("\nhuggingface_hub[cli]\n") |
| |
| |
| |
| with open("start.sh", "r") as f: |
| original_start = f.read() |
| |
| download_cmd = f""" |
| # Set default PORT if not set (for Streamlit) |
| export PORT=${{PORT:-8501}} |
| |
| # Qdrant mode - No local KB download needed! |
| echo "β
Using Remote Vector Database (Qdrant)" |
| """ |
| |
| if original_start.startswith("#!"): |
| lines = original_start.split("\n") |
| new_start = lines[0] + "\n" + download_cmd + "\n" + "\n".join(lines[1:]) |
| else: |
| new_start = "#!/bin/bash\n" + download_cmd + "\n" + original_start |
| |
| with open(os.path.join(build_dir, "start.sh"), "w") as f: |
| f.write(new_start) |
| os.chmod(os.path.join(build_dir, "start.sh"), 0o755) |
| |
| |
| metadata = """--- |
| title: MediQuery Healthcare AI |
| emoji: π₯ |
| colorFrom: blue |
| colorTo: indigo |
| sdk: docker |
| pinned: false |
| app_port: 8501 |
| --- |
| """ |
| if os.path.exists("README.md"): |
| with open("README.md", "r") as f: |
| content = f.read() |
| if not content.strip().startswith("---"): |
| content = metadata + "\n" + content |
| else: |
| content = metadata + "\n# Healthcare QA Chatbot" |
| |
| with open(os.path.join(build_dir, "README.md"), "w") as f: |
| f.write(content) |
|
|
| print(" β
Staging area ready!") |
|
|
| |
| |
| |
| try: |
| print(f"\n[3/3] Deploying to Space {space_name}...") |
| url = create_repo( |
| repo_id=space_name, |
| token=token, |
| repo_type="space", |
| space_sdk="docker", |
| exist_ok=True, |
| private=False |
| ) |
| print(f" Space ready: {url}") |
| |
| print(" π€ Uploading application code...") |
| |
| api.upload_folder( |
| folder_path=build_dir, |
| repo_id=space_name, |
| repo_type="space" |
| ) |
| |
| print("\nβ
Deployment Complete!") |
| print(f"π Live at: https://huggingface.co/spaces/{space_name}") |
| |
| except Exception as e: |
| print(f"\nβ Deployment failed: {e}") |
| finally: |
| |
| if os.path.exists(build_dir): |
| shutil.rmtree(build_dir) |
|
|
| if __name__ == "__main__": |
| if len(sys.argv) < 3: |
| print("Usage: python deploy_to_hf.py <token> <space_name>") |
| sys.exit(1) |
| |
| deploy(sys.argv[1], sys.argv[2]) |
|
|