JatsTheAIGen commited on
Commit
83fb1b5
Β·
1 Parent(s): ca75c2d

api migration v1

Browse files
Files changed (1) hide show
  1. DOCKER_SDK_SETUP.md +233 -0
DOCKER_SDK_SETUP.md ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Docker SDK Setup Guide
2
+
3
+ ## Overview
4
+
5
+ The Research AI Assistant now uses **Docker SDK** instead of Gradio SDK for Hugging Face Spaces deployment. This provides more control and flexibility.
6
+
7
+ ## Changes Made
8
+
9
+ ### 1. README.md Configuration
10
+
11
+ Changed from:
12
+ ```yaml
13
+ sdk: gradio
14
+ app_file: app.py
15
+ ```
16
+
17
+ To:
18
+ ```yaml
19
+ sdk: docker
20
+ app_port: 7860
21
+ ```
22
+
23
+ ### 2. Dockerfile Configuration
24
+
25
+ **Key Features:**
26
+ - **Base Image**: Python 3.10-slim
27
+ - **Entry Point**: `main.py` (starts both Gradio and Flask API)
28
+ - **Ports**:
29
+ - 7860: Gradio UI (primary, exposed by HF Spaces)
30
+ - 5001: Flask API (background thread)
31
+ - **Health Check**: Monitors Gradio endpoint
32
+
33
+ ## How It Works
34
+
35
+ ### Startup Sequence
36
+
37
+ 1. **Docker Container Starts**
38
+ - Runs `python main.py`
39
+
40
+ 2. **main.py Initializes**
41
+ - Starts Flask API in background thread (port 5001)
42
+ - Creates Gradio interface from `app.py`
43
+ - Launches Gradio on port 7860
44
+
45
+ 3. **Both Services Available**
46
+ - Gradio UI: `https://your-space.hf.space` (port 7860)
47
+ - Flask API: Available internally (port 5001)
48
+
49
+ ## File Structure
50
+
51
+ ```
52
+ Research_AI_Assistant/
53
+ β”œβ”€β”€ Dockerfile # Main Dockerfile (used by HF Spaces)
54
+ β”œβ”€β”€ Dockerfile.hf # Alternative/backup Dockerfile
55
+ β”œβ”€β”€ README.md # HF Spaces config (sdk: docker)
56
+ β”œβ”€β”€ main.py # Entry point (starts Flask + Gradio)
57
+ β”œβ”€β”€ app.py # Gradio interface definition
58
+ └── flask_api.py # Flask API (started by main.py)
59
+ ```
60
+
61
+ ## Benefits of Docker SDK
62
+
63
+ ### 1. **More Control**
64
+ - Full control over container environment
65
+ - Can install system dependencies
66
+ - Custom startup scripts
67
+
68
+ ### 2. **Multiple Services**
69
+ - Can run multiple processes (Gradio + Flask)
70
+ - Better resource management
71
+ - Health checks
72
+
73
+ ### 3. **Flexibility**
74
+ - Not limited to Gradio's constraints
75
+ - Can add additional services
76
+ - Better debugging
77
+
78
+ ### 4. **Production Ready**
79
+ - Standard Docker patterns
80
+ - Easier to migrate to other platforms
81
+ - Better for CI/CD
82
+
83
+ ## Deployment
84
+
85
+ ### On Hugging Face Spaces
86
+
87
+ 1. **Push to Repository**
88
+ ```bash
89
+ git push origin main
90
+ ```
91
+
92
+ 2. **HF Spaces Auto-Builds**
93
+ - Detects `sdk: docker` in README.md
94
+ - Builds Docker image from Dockerfile
95
+ - Exposes port 7860
96
+
97
+ 3. **Services Start**
98
+ - Flask API starts in background
99
+ - Gradio UI launches on port 7860
100
+ - Both share orchestrator instance
101
+
102
+ ### Local Testing
103
+
104
+ Test the Docker build locally:
105
+
106
+ ```bash
107
+ # Build image
108
+ docker build -t research-assistant .
109
+
110
+ # Run container
111
+ docker run -p 7860:7860 -p 5001:5001 \
112
+ -e HF_TOKEN=your_token \
113
+ research-assistant
114
+ ```
115
+
116
+ ## Port Configuration
117
+
118
+ ### Exposed Ports
119
+
120
+ - **7860**: Gradio UI (primary, exposed by HF Spaces)
121
+ - Accessible at: `https://your-space.hf.space`
122
+
123
+ - **5001**: Flask API (internal, background thread)
124
+ - Not directly exposed by HF Spaces
125
+ - Can be accessed via port forwarding if needed
126
+
127
+ ### Internal Communication
128
+
129
+ - Flask API runs in same container
130
+ - Both services share Python process
131
+ - Shared orchestrator instance
132
+ - No network overhead
133
+
134
+ ## Environment Variables
135
+
136
+ Set in HF Spaces Secrets:
137
+
138
+ ```bash
139
+ HF_TOKEN=your_huggingface_token
140
+ ENABLE_FLASK_API=true # Default: true
141
+ FLASK_PORT=5001 # Default: 5001
142
+ FLASK_HOST=0.0.0.0 # Default: 0.0.0.0
143
+ ```
144
+
145
+ ## Health Checks
146
+
147
+ The Dockerfile includes a health check:
148
+
149
+ ```dockerfile
150
+ HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
151
+ CMD curl -f http://localhost:7860/ || exit 1
152
+ ```
153
+
154
+ - Checks Gradio endpoint every 30 seconds
155
+ - 60-second startup period (allows initialization)
156
+ - 3 retries before marking unhealthy
157
+
158
+ ## Troubleshooting
159
+
160
+ ### Issue: Build Fails
161
+
162
+ **Check:**
163
+ 1. Dockerfile syntax is correct
164
+ 2. Requirements.txt has all dependencies
165
+ 3. Python 3.10 is available
166
+
167
+ **Solution:**
168
+ ```bash
169
+ # Test build locally
170
+ docker build -t test-build .
171
+ ```
172
+
173
+ ### Issue: Port Not Accessible
174
+
175
+ **Check:**
176
+ 1. `app_port: 7860` in README.md
177
+ 2. Gradio is listening on 0.0.0.0:7860
178
+ 3. No firewall blocking
179
+
180
+ **Solution:**
181
+ - Verify in logs: "Running on local URL: http://0.0.0.0:7860"
182
+
183
+ ### Issue: Flask API Not Starting
184
+
185
+ **Check:**
186
+ 1. Logs show Flask initialization
187
+ 2. `ENABLE_FLASK_API=true` (default)
188
+ 3. Port 5001 not conflicting
189
+
190
+ **Solution:**
191
+ - Check logs for "Starting Flask API in background thread..."
192
+ - Verify Flask thread starts successfully
193
+
194
+ ## Comparison: Gradio SDK vs Docker SDK
195
+
196
+ | Feature | Gradio SDK | Docker SDK |
197
+ |---------|------------|------------|
198
+ | **Setup** | Simple | More control |
199
+ | **Multiple Services** | ❌ Limited | βœ… Full control |
200
+ | **System Dependencies** | ⚠️ Limited | βœ… Full access |
201
+ | **Customization** | ⚠️ Limited | βœ… Complete |
202
+ | **Debugging** | ⚠️ Harder | βœ… Easier |
203
+ | **Port Configuration** | ⚠️ Fixed | βœ… Flexible |
204
+ | **Health Checks** | ❌ No | βœ… Yes |
205
+
206
+ ## Migration Notes
207
+
208
+ ### From Gradio SDK
209
+
210
+ If migrating from Gradio SDK:
211
+
212
+ 1. βœ… **README.md**: Changed `sdk: gradio` β†’ `sdk: docker`
213
+ 2. βœ… **Dockerfile**: Created/updated to use `main.py`
214
+ 3. βœ… **Entry Point**: Changed from `app.py` β†’ `main.py`
215
+ 4. βœ… **Ports**: Explicitly configured (7860, 5001)
216
+
217
+ ### What Still Works
218
+
219
+ - βœ… All Gradio functionality preserved
220
+ - βœ… All API endpoints work
221
+ - βœ… Flask API integration works
222
+ - βœ… Orchestrator initialization unchanged
223
+
224
+ ## Summary
225
+
226
+ βœ… **Docker SDK**: More control and flexibility
227
+ βœ… **Dual Services**: Gradio + Flask API
228
+ βœ… **Production Ready**: Standard Docker patterns
229
+ βœ… **Health Checks**: Built-in monitoring
230
+ βœ… **Flexible**: Easy to extend and customize
231
+
232
+ The application now uses Docker SDK for better control and flexibility while maintaining all existing functionality.
233
+