File size: 7,071 Bytes
192b2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# πŸš€ Hugging Face Spaces Deployment Guide (Docker + Streamlit)

This guide will walk you through deploying your RAG system to Hugging Face Spaces using **Docker with Streamlit**.

## πŸ“‹ Prerequisites

- Hugging Face account
- All files from the `huggingface_deploy/` folder
- Basic understanding of Docker (optional)

## 🎯 Step-by-Step Deployment

### Step 1: Create a New Space

1. **Go to Hugging Face Spaces:**
   - Visit [https://huggingface.co/spaces](https://huggingface.co/spaces)
   - Click "Create new Space"

2. **Configure your Space:**
   - **Owner**: Choose your username or organization
   - **Space name**: Choose a unique name (e.g., `my-rag-system`)
   - **License**: Choose appropriate license (e.g., MIT)
   - **SDK**: Select **Docker**
   - **Visibility**: Choose Public or Private
   - **Hardware**: Select appropriate hardware (CPU is sufficient for basic usage)

3. **Click "Create Space"**

### Step 2: Upload Files

#### Option A: Using Git (Recommended)

1. **Clone your Space repository:**
```bash
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
```

2. **Copy files from the deployment folder:**
```bash
cp -r ../huggingface_deploy/* .
```

3. **Commit and push:**
```bash
git add .
git commit -m "Initial RAG system deployment with Docker"
git push
```

#### Option B: Using Web Interface

1. **Upload files manually:**
   - Go to your Space's "Files" tab
   - Click "Add file" β†’ "Upload files"
   - Upload all files from the `huggingface_deploy/` folder:
     - `app.py`
     - `rag_system.py`
     - `pdf_processor.py`
     - `requirements.txt`
     - `Dockerfile`
     - `.dockerignore`
     - `README.md`

### Step 3: Configure the Space

1. **Set up environment variables (optional):**
   - Go to your Space's "Settings" tab
   - Add environment variables if needed:
     ```
     EMBEDDING_MODEL=all-MiniLM-L6-v2
     GENERATIVE_MODEL=Qwen/Qwen2.5-1.5B-Instruct
     ```

2. **Configure hardware (if needed):**
   - Go to "Settings" β†’ "Hardware"
   - Select appropriate hardware based on your needs

### Step 4: Deploy and Test

1. **Wait for deployment:**
   - Hugging Face will automatically build and deploy your Docker container
   - This may take 10-15 minutes for the first deployment (model downloads)

2. **Test your application:**
   - Visit your Space URL: `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`
   - Upload a PDF document
   - Ask questions to test the RAG system

## πŸ”§ Docker Configuration

### Dockerfile Features

- **Base Image**: Python 3.10 slim
- **System Dependencies**: build-essential, curl
- **Health Check**: Monitors Streamlit health endpoint
- **Environment Variables**: Configured for Streamlit
- **Port**: Exposes port 8501

### Local Docker Testing

You can test the Docker build locally:

```bash
# Build the Docker image
docker build -t rag-system .

# Run the container
docker run -p 8501:8501 rag-system

# Or use docker-compose
docker-compose up --build
```

## πŸ”§ Configuration Options

### Environment Variables

You can customize your deployment by setting these environment variables in your Space settings:

```bash
# Model configuration
EMBEDDING_MODEL=all-MiniLM-L6-v2
GENERATIVE_MODEL=Qwen/Qwen2.5-1.5B-Instruct

# Chunk sizes
CHUNK_SIZES=100,400

# Vector store path
VECTOR_STORE_PATH=./vector_store

# Streamlit configuration
STREAMLIT_SERVER_PORT=8501
STREAMLIT_SERVER_ADDRESS=0.0.0.0
STREAMLIT_SERVER_HEADLESS=true
```

### Hardware Options

- **CPU**: Sufficient for basic usage, slower inference
- **T4**: Good for faster inference, limited memory
- **A10G**: High performance, more memory
- **A100**: Maximum performance, highest cost

## πŸ› Troubleshooting

### Common Issues

1. **Build Fails**
   - Check that all required files are uploaded
   - Verify `requirements.txt` and `Dockerfile` are correct
   - Check the build logs for specific errors

2. **Model Loading Errors**
   - Ensure internet connectivity for model downloads
   - Check model names are correct
   - Verify sufficient disk space

3. **Memory Issues**
   - Use smaller models
   - Reduce chunk sizes
   - Upgrade to higher-tier hardware

4. **Slow Performance**
   - Upgrade hardware tier
   - Use smaller embedding models
   - Optimize chunk sizes

5. **Docker Build Issues**
   - Check `.dockerignore` excludes unnecessary files
   - Verify Dockerfile syntax
   - Check for missing dependencies

### Debug Mode

To enable debug logging, add this to your `app.py`:

```python
import logging
logging.basicConfig(level=logging.DEBUG)
```

## πŸ“Š Monitoring

### Space Metrics

- **Build Status**: Check if Docker build was successful
- **Runtime Logs**: Monitor application logs
- **Resource Usage**: Track CPU and memory usage
- **Error Logs**: Identify and fix issues

### Docker Logs

Check Docker logs in your Space:
- Go to "Settings" β†’ "Logs"
- Monitor build and runtime logs
- Look for error messages

## πŸ”’ Security Considerations

1. **File Upload:**
   - Validate PDF files before processing
   - Implement file size limits
   - Check file types

2. **Model Access:**
   - Use appropriate model access tokens
   - Consider private models for sensitive data

3. **Data Privacy:**
   - Be aware that uploaded documents are processed
   - Consider data retention policies

4. **Docker Security:**
   - Use non-root user in Dockerfile
   - Minimize attack surface
   - Keep base images updated

## πŸ“ˆ Scaling

### For Production Use

1. **Multiple Spaces:**
   - Create separate Spaces for different use cases
   - Use different hardware tiers as needed

2. **Custom Domains:**
   - Set up custom domains for your Spaces
   - Use proper SSL certificates

3. **Load Balancing:**
   - Consider multiple Space instances
   - Implement proper caching strategies

## πŸŽ‰ Success Checklist

- [ ] Space created successfully with Docker SDK
- [ ] All files uploaded (including Dockerfile)
- [ ] Docker build completed without errors
- [ ] Application loads correctly
- [ ] PDF upload works
- [ ] Question answering works
- [ ] Search results display correctly
- [ ] Performance is acceptable

## πŸ“ž Support

If you encounter issues:

1. **Check the logs** in your Space's "Logs" tab
2. **Review this guide** for common solutions
3. **Search Hugging Face documentation**
4. **Create an issue** in the project repository
5. **Contact Hugging Face support** for Space-specific issues

## πŸš€ Next Steps

After successful deployment:

1. **Test thoroughly** with different document types
2. **Optimize performance** based on usage patterns
3. **Add custom features** as needed
4. **Share your Space** with others
5. **Monitor usage** and gather feedback

## πŸ”„ Updates and Maintenance

### Updating Your Space

1. **Make changes locally**
2. **Test with Docker locally**
3. **Push changes to your Space repository**
4. **Monitor the rebuild process**

### Version Management

- Use specific versions in `requirements.txt`
- Tag your Docker images
- Keep track of model versions

---

**Happy deploying with Docker! πŸ³πŸš€**