Spaces:
Sleeping
Sleeping
Deploying to HuggingFace Spaces
This guide will help you deploy the Enflow backend to HuggingFace Spaces securely by setting up environment variables.
1. Create a HuggingFace Space
- Go to HuggingFace Spaces
- Click on "Create a Space"
- Choose a name for your space
- Select "Docker" as the Space SDK
- Choose public or private visibility as needed
- Create the space
2. Set Up Environment Variables in HuggingFace
To securely manage credentials, add them as secrets in HuggingFace Spaces:
- Navigate to your space's page
- Go to the "Settings" tab
- Scroll down to the "Repository secrets" section
- Add each of the following secrets (matching your .env file):
MONGO_URIJWT_SECRETCLOUDINARY_CLOUD_NAMECLOUDINARY_API_KEYCLOUDINARY_API_SECRETREDIS_URLOPENAI_API_KEYFLASK_ENV(set to "production" for deployment)
3. Update the Dockerfile
Create a Dockerfile in your backend directory with the following content:
FROM python:3.9-slim
WORKDIR /app
# Install system dependencies for tesseract
RUN apt-get update && apt-get install -y \
tesseract-ocr \
poppler-utils \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Set environment variables from HuggingFace secrets
ENV MONGO_URI=${MONGO_URI}
ENV JWT_SECRET=${JWT_SECRET}
ENV CLOUDINARY_CLOUD_NAME=${CLOUDINARY_CLOUD_NAME}
ENV CLOUDINARY_API_KEY=${CLOUDINARY_API_KEY}
ENV CLOUDINARY_API_SECRET=${CLOUDINARY_API_SECRET}
ENV REDIS_URL=${REDIS_URL}
ENV OPENAI_API_KEY=${OPENAI_API_KEY}
ENV FLASK_ENV=${FLASK_ENV}
# Expose port for the Flask application
EXPOSE 7860
# Start the application
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
4. Push to HuggingFace
Clone your HuggingFace space repository:
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAMECopy your backend files to the cloned repository:
cp -r backend/* path/to/cloned/repo/Commit and push the changes:
cd path/to/cloned/repo git add . git commit -m "Deploy Enflow backend" git push
5. Verify Deployment
- Wait for the build to complete on HuggingFace Spaces
- Visit your space URL
- Check that the health check endpoint
/healthreturns a successful response
Important Security Notes
- NEVER commit sensitive credentials to the repository
- Always use environment variables for secrets
- Review your code for hardcoded credentials before pushing
- For local development, continue using the .env file