Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from datetime import datetime
|
|
8 |
import os
|
9 |
|
10 |
# Configuration
|
11 |
-
MCP_SERVER_PORT = 8001
|
12 |
MCP_SERVER_URL = "https://elanuk-mcp-hf.hf.space/"
|
13 |
|
14 |
# Bihar districts list
|
@@ -136,10 +136,19 @@ def test_mcp_workflow(district):
|
|
136 |
"district": district.lower()
|
137 |
}
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
response = requests.post(
|
140 |
f"{MCP_SERVER_URL}/api/run-workflow",
|
141 |
json=payload,
|
142 |
-
timeout=60
|
143 |
)
|
144 |
|
145 |
if response.status_code == 200:
|
@@ -150,29 +159,46 @@ def test_mcp_workflow(district):
|
|
150 |
csv_content = result.get('csv', '')
|
151 |
|
152 |
return workflow_output, alert_summary, csv_content
|
153 |
-
|
|
|
154 |
else:
|
155 |
-
error_msg = f"β Server Error ({response.status_code}): {response.text}"
|
156 |
return error_msg, "", ""
|
157 |
|
158 |
except requests.exceptions.Timeout:
|
159 |
-
return "β° Request timed out. The
|
160 |
except requests.exceptions.ConnectionError:
|
161 |
-
return f"π Connection Error: Cannot reach MCP server
|
162 |
except Exception as e:
|
163 |
return f"β Error: {str(e)}", "", ""
|
164 |
|
165 |
def check_server_health():
|
166 |
"""Check if the MCP server is running"""
|
167 |
try:
|
|
|
168 |
response = requests.get(f"{MCP_SERVER_URL}/api/health", timeout=10)
|
169 |
if response.status_code == 200:
|
170 |
data = response.json()
|
171 |
return f"β
Server Online | OpenAI: {'β
' if data.get('openai_available') else 'β'} | Time: {data.get('timestamp', 'N/A')}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
else:
|
173 |
return f"β οΈ Server responded with status {response.status_code}"
|
|
|
|
|
|
|
|
|
174 |
except Exception as e:
|
175 |
-
return f"β Server
|
176 |
|
177 |
# Start server in background thread
|
178 |
print("π§ Initializing BIHAR AgMCP...")
|
@@ -220,14 +246,16 @@ with gr.Blocks(
|
|
220 |
|
221 |
# Server status
|
222 |
with gr.Row():
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
|
|
|
|
231 |
|
232 |
# Main interface
|
233 |
with gr.Row():
|
@@ -235,8 +263,8 @@ with gr.Blocks(
|
|
235 |
district_input = gr.Dropdown(
|
236 |
choices=BIHAR_DISTRICTS,
|
237 |
label="π Select Bihar District",
|
238 |
-
|
239 |
-
|
240 |
)
|
241 |
|
242 |
run_btn = gr.Button(
|
@@ -289,6 +317,36 @@ with gr.Blocks(
|
|
289 |
|
290 |
# Connect events
|
291 |
refresh_btn.click(check_server_health, outputs=server_status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
run_btn.click(
|
293 |
run_workflow_with_csv,
|
294 |
inputs=[district_input],
|
|
|
8 |
import os
|
9 |
|
10 |
# Configuration
|
11 |
+
MCP_SERVER_PORT = 8001
|
12 |
MCP_SERVER_URL = "https://elanuk-mcp-hf.hf.space/"
|
13 |
|
14 |
# Bihar districts list
|
|
|
136 |
"district": district.lower()
|
137 |
}
|
138 |
|
139 |
+
# First check if server is responding
|
140 |
+
try:
|
141 |
+
health_check = requests.get(f"{MCP_SERVER_URL}/", timeout=5)
|
142 |
+
if health_check.status_code != 200:
|
143 |
+
return f"β MCP Server not responding properly (status: {health_check.status_code})", "", ""
|
144 |
+
except:
|
145 |
+
return "β MCP Server is not running. Please check server status above.", "", ""
|
146 |
+
|
147 |
+
# Try the workflow endpoint
|
148 |
response = requests.post(
|
149 |
f"{MCP_SERVER_URL}/api/run-workflow",
|
150 |
json=payload,
|
151 |
+
timeout=60
|
152 |
)
|
153 |
|
154 |
if response.status_code == 200:
|
|
|
159 |
csv_content = result.get('csv', '')
|
160 |
|
161 |
return workflow_output, alert_summary, csv_content
|
162 |
+
elif response.status_code == 404:
|
163 |
+
return "β Workflow endpoint not found. The server may not be fully started or may be missing the workflow functionality.", "", ""
|
164 |
else:
|
165 |
+
error_msg = f"β Server Error ({response.status_code}): {response.text[:500]}"
|
166 |
return error_msg, "", ""
|
167 |
|
168 |
except requests.exceptions.Timeout:
|
169 |
+
return "β° Request timed out. The workflow is taking longer than expected...", "", ""
|
170 |
except requests.exceptions.ConnectionError:
|
171 |
+
return f"π Connection Error: Cannot reach MCP server at {MCP_SERVER_URL}", "", ""
|
172 |
except Exception as e:
|
173 |
return f"β Error: {str(e)}", "", ""
|
174 |
|
175 |
def check_server_health():
|
176 |
"""Check if the MCP server is running"""
|
177 |
try:
|
178 |
+
# Try the health endpoint first
|
179 |
response = requests.get(f"{MCP_SERVER_URL}/api/health", timeout=10)
|
180 |
if response.status_code == 200:
|
181 |
data = response.json()
|
182 |
return f"β
Server Online | OpenAI: {'β
' if data.get('openai_available') else 'β'} | Time: {data.get('timestamp', 'N/A')}"
|
183 |
+
elif response.status_code == 404:
|
184 |
+
# Try the root endpoint as fallback
|
185 |
+
try:
|
186 |
+
root_response = requests.get(f"{MCP_SERVER_URL}/", timeout=5)
|
187 |
+
if root_response.status_code == 200:
|
188 |
+
root_data = root_response.json()
|
189 |
+
if "MCP Weather Server" in str(root_data):
|
190 |
+
return f"β οΈ Server Running (health endpoint missing) | Status: {root_data.get('status', 'unknown')}"
|
191 |
+
return f"β οΈ Server responded with status {response.status_code} (health endpoint not found)"
|
192 |
+
except:
|
193 |
+
return f"β οΈ Server responded with status {response.status_code} (health endpoint not found)"
|
194 |
else:
|
195 |
return f"β οΈ Server responded with status {response.status_code}"
|
196 |
+
except requests.exceptions.ConnectionError:
|
197 |
+
return f"β Cannot connect to server at {MCP_SERVER_URL}"
|
198 |
+
except requests.exceptions.Timeout:
|
199 |
+
return f"β° Server connection timeout"
|
200 |
except Exception as e:
|
201 |
+
return f"β Server check failed: {str(e)}"
|
202 |
|
203 |
# Start server in background thread
|
204 |
print("π§ Initializing BIHAR AgMCP...")
|
|
|
246 |
|
247 |
# Server status
|
248 |
with gr.Row():
|
249 |
+
with gr.Column(scale=3):
|
250 |
+
server_status = gr.Textbox(
|
251 |
+
label="π§ Server Status",
|
252 |
+
value="π Starting server...",
|
253 |
+
interactive=False,
|
254 |
+
container=True
|
255 |
+
)
|
256 |
+
with gr.Column(scale=1):
|
257 |
+
refresh_btn = gr.Button("π Check Status", size="sm")
|
258 |
+
debug_btn = gr.Button("π Debug Info", size="sm", variant="secondary")
|
259 |
|
260 |
# Main interface
|
261 |
with gr.Row():
|
|
|
263 |
district_input = gr.Dropdown(
|
264 |
choices=BIHAR_DISTRICTS,
|
265 |
label="π Select Bihar District",
|
266 |
+
value="Patna",
|
267 |
+
info="Choose a district to generate weather alerts"
|
268 |
)
|
269 |
|
270 |
run_btn = gr.Button(
|
|
|
317 |
|
318 |
# Connect events
|
319 |
refresh_btn.click(check_server_health, outputs=server_status)
|
320 |
+
|
321 |
+
def debug_server():
|
322 |
+
"""Debug server endpoints"""
|
323 |
+
try:
|
324 |
+
debug_info = []
|
325 |
+
|
326 |
+
# Test root endpoint
|
327 |
+
try:
|
328 |
+
root_resp = requests.get(f"{MCP_SERVER_URL}/", timeout=5)
|
329 |
+
debug_info.append(f"β
Root endpoint: {root_resp.status_code} - {root_resp.text[:100]}")
|
330 |
+
except Exception as e:
|
331 |
+
debug_info.append(f"β Root endpoint failed: {str(e)}")
|
332 |
+
|
333 |
+
# Test health endpoint
|
334 |
+
try:
|
335 |
+
health_resp = requests.get(f"{MCP_SERVER_URL}/api/health", timeout=5)
|
336 |
+
debug_info.append(f"β
Health endpoint: {health_resp.status_code} - {health_resp.text[:100]}")
|
337 |
+
except Exception as e:
|
338 |
+
debug_info.append(f"β Health endpoint failed: {str(e)}")
|
339 |
+
|
340 |
+
# List what we're trying to connect to
|
341 |
+
debug_info.append(f"π Trying to connect to: {MCP_SERVER_URL}")
|
342 |
+
debug_info.append(f"π³ Container environment: {os.getenv('SPACE_ID', 'Not in HF Spaces')}")
|
343 |
+
|
344 |
+
return "π **Debug Information:**\n\n" + "\n".join(debug_info)
|
345 |
+
|
346 |
+
except Exception as e:
|
347 |
+
return f"β Debug failed: {str(e)}"
|
348 |
+
|
349 |
+
debug_btn.click(debug_server, outputs=workflow_output)
|
350 |
run_btn.click(
|
351 |
run_workflow_with_csv,
|
352 |
inputs=[district_input],
|