Spaces:
Running
Running
Commit
·
5c7c72c
1
Parent(s):
ccceff6
fix: Improve cost estimation MCP fallback error handling
Browse files- Add proper error handling for MCP cost estimator failures
- Return structured error response instead of None
- Display user-friendly message when MCP is unavailable
- Add debug logging to track MCP response format
- Allow users to proceed with evaluation even if cost estimation fails
This fixes the issue where models not in the leaderboard would show
a generic error instead of attempting MCP fallback and handling failures gracefully.
app.py
CHANGED
|
@@ -2429,28 +2429,54 @@ with gr.Blocks(title="TraceMind-AI", theme=theme) as app:
|
|
| 2429 |
else:
|
| 2430 |
# No historical data - use MCP tool
|
| 2431 |
print(f"[INFO] No historical data for {model}, using MCP cost estimator")
|
| 2432 |
-
|
| 2433 |
-
|
| 2434 |
-
|
| 2435 |
-
|
| 2436 |
-
|
| 2437 |
-
|
| 2438 |
-
|
| 2439 |
-
|
| 2440 |
-
|
| 2441 |
-
|
|
|
|
| 2442 |
|
| 2443 |
-
|
| 2444 |
-
|
| 2445 |
-
|
| 2446 |
-
|
| 2447 |
-
|
| 2448 |
-
|
| 2449 |
-
|
| 2450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2451 |
|
| 2452 |
except Exception as e:
|
| 2453 |
-
print(f"[ERROR] Cost estimation failed: {e}")
|
| 2454 |
return None
|
| 2455 |
|
| 2456 |
def on_hardware_change(model, hardware):
|
|
@@ -2471,6 +2497,23 @@ with gr.Blocks(title="TraceMind-AI", theme=theme) as app:
|
|
| 2471 |
"""
|
| 2472 |
return info_html
|
| 2473 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2474 |
# Format based on source
|
| 2475 |
if cost_est['source'] == 'historical':
|
| 2476 |
source_label = f"📊 Historical Data ({cost_est['historical_runs']} past runs)"
|
|
|
|
| 2429 |
else:
|
| 2430 |
# No historical data - use MCP tool
|
| 2431 |
print(f"[INFO] No historical data for {model}, using MCP cost estimator")
|
| 2432 |
+
try:
|
| 2433 |
+
from gradio_client import Client
|
| 2434 |
+
|
| 2435 |
+
mcp_client = Client("https://mcp-1st-birthday-tracemind-mcp-server.hf.space/")
|
| 2436 |
+
result = mcp_client.predict(
|
| 2437 |
+
model=model,
|
| 2438 |
+
agent_type="both",
|
| 2439 |
+
num_tests=100,
|
| 2440 |
+
hardware=hardware,
|
| 2441 |
+
api_name="/run_estimate_cost"
|
| 2442 |
+
)
|
| 2443 |
|
| 2444 |
+
# Parse MCP result
|
| 2445 |
+
print(f"[INFO] MCP result: {result}")
|
| 2446 |
+
|
| 2447 |
+
# Handle case where MCP returns error or None
|
| 2448 |
+
if result is None or isinstance(result, str):
|
| 2449 |
+
print(f"[WARNING] MCP returned unexpected result: {result}")
|
| 2450 |
+
return {
|
| 2451 |
+
'source': 'mcp',
|
| 2452 |
+
'total_cost_usd': 'N/A',
|
| 2453 |
+
'estimated_duration_minutes': 'N/A',
|
| 2454 |
+
'historical_runs': 0,
|
| 2455 |
+
'has_cost_data': False,
|
| 2456 |
+
'error': 'MCP server returned invalid response'
|
| 2457 |
+
}
|
| 2458 |
+
|
| 2459 |
+
return {
|
| 2460 |
+
'source': 'mcp',
|
| 2461 |
+
'total_cost_usd': result.get('estimated_cost', 'N/A'),
|
| 2462 |
+
'estimated_duration_minutes': result.get('estimated_duration', 'N/A'),
|
| 2463 |
+
'historical_runs': 0,
|
| 2464 |
+
'has_cost_data': True
|
| 2465 |
+
}
|
| 2466 |
+
except Exception as mcp_error:
|
| 2467 |
+
print(f"[ERROR] MCP cost estimation failed: {mcp_error}")
|
| 2468 |
+
# Return a result indicating MCP is unavailable
|
| 2469 |
+
return {
|
| 2470 |
+
'source': 'mcp',
|
| 2471 |
+
'total_cost_usd': 'N/A',
|
| 2472 |
+
'estimated_duration_minutes': 'N/A',
|
| 2473 |
+
'historical_runs': 0,
|
| 2474 |
+
'has_cost_data': False,
|
| 2475 |
+
'error': str(mcp_error)
|
| 2476 |
+
}
|
| 2477 |
|
| 2478 |
except Exception as e:
|
| 2479 |
+
print(f"[ERROR] Cost estimation failed (leaderboard load): {e}")
|
| 2480 |
return None
|
| 2481 |
|
| 2482 |
def on_hardware_change(model, hardware):
|
|
|
|
| 2497 |
"""
|
| 2498 |
return info_html
|
| 2499 |
|
| 2500 |
+
# Check if MCP returned an error
|
| 2501 |
+
if cost_est.get('error'):
|
| 2502 |
+
info_html = f"""
|
| 2503 |
+
<div style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 2504 |
+
padding: 20px; border-radius: 10px; color: white; margin: 10px 0;">
|
| 2505 |
+
<h3 style="margin-top: 0;">⚠️ MCP Cost Estimator Unavailable</h3>
|
| 2506 |
+
<div style="font-size: 1em; margin-top: 15px; line-height: 1.6;">
|
| 2507 |
+
<p>No historical data available for <strong>{model}</strong>.</p>
|
| 2508 |
+
<p style="margin-top: 10px;">MCP cost estimator failed: {cost_est.get('error', 'Unknown error')}</p>
|
| 2509 |
+
<p style="margin-top: 10px; font-size: 0.9em; opacity: 0.9;">
|
| 2510 |
+
💡 You can still proceed with the evaluation. Actual costs will be tracked and displayed after completion.
|
| 2511 |
+
</p>
|
| 2512 |
+
</div>
|
| 2513 |
+
</div>
|
| 2514 |
+
"""
|
| 2515 |
+
return info_html
|
| 2516 |
+
|
| 2517 |
# Format based on source
|
| 2518 |
if cost_est['source'] == 'historical':
|
| 2519 |
source_label = f"📊 Historical Data ({cost_est['historical_runs']} past runs)"
|