JatsTheAIGen commited on
Commit
2fe635d
Β·
1 Parent(s): a1cdcd6

api endpoints for UI migration v8

Browse files
Files changed (4) hide show
  1. GRADIO_API_CONFLICT_DIAGNOSIS.md +414 -0
  2. QUICK_LOG_CHECK.md +99 -0
  3. app.py +2 -1
  4. main.py +1 -1
GRADIO_API_CONFLICT_DIAGNOSIS.md ADDED
@@ -0,0 +1,414 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio API Endpoint Conflict Diagnosis Guide
2
+
3
+ ## Overview
4
+
5
+ This document helps diagnose and resolve conflicts with Gradio API endpoints in the Research AI Assistant application.
6
+
7
+ ## Current Setup
8
+
9
+ - **Framework**: Gradio (Python), NOT Express/Node.js
10
+ - **Port**: 7860 (configurable via `GRADIO_PORT` env var)
11
+ - **Host**: 0.0.0.0 (all interfaces)
12
+ - **API Enabled**: Yes (`show_api=True`)
13
+ - **Deployment**: Hugging Face Spaces / Local
14
+
15
+ ## Common Conflict Scenarios
16
+
17
+ ### 1. Port Conflicts
18
+
19
+ **Symptoms:**
20
+ - Error: "Address already in use"
21
+ - Application fails to start
22
+ - Port binding errors in logs
23
+
24
+ **Diagnosis:**
25
+ ```bash
26
+ # Check if port 7860 is already in use
27
+ # Windows
28
+ netstat -ano | findstr :7860
29
+
30
+ # Linux/Mac
31
+ lsof -i :7860
32
+ # or
33
+ netstat -tulpn | grep 7860
34
+ ```
35
+
36
+ **Solutions:**
37
+
38
+ **Option A: Change Gradio Port**
39
+ ```python
40
+ # In app.py, modify demo.launch():
41
+ demo.launch(
42
+ server_name="0.0.0.0",
43
+ server_port=7861, # Use different port
44
+ show_api=True
45
+ )
46
+ ```
47
+
48
+ **Option B: Use Environment Variable**
49
+ ```bash
50
+ # Set before running
51
+ export GRADIO_PORT=7861
52
+ python app.py
53
+ ```
54
+
55
+ **Option C: Kill Conflicting Process**
56
+ ```bash
57
+ # Windows (find PID from netstat)
58
+ taskkill /PID <PID> /F
59
+
60
+ # Linux/Mac
61
+ kill -9 <PID>
62
+ ```
63
+
64
+ ---
65
+
66
+ ### 2. Multiple Gradio Instances Running
67
+
68
+ **Symptoms:**
69
+ - Multiple instances competing for the same port
70
+ - Unpredictable endpoint routing
71
+ - Some endpoints work, others don't
72
+
73
+ **Diagnosis:**
74
+ Check for multiple Python processes:
75
+ ```bash
76
+ # Windows
77
+ tasklist | findstr python
78
+
79
+ # Linux/Mac
80
+ ps aux | grep python | grep app.py
81
+ ```
82
+
83
+ **Solution:**
84
+ Ensure only one instance is running:
85
+ ```bash
86
+ # Kill all Python processes (use with caution!)
87
+ # Windows
88
+ taskkill /IM python.exe /F
89
+
90
+ # Linux/Mac
91
+ pkill -f app.py
92
+ ```
93
+
94
+ ---
95
+
96
+ ### 3. API Endpoint Routing Conflicts
97
+
98
+ **Symptoms:**
99
+ - 404 errors on `/api/*` endpoints
100
+ - Endpoints return wrong responses
101
+ - API documentation not accessible at `/docs`
102
+
103
+ **Diagnosis:**
104
+
105
+ **Check 1: Verify API is Enabled**
106
+ ```python
107
+ # Add to app.py after demo creation
108
+ print(f"API Open: {demo.config.api_open}")
109
+ print(f"Show API: {demo.config.show_api}")
110
+ ```
111
+
112
+ **Check 2: Verify Endpoint Registration**
113
+ Look for this in logs:
114
+ ```
115
+ βœ“ API endpoint '/api/safe_gpu_chat_handler' registered with api_name
116
+ βœ“ API handler function initialized: True
117
+ ```
118
+
119
+ **Check 3: Test Endpoint Directly**
120
+ ```bash
121
+ # Check if API docs are accessible
122
+ curl http://localhost:7860/docs
123
+
124
+ # Test endpoint
125
+ curl -X POST http://localhost:7860/api/safe_gpu_chat_handler \
126
+ -H "Content-Type: application/json" \
127
+ -d '{"data": ["test", [], "Admin_J", ""]}'
128
+ ```
129
+
130
+ **Solutions:**
131
+
132
+ **Ensure API is Explicitly Enabled:**
133
+ ```python
134
+ # In app.py (line 2109-2116)
135
+ demo.launch(
136
+ server_name="0.0.0.0",
137
+ server_port=7860,
138
+ share=False,
139
+ show_api=True, # βœ… This must be True
140
+ # ... other settings
141
+ )
142
+ ```
143
+
144
+ **Verify Handler Function is Set:**
145
+ ```python
146
+ # Check that _api_chat_handler_fn is not None
147
+ # This is set at lines 1899-1900 or 2002-2003 in app.py
148
+ ```
149
+
150
+ ---
151
+
152
+ ### 4. CORS Issues (Cross-Origin Resource Sharing)
153
+
154
+ **Symptoms:**
155
+ - Browser console shows CORS errors
156
+ - API calls from external domains fail
157
+ - "Access-Control-Allow-Origin" errors
158
+
159
+ **Solutions:**
160
+
161
+ **Option A: Enable CORS in Gradio Launch**
162
+ ```python
163
+ demo.launch(
164
+ server_name="0.0.0.0",
165
+ server_port=7860,
166
+ show_api=True,
167
+ # Add CORS configuration
168
+ allowed_paths=["*"], # Allow all paths (or specify)
169
+ # Note: Gradio handles CORS automatically when show_api=True
170
+ )
171
+ ```
172
+
173
+ **Option B: Use Gradio's Share Feature (for external access)**
174
+ ```python
175
+ demo.launch(
176
+ server_name="0.0.0.0",
177
+ server_port=7860,
178
+ share=True, # Creates public Gradio link
179
+ show_api=True
180
+ )
181
+ ```
182
+
183
+ **Option C: Add CORS Headers Manually (if needed)**
184
+ ```python
185
+ # If using FastAPI/Flask alongside Gradio, add CORS middleware
186
+ # But this app doesn't use FastAPI/Flask, so this shouldn't be needed
187
+ ```
188
+
189
+ ---
190
+
191
+ ### 5. Hugging Face Spaces Deployment Conflicts
192
+
193
+ **Symptoms:**
194
+ - Works locally but not on HF Spaces
195
+ - Port conflicts in container
196
+ - API endpoints unavailable
197
+
198
+ **Solutions:**
199
+
200
+ **Check HF Spaces Configuration:**
201
+ 1. Ensure `Dockerfile.hf` exposes correct port:
202
+ ```dockerfile
203
+ EXPOSE 7860
204
+ ```
205
+
206
+ 2. Verify `app.py` is the main entry point (check `README.md` for `sdk: gradio`)
207
+
208
+ 3. Check HF Spaces logs for startup errors:
209
+ - Go to your Space settings
210
+ - View Logs tab
211
+ - Look for "LAUNCHING GRADIO APP" message
212
+
213
+ **Common HF Spaces Issues:**
214
+ - Port 7860 is required (cannot be changed on HF Spaces)
215
+ - `server_name="0.0.0.0"` is correct
216
+ - `share=True` creates an additional public link (HF Spaces also provides its own URL)
217
+
218
+ ---
219
+
220
+ ### 6. API Endpoint Not Found (404)
221
+
222
+ **Symptoms:**
223
+ - `/api/safe_gpu_chat_handler` returns 404
224
+ - Endpoint not listed in `/docs`
225
+
226
+ **Diagnosis Checklist:**
227
+
228
+ 1. βœ… **Handler Function Initialized?**
229
+ ```python
230
+ # Check logs for:
231
+ # "βœ“ API handler function initialized: True"
232
+ ```
233
+
234
+ 2. βœ… **api_name Parameter Set?**
235
+ ```python
236
+ # In app.py line 845:
237
+ api_message.submit(
238
+ fn=api_chat_handler,
239
+ inputs=[...],
240
+ outputs=[...],
241
+ api_name="safe_gpu_chat_handler" # βœ… Must be present
242
+ )
243
+ ```
244
+
245
+ 3. βœ… **show_api=True in launch()?**
246
+ ```python
247
+ demo.launch(show_api=True) # βœ… Must be True
248
+ ```
249
+
250
+ 4. βœ… **App Restarted After Changes?**
251
+ - Gradio requires restart to register new endpoints
252
+
253
+ **Solution:**
254
+ ```python
255
+ # Verify in app.py:
256
+ # 1. Handler function exists (lines 804-836)
257
+ # 2. api_name is set (line 845)
258
+ # 3. show_api=True in launch (line 2113)
259
+ # 4. Restart the application
260
+ ```
261
+
262
+ ---
263
+
264
+ ### 7. Gradio vs. FastAPI/Flask Conflicts (Not Applicable Here)
265
+
266
+ **Note:** This application uses **only Gradio** - there is no FastAPI or Flask server running.
267
+
268
+ If you're trying to add FastAPI alongside:
269
+ ```python
270
+ # DON'T do this - Gradio handles routing
271
+ from fastapi import FastAPI
272
+ app = FastAPI() # This would conflict!
273
+ ```
274
+
275
+ Gradio's internal server handles all routing. You don't need Express, FastAPI, or Flask.
276
+
277
+ ---
278
+
279
+ ## Diagnostic Commands
280
+
281
+ ### Complete Health Check
282
+
283
+ ```bash
284
+ # 1. Check if app is running
285
+ curl http://localhost:7860/
286
+
287
+ # 2. Check API documentation
288
+ curl http://localhost:7860/docs
289
+
290
+ # 3. Check API info endpoint
291
+ curl http://localhost:7860/api_info
292
+
293
+ # 4. Test chat endpoint
294
+ curl -X POST http://localhost:7860/api/safe_gpu_chat_handler \
295
+ -H "Content-Type: application/json" \
296
+ -d '{"data": ["Hello", [], "Admin_J", ""]}'
297
+
298
+ # 5. Check logs for errors
299
+ # Windows
300
+ type app.log | findstr ERROR
301
+
302
+ # Linux/Mac
303
+ grep ERROR app.log
304
+ ```
305
+
306
+ ### Verify Endpoint Registration
307
+
308
+ ```bash
309
+ # Use the provided verification script
310
+ python verify_api_endpoint.py http://localhost:7860
311
+ ```
312
+
313
+ ---
314
+
315
+ ## Configuration Reference
316
+
317
+ ### Current Launch Configuration (app.py:2109-2116)
318
+
319
+ ```python
320
+ demo.launch(
321
+ server_name="0.0.0.0", # Accept connections from all interfaces
322
+ server_port=7860, # Default Gradio port
323
+ share=True, # Creates public Gradio link for external access
324
+ show_api=True, # Enable API endpoints
325
+ allowed_paths=[], # Security: don't serve files
326
+ blocked_paths=["/tmp", "/var", "/etc", "/home"] # Block sensitive paths
327
+ )
328
+ ```
329
+
330
+ ### Environment Variables
331
+
332
+ ```bash
333
+ # Port configuration (optional - defaults to 7860)
334
+ export GRADIO_PORT=7860
335
+
336
+ # Host configuration (optional - defaults to 0.0.0.0)
337
+ export GRADIO_HOST=0.0.0.0
338
+ ```
339
+
340
+ ---
341
+
342
+ ## Troubleshooting Checklist
343
+
344
+ Use this checklist when experiencing issues:
345
+
346
+ - [ ] Port 7860 is not in use by another process
347
+ - [ ] Only one instance of `app.py` is running
348
+ - [ ] `show_api=True` is set in `demo.launch()`
349
+ - [ ] `api_name` parameter is set on the submit event
350
+ - [ ] Handler function (`_api_chat_handler_fn`) is not None
351
+ - [ ] Application logs show "API endpoint registered" message
352
+ - [ ] `/docs` endpoint is accessible
353
+ - [ ] `/api_info` endpoint returns JSON
354
+ - [ ] Application was restarted after code changes
355
+ - [ ] No CORS errors in browser console (if accessing from browser)
356
+ - [ ] HF Spaces configuration is correct (if deploying to Spaces)
357
+
358
+ ---
359
+
360
+ ## Quick Fixes
361
+
362
+ ### Fix 1: Port Conflict
363
+ ```bash
364
+ # Change port in app.py line 2111:
365
+ server_port=7861
366
+ ```
367
+
368
+ ### Fix 2: API Not Enabled
369
+ ```python
370
+ # Ensure in app.py line 2113:
371
+ show_api=True # Not False!
372
+ ```
373
+
374
+ ### Fix 3: Handler Not Set
375
+ ```python
376
+ # Check lines 1899-1900 or 2002-2003:
377
+ # _api_chat_handler_fn must be assigned
378
+ ```
379
+
380
+ ### Fix 4: Restart Required
381
+ ```bash
382
+ # Stop and restart the application
383
+ # Endpoints only register on startup
384
+ ```
385
+
386
+ ---
387
+
388
+ ## Getting Help
389
+
390
+ If issues persist:
391
+
392
+ 1. **Check Logs**: Review `app.log` for error messages
393
+ 2. **Run Verification Script**: `python verify_api_endpoint.py`
394
+ 3. **Test Manually**: Use curl commands above
395
+ 4. **Check HF Spaces Logs**: If deploying to Hugging Face
396
+
397
+ ---
398
+
399
+ ## Summary
400
+
401
+ This is a **Gradio-only** application. There are no Express, FastAPI, or Flask servers. All routing is handled by Gradio's internal server. Common conflicts are:
402
+
403
+ 1. **Port conflicts** β†’ Change port or kill conflicting process
404
+ 2. **API not enabled** β†’ Set `show_api=True`
405
+ 3. **Handler not initialized** β†’ Check initialization code
406
+ 4. **Multiple instances** β†’ Ensure only one is running
407
+ 5. **CORS issues** β†’ Gradio handles automatically, but check if accessing from external domain
408
+
409
+ If you're experiencing specific errors, share:
410
+ - The exact error message
411
+ - Where you're running (local, HF Spaces, etc.)
412
+ - The endpoint that's failing
413
+ - Log output from app startup
414
+
QUICK_LOG_CHECK.md ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Quick Log Check Guide - API Endpoint Deployment
2
+
3
+ ## What to Look For
4
+
5
+ When the application starts successfully, you should see these log messages **in order**:
6
+
7
+ ### 1. Initial Setup (Early in startup)
8
+ ```
9
+ ============================================================
10
+ STARTING APP
11
+ ============================================================
12
+ ```
13
+
14
+ ### 2. Interface Creation
15
+ ```
16
+ βœ“ Interface created
17
+ Orchestrator available: True (or False)
18
+ ```
19
+
20
+ ### 3. **API Endpoint Registration** ⭐ KEY CHECKPOINT
21
+ ```
22
+ βœ“ API endpoint '/api/safe_gpu_chat_handler' registered with api_name
23
+ ```
24
+
25
+ ### 4. Handler Initialization ⭐ KEY CHECKPOINT
26
+ Either:
27
+ ```
28
+ βœ“ GPU chat handler initialized and set as API handler
29
+ ```
30
+ OR
31
+ ```
32
+ βœ“ Non-GPU chat handler initialized and set as API handler
33
+ ```
34
+
35
+ ### 5. Startup Verification ⭐ KEY CHECKPOINT
36
+ ```
37
+ ============================================================
38
+ VERIFYING API ENDPOINTS
39
+ ============================================================
40
+ βœ“ API is enabled in Gradio config: True
41
+ Registered API endpoints:
42
+ - /api/safe_gpu_chat_handler (POST)
43
+ - /api/new_session (POST)
44
+ ...
45
+ βœ“ API handler function initialized: True
46
+ Handler type: function
47
+ ```
48
+
49
+ ### 6. Application Launch
50
+ ```
51
+ ============================================================
52
+ LAUNCHING GRADIO APP
53
+ ============================================================
54
+ API endpoints available at: http://<host>:7860/api/
55
+ ```
56
+
57
+ ## Verification Checklist
58
+
59
+ - [ ] See "API endpoint '/api/safe_gpu_chat_handler' registered with api_name"
60
+ - [ ] See handler initialization message (GPU or Non-GPU)
61
+ - [ ] See "VERIFYING API ENDPOINTS" section
62
+ - [ ] See "API handler function initialized: True"
63
+ - [ ] See "API endpoints available at: http://<host>:7860/api/"
64
+
65
+ ## If Logs Show Different Messages
66
+
67
+ ### Missing "API endpoint registered" message
68
+ **Problem**: Endpoint might not be registered
69
+ **Action**: Check if `api_name="safe_gpu_chat_handler"` is in the code (line 845)
70
+
71
+ ### Missing handler initialization
72
+ **Problem**: Handler function might not be set
73
+ **Action**: Check GPU availability and handler function definition
74
+
75
+ ### Handler function initialized: False
76
+ **Problem**: `_api_chat_handler_fn` is None
77
+ **Action**: Check if handlers are defined before interface creation
78
+
79
+ ## Current Logs Analysis
80
+
81
+ Based on your logs:
82
+ - βœ… Application is starting ("starting up user application")
83
+ - ⏳ Need to check for Python/Gradio app logs
84
+ - ⏳ Need to see if API registration messages appear
85
+
86
+ **Next Steps**: Wait for the Python application to fully start and check for the messages above.
87
+
88
+ ## Quick Test
89
+
90
+ Once you see the "LAUNCHING GRADIO APP" message, the endpoint should be accessible. You can test with:
91
+
92
+ ```bash
93
+ curl -X POST https://<your-space-url>/api/safe_gpu_chat_handler \
94
+ -H "Content-Type: application/json" \
95
+ -d '{"data": ["test", [], "Admin_J", "Session: test | User: Admin_J | Interactions: 0"]}'
96
+ ```
97
+
98
+ Expected: Response (200 or 422 = endpoint exists, 404 = not deployed)
99
+
app.py CHANGED
@@ -2106,10 +2106,11 @@ if __name__ == "__main__":
2106
  logger.info("LAUNCHING GRADIO APP")
2107
  logger.info("=" * 60)
2108
  logger.info("API endpoints available at: http://<host>:7860/api/")
 
2109
  demo.launch(
2110
  server_name="0.0.0.0",
2111
  server_port=7860,
2112
- share=False,
2113
  show_api=True, # Explicitly enable API (recommended for API access)
2114
  allowed_paths=[], # Empty list = don't serve any files (prevents file URLs)
2115
  blocked_paths=["/tmp", "/var", "/etc", "/home"] # Explicitly block sensitive paths (extra security)
 
2106
  logger.info("LAUNCHING GRADIO APP")
2107
  logger.info("=" * 60)
2108
  logger.info("API endpoints available at: http://<host>:7860/api/")
2109
+ logger.info("Creating public Gradio share link for external access...")
2110
  demo.launch(
2111
  server_name="0.0.0.0",
2112
  server_port=7860,
2113
+ share=True, # Creates public Gradio link for external access
2114
  show_api=True, # Explicitly enable API (recommended for API access)
2115
  allowed_paths=[], # Empty list = don't serve any files (prevents file URLs)
2116
  blocked_paths=["/tmp", "/var", "/etc", "/home"] # Explicitly block sensitive paths (extra security)
main.py CHANGED
@@ -172,7 +172,7 @@ def main():
172
  launch_config = {
173
  'server_name': '0.0.0.0',
174
  'server_port': 7860,
175
- 'share': False,
176
  'debug': False
177
  }
178
 
 
172
  launch_config = {
173
  'server_name': '0.0.0.0',
174
  'server_port': 7860,
175
+ 'share': True, # Creates public Gradio link for external access
176
  'debug': False
177
  }
178