Spaces:
Sleeping
Sleeping
Deployment Guide: Hugging Face Spaces
This guide covers how to deploy the Envirolytics Backend to Hugging Face Spaces with MongoDB integration.
Prerequisites
- Hugging Face Account: Create one at huggingface.co.
- MongoDB Atlas Cluster: Create a free cluster at mongodb.com/atlas.
- Git Installed: Ensure git is running on your machine.
- Git LFS Installed: Run
git lfs install(Required for large model files).
Step 1: Create Space
- Go to Hugging Face Spaces -> Create new Space.
- Name:
envirolytics-backend(or similar). - License:
MIT. - SDK: Select
Docker. - Visibility: Public or Private.
- Click Create Space.
Step 2: Configure Secrets
Go to the Settings tab of your new Space. Scroll to Variables and secrets. Add the following Secrets (Not Variables):
| Name | Value | Description |
|---|---|---|
OPENAQ_API_KEY |
your_openaq_key |
From OpenAQ Dashboard |
MONGO_URI |
mongodb+srv://... |
Connection string from MongoDB Atlas |
Tip: For
MONGO_URI, ensure you allow access from "Anywhere" (0.0.0.0/0) in Atlas Network Access, as Hugging Face IPs change.
Step 3: Push Code
Run these commands in your project folder (d:/Data/envirolytics(1)):
# 1. Initialize Git (if not done)
git init
# 2. Add files
git add .
# 3. Commit
git commit -m "Initial deploy with MongoDB"
# 4. Add Remote (Copy exact URL from your Space's "Clone repository" button)
git remote add space https://huggingface.co/spaces/<YOUR_USERNAME>/<SPACE_NAME>
# 5. Push
git push space master
Step 4: Verify
- Go to the App tab in your Space.
- Wait for the Building status to turn to Running.
- Once running, click "Embed this space" or check the direct URL to see the API docs.
Troubleshooting
- "Application Error": Check Logs tab.
- MongoDB Timeout: Ensure Atlas Network Access IP Whitelist includes
0.0.0.0/0. - Cold Start: The app logs
MongoDB Connection Verified!on startup. If it fails initially, it handles lazy connections gracefully.