MedSpace / scripts /deploy_to_hf.py
kbsss's picture
Upload folder using huggingface_hub
df8b435 verified
Raw
History Blame Contribute Delete
5 kB
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}")
# ---------------------------------------------------------
# 1. Set Secrets for Qdrant (Secure)
# ---------------------------------------------------------
print(f"\n[1/3] Setting Secrets for Qdrant...")
# These should be passed as args or found in env, but for now using the hardcoded ones user provided
# (In production, use env vars!)
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}")
# ---------------------------------------------------------
# 2. Prepare Staging Area (Clean Build)
# ---------------------------------------------------------
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)
# Copy Application Files
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)
# Ensure huggingface_hub matches requirements
with open(os.path.join(build_dir, "requirements.txt"), "a") as f:
f.write("\nhuggingface_hub[cli]\n")
# Modify Startup Script
# Read original start.sh
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)"
"""
# Create start.sh in build dir
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)
# Create README with Metadata
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!")
# ---------------------------------------------------------
# 3. Create Space & Upload App Code from Staging
# ---------------------------------------------------------
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:
# Cleanup
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])