File size: 2,761 Bytes
18c9405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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](https://huggingface.co/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:

```dockerfile
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:
   ```bash
   git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
   ```

2. Copy your backend files to the cloned repository:
   ```bash
   cp -r backend/* path/to/cloned/repo/
   ```

3. Commit and push the changes:
   ```bash
   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