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.
|