Spaces:
Runtime error
Runtime error
Download deploy_hf.py from PhaelSide/cnesst-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/spaces/PhaelSide/cnesst-chatbot/resolve/main/deploy_hf.py
- Command line
-
hf download hf://spaces/PhaelSide/cnesst-chatbot/deploy_hf.py
-
curl -L -o deploy_hf.py https://huggingface.co/spaces/PhaelSide/cnesst-chatbot/resolve/main/deploy_hf.py
3.27 kB
| #!/usr/bin/env python3 | |
| """ | |
| Deploy CNESST chatbot to HuggingFace Spaces using the Python API. | |
| Usage: | |
| HF_TOKEN=hf_xxx HF_USERNAME=yourusername python3 deploy_hf.py | |
| # or just: python3 deploy_hf.py (will prompt for token if not set) | |
| """ | |
| import os | |
| import sys | |
| from pathlib import Path | |
| # Ensure huggingface_hub is available | |
| try: | |
| from huggingface_hub import HfApi, create_repo, upload_folder | |
| except ImportError: | |
| print("Installing huggingface_hub...") | |
| os.system("pip install huggingface_hub") | |
| from huggingface_hub import HfApi, create_repo, upload_folder | |
| HF_TOKEN = os.environ.get("HF_TOKEN", "") | |
| HF_USERNAME = os.environ.get("HF_USERNAME", "") | |
| SPACE_NAME = os.environ.get("SPACE_NAME", "cnesst-chatbot") | |
| GROQ_API_KEY = "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" | |
| if not HF_TOKEN: | |
| HF_TOKEN = input("Enter your HuggingFace token (from https://huggingface.co/settings/tokens): ").strip() | |
| if not HF_USERNAME: | |
| HF_USERNAME = input("Enter your HuggingFace username: ").strip() | |
| if not HF_TOKEN or not HF_USERNAME: | |
| print("ERROR: HF_TOKEN and HF_USERNAME are required") | |
| sys.exit(1) | |
| REPO_ID = f"{HF_USERNAME}/{SPACE_NAME}" | |
| SPACE_DIR = Path(__file__).parent | |
| print(f"\n🚀 Deploying CNESST chatbot to HuggingFace Spaces") | |
| print(f" Space: https://huggingface.co/spaces/{REPO_ID}") | |
| print(f" Source: {SPACE_DIR}") | |
| api = HfApi(token=HF_TOKEN) | |
| # Step 1: Create space | |
| print("\n📦 Creating space (sdk=docker)...") | |
| try: | |
| repo_url = create_repo( | |
| repo_id=REPO_ID, | |
| repo_type="space", | |
| space_sdk="docker", | |
| private=False, | |
| token=HF_TOKEN, | |
| exist_ok=True, | |
| ) | |
| print(f"✅ Space created/found: {repo_url}") | |
| except Exception as e: | |
| print(f"⚠️ Space creation response: {e}") | |
| # Step 2: Set secrets | |
| print("\n🔐 Setting environment secrets...") | |
| try: | |
| api.add_space_secret( | |
| repo_id=REPO_ID, | |
| key="GROQ_API_KEY", | |
| value=GROQ_API_KEY, | |
| ) | |
| print("✅ GROQ_API_KEY secret set") | |
| except Exception as e: | |
| print(f"⚠️ Could not set GROQ_API_KEY secret: {e}") | |
| try: | |
| api.add_space_secret( | |
| repo_id=REPO_ID, | |
| key="LLM_BACKEND", | |
| value="groq", | |
| ) | |
| print("✅ LLM_BACKEND secret set") | |
| except Exception as e: | |
| print(f"⚠️ Could not set LLM_BACKEND secret: {e}") | |
| # Step 3: Upload files | |
| print(f"\n📤 Uploading files from {SPACE_DIR}...") | |
| ignore_patterns = [ | |
| "*.py~", | |
| "__pycache__", | |
| "*.pyc", | |
| ".git", | |
| "deploy*.py", | |
| "deploy*.sh", | |
| "*.log", | |
| ] | |
| upload_result = upload_folder( | |
| repo_id=REPO_ID, | |
| repo_type="space", | |
| folder_path=str(SPACE_DIR), | |
| token=HF_TOKEN, | |
| ignore_patterns=ignore_patterns, | |
| commit_message="Deploy CNESST RAG chatbot", | |
| ) | |
| print(f"\n✅ Deployment complete!") | |
| print(f" Space URL: https://huggingface.co/spaces/{REPO_ID}") | |
| print(f" Commit: {upload_result}") | |
| print(f"\n⏳ The Docker container is now building...") | |
| print(f" This takes 3-5 minutes on first deploy.") | |
| print(f" Check build logs at: https://huggingface.co/spaces/{REPO_ID}/logs") | |
| print(f"\n📱 Your chatbot will be accessible at:") | |
| print(f" https://{HF_USERNAME.lower().replace('_', '-')}-{SPACE_NAME.lower()}.hf.space") | |
| print(f" (or check the space page for the exact URL)") | |