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 | |
| 1. **Hugging Face Account**: Create one at [huggingface.co](https://huggingface.co). | |
| 2. **MongoDB Atlas Cluster**: Create a free cluster at [mongodb.com/atlas](https://www.mongodb.com/atlas). | |
| 3. **Git Installed**: Ensure git is running on your machine. | |
| 4. **Git LFS Installed**: Run `git lfs install` (Required for large model files). | |
| ## Step 1: Create Space | |
| 1. Go to **Hugging Face Spaces** -> **Create new Space**. | |
| 2. **Name**: `envirolytics-backend` (or similar). | |
| 3. **License**: `MIT`. | |
| 4. **SDK**: Select `Docker`. | |
| 5. **Visibility**: Public or Private. | |
| 6. 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)`): | |
| ```bash | |
| # 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 | |
| 1. Go to the **App** tab in your Space. | |
| 2. Wait for the **Building** status to turn to **Running**. | |
| 3. 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. | |