Spaces:
Runtime error
Runtime error
| import os | |
| from huggingface_hub import HfApi, create_repo | |
| from custom_config import HF_TOKEN | |
| def deploy_to_huggingface(): | |
| if not HF_TOKEN: | |
| print("Error: HF_TOKEN is missing!") | |
| return | |
| api = HfApi(token=HF_TOKEN) | |
| try: | |
| # Get username from token | |
| user_info = api.whoami() | |
| username = user_info["name"] | |
| repo_id = f"{username}/Rag-Assistant" | |
| # Create a new Space | |
| print(f"Creating or retrieving Space: {repo_id}...") | |
| create_repo( | |
| repo_id=repo_id, | |
| repo_type="space", | |
| space_sdk="docker", | |
| exist_ok=True | |
| ) | |
| # Upload all files | |
| print(f"Uploading files to {repo_id}...") | |
| api.upload_folder( | |
| folder_path=".", | |
| repo_id=repo_id, | |
| repo_type="space", | |
| ignore_patterns=[ | |
| "venv/*", "backend/venv/*", ".git/*", | |
| "chroma_db/*", "data/*", "uploads/*", | |
| "node_modules/*", "frontend/node_modules/*", | |
| "**/__pycache__/*", "**/*.pyc", ".env", "**/.env", | |
| "frontend/dist/*" # Built in docker | |
| ] | |
| ) | |
| # Upload secrets | |
| print("Pushing secrets to Space...") | |
| from dotenv import load_dotenv | |
| load_dotenv("backend/.env") | |
| openai_key = os.getenv("OPENAI_API_KEY") | |
| if openai_key: | |
| api.add_space_secret(repo_id=repo_id, key="OPENAI_API_KEY", value=openai_key) | |
| print("✅ OPENAI_API_KEY secret added successfully.") | |
| print("✅ Deployment initiated successfully!") | |
| print(f"Check your deployment at: https://huggingface.co/spaces/{repo_id}") | |
| except Exception as e: | |
| print(f"Error during deployment: {e}") | |
| if __name__ == "__main__": | |
| deploy_to_huggingface() | |