Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import json | |
| def calculate_cloud_vs_device(daily_queries: int, cloud_cost_per_1k: float, days: int = 365) -> str: | |
| """Compare cloud API costs vs on-device inference costs. | |
| Use this tool when a user asks about the cost of running AI inference, | |
| or whether to use cloud vs on-device models. Shows annual savings from | |
| switching to on-device inference. | |
| Args: | |
| daily_queries: Number of AI queries per day | |
| cloud_cost_per_1k: Cost in USD per 1000 queries on cloud API (e.g., 0.50 for GPT-4o-mini) | |
| days: Number of days to calculate over (default 365) | |
| Returns: | |
| JSON string with cost comparison and savings | |
| """ | |
| cloud_daily = (daily_queries / 1000) * cloud_cost_per_1k | |
| cloud_total = cloud_daily * days | |
| onetime_download = 0.5 # 500MB at $1/GB | |
| device_total = onetime_download | |
| savings = cloud_total - device_total | |
| savings_pct = (savings / cloud_total * 100) if cloud_total > 0 else 0 | |
| return json.dumps({ | |
| "cloud_api": { | |
| "cost_per_1k_queries": cloud_cost_per_1k, | |
| "daily_cost": round(cloud_daily, 2), | |
| "monthly_cost": round(cloud_daily * 30, 2), | |
| "total_cost": round(cloud_total, 2), | |
| }, | |
| "on_device_dispatchai": { | |
| "cost_per_1k_queries": 0.0, | |
| "daily_cost": 0.0, | |
| "monthly_cost": 0.0, | |
| "total_cost": round(device_total, 2), | |
| "one_time_setup": round(onetime_download, 2), | |
| }, | |
| "savings": { | |
| "total_savings": round(savings, 2), | |
| "savings_percentage": round(savings_pct, 1), | |
| "5_year_savings": round(savings * 5, 2), | |
| "10_year_savings": round(savings * 10, 2), | |
| }, | |
| "benefits_of_on_device": [ | |
| "Zero per-query cost", | |
| "Works offline", | |
| "Zero latency (no network round-trip)", | |
| "Full privacy (data never leaves device)", | |
| "No rate limits", | |
| "No API bills" | |
| ], | |
| "recommended_models": { | |
| "budget": "dispatchAI/SmolLM2-135M-Instruct-mobile (270MB)", | |
| "balanced": "dispatchAI/Llama-3.2-1B-Instruct-Q4-mobile (700MB)", | |
| "quality": "dispatchAI/Llama-3.2-3B-Instruct-Q5-mobile (2.1GB)", | |
| } | |
| }, indent=2) | |
| with gr.Blocks(title="dispatchAI Cost Calculator MCP") as demo: | |
| gr.Markdown("## 💰 dispatchAI Cost Calculator (MCP Tool)") | |
| with gr.Row(): | |
| dq = gr.Slider(100, 100000, value=10000, step=100, label="Daily Queries") | |
| cc = gr.Slider(0.1, 10.0, value=0.5, step=0.1, label="Cloud Cost ($/1K queries)") | |
| dy = gr.Slider(30, 3650, value=365, step=30, label="Days") | |
| btn = gr.Button("Calculate", variant="primary") | |
| out = gr.Textbox(label="Cost Analysis (JSON)", lines=20) | |
| btn.click(fn=calculate_cloud_vs_device, inputs=[dq, cc, dy], outputs=out) | |
| demo.launch(mcp_server=True) | |