File size: 5,644 Bytes
83fb1b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Docker SDK Setup Guide

## Overview

The Research AI Assistant now uses **Docker SDK** instead of Gradio SDK for Hugging Face Spaces deployment. This provides more control and flexibility.

## Changes Made

### 1. README.md Configuration

Changed from:
```yaml
sdk: gradio
app_file: app.py
```

To:
```yaml
sdk: docker
app_port: 7860
```

### 2. Dockerfile Configuration

**Key Features:**
- **Base Image**: Python 3.10-slim
- **Entry Point**: `main.py` (starts both Gradio and Flask API)
- **Ports**: 
  - 7860: Gradio UI (primary, exposed by HF Spaces)
  - 5001: Flask API (background thread)
- **Health Check**: Monitors Gradio endpoint

## How It Works

### Startup Sequence

1. **Docker Container Starts**
   - Runs `python main.py`

2. **main.py Initializes**
   - Starts Flask API in background thread (port 5001)
   - Creates Gradio interface from `app.py`
   - Launches Gradio on port 7860

3. **Both Services Available**
   - Gradio UI: `https://your-space.hf.space` (port 7860)
   - Flask API: Available internally (port 5001)

## File Structure

```
Research_AI_Assistant/
β”œβ”€β”€ Dockerfile              # Main Dockerfile (used by HF Spaces)
β”œβ”€β”€ Dockerfile.hf           # Alternative/backup Dockerfile
β”œβ”€β”€ README.md               # HF Spaces config (sdk: docker)
β”œβ”€β”€ main.py                 # Entry point (starts Flask + Gradio)
β”œβ”€β”€ app.py                  # Gradio interface definition
└── flask_api.py            # Flask API (started by main.py)
```

## Benefits of Docker SDK

### 1. **More Control**
- Full control over container environment
- Can install system dependencies
- Custom startup scripts

### 2. **Multiple Services**
- Can run multiple processes (Gradio + Flask)
- Better resource management
- Health checks

### 3. **Flexibility**
- Not limited to Gradio's constraints
- Can add additional services
- Better debugging

### 4. **Production Ready**
- Standard Docker patterns
- Easier to migrate to other platforms
- Better for CI/CD

## Deployment

### On Hugging Face Spaces

1. **Push to Repository**
   ```bash
   git push origin main
   ```

2. **HF Spaces Auto-Builds**
   - Detects `sdk: docker` in README.md
   - Builds Docker image from Dockerfile
   - Exposes port 7860

3. **Services Start**
   - Flask API starts in background
   - Gradio UI launches on port 7860
   - Both share orchestrator instance

### Local Testing

Test the Docker build locally:

```bash
# Build image
docker build -t research-assistant .

# Run container
docker run -p 7860:7860 -p 5001:5001 \
  -e HF_TOKEN=your_token \
  research-assistant
```

## Port Configuration

### Exposed Ports

- **7860**: Gradio UI (primary, exposed by HF Spaces)
  - Accessible at: `https://your-space.hf.space`
  
- **5001**: Flask API (internal, background thread)
  - Not directly exposed by HF Spaces
  - Can be accessed via port forwarding if needed

### Internal Communication

- Flask API runs in same container
- Both services share Python process
- Shared orchestrator instance
- No network overhead

## Environment Variables

Set in HF Spaces Secrets:

```bash
HF_TOKEN=your_huggingface_token
ENABLE_FLASK_API=true      # Default: true
FLASK_PORT=5001            # Default: 5001
FLASK_HOST=0.0.0.0        # Default: 0.0.0.0
```

## Health Checks

The Dockerfile includes a health check:

```dockerfile
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:7860/ || exit 1
```

- Checks Gradio endpoint every 30 seconds
- 60-second startup period (allows initialization)
- 3 retries before marking unhealthy

## Troubleshooting

### Issue: Build Fails

**Check:**
1. Dockerfile syntax is correct
2. Requirements.txt has all dependencies
3. Python 3.10 is available

**Solution:**
```bash
# Test build locally
docker build -t test-build .
```

### Issue: Port Not Accessible

**Check:**
1. `app_port: 7860` in README.md
2. Gradio is listening on 0.0.0.0:7860
3. No firewall blocking

**Solution:**
- Verify in logs: "Running on local URL: http://0.0.0.0:7860"

### Issue: Flask API Not Starting

**Check:**
1. Logs show Flask initialization
2. `ENABLE_FLASK_API=true` (default)
3. Port 5001 not conflicting

**Solution:**
- Check logs for "Starting Flask API in background thread..."
- Verify Flask thread starts successfully

## Comparison: Gradio SDK vs Docker SDK

| Feature | Gradio SDK | Docker SDK |
|---------|------------|------------|
| **Setup** | Simple | More control |
| **Multiple Services** | ❌ Limited | βœ… Full control |
| **System Dependencies** | ⚠️ Limited | βœ… Full access |
| **Customization** | ⚠️ Limited | βœ… Complete |
| **Debugging** | ⚠️ Harder | βœ… Easier |
| **Port Configuration** | ⚠️ Fixed | βœ… Flexible |
| **Health Checks** | ❌ No | βœ… Yes |

## Migration Notes

### From Gradio SDK

If migrating from Gradio SDK:

1. βœ… **README.md**: Changed `sdk: gradio` β†’ `sdk: docker`
2. βœ… **Dockerfile**: Created/updated to use `main.py`
3. βœ… **Entry Point**: Changed from `app.py` β†’ `main.py`
4. βœ… **Ports**: Explicitly configured (7860, 5001)

### What Still Works

- βœ… All Gradio functionality preserved
- βœ… All API endpoints work
- βœ… Flask API integration works
- βœ… Orchestrator initialization unchanged

## Summary

βœ… **Docker SDK**: More control and flexibility  
βœ… **Dual Services**: Gradio + Flask API  
βœ… **Production Ready**: Standard Docker patterns  
βœ… **Health Checks**: Built-in monitoring  
βœ… **Flexible**: Easy to extend and customize  

The application now uses Docker SDK for better control and flexibility while maintaining all existing functionality.