enflow-api / hugginface_setup.md
dhruv575
Security key
18c9405

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

  1. Go to HuggingFace Spaces
  2. Click on "Create a Space"
  3. Choose a name for your space
  4. Select "Docker" as the Space SDK
  5. Choose public or private visibility as needed
  6. Create the space

2. Set Up Environment Variables in HuggingFace

To securely manage credentials, add them as secrets in HuggingFace Spaces:

  1. Navigate to your space's page
  2. Go to the "Settings" tab
  3. Scroll down to the "Repository secrets" section
  4. Add each of the following secrets (matching your .env file):
    • MONGO_URI
    • JWT_SECRET
    • CLOUDINARY_CLOUD_NAME
    • CLOUDINARY_API_KEY
    • CLOUDINARY_API_SECRET
    • REDIS_URL
    • OPENAI_API_KEY
    • FLASK_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

  1. Clone your HuggingFace space repository:

    git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
    
  2. Copy your backend files to the cloned repository:

    cp -r backend/* path/to/cloned/repo/
    
  3. Commit and push the changes:

    cd path/to/cloned/repo
    git add .
    git commit -m "Deploy Enflow backend"
    git push
    

5. Verify Deployment

  1. Wait for the build to complete on HuggingFace Spaces
  2. Visit your space URL
  3. Check that the health check endpoint /health returns 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