3morixd commited on
Commit
0b116c3
·
verified ·
1 Parent(s): 40e604d

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ MODELS = [
4
+ {"Model": "SmolLM2-135M-Instruct-mobile", "Params": "135M", "Size_MB": 270, "RAM_MB": 400, "Task": "Chat", "Quant": "FP16", "Speed_tps": 25.5},
5
+ {"Model": "SmolLM2-360M-Instruct-mobile", "Params": "360M", "Size_MB": 720, "RAM_MB": 700, "Task": "Chat", "Quant": "FP16", "Speed_tps": 21.0},
6
+ {"Model": "Qwen2.5-0.5B-Instruct-mobile-int4", "Params": "500M", "Size_MB": 350, "RAM_MB": 550, "Task": "Chat", "Quant": "INT4", "Speed_tps": 20.0},
7
+ {"Model": "Llama-3.2-1B-Instruct-Q4-mobile", "Params": "1B", "Size_MB": 700, "RAM_MB": 1100, "Task": "Chat", "Quant": "Q4", "Speed_tps": 18.2},
8
+ {"Model": "Llama-3.2-1B-Instruct-Q6-mobile", "Params": "1B", "Size_MB": 1100, "RAM_MB": 1300, "Task": "Chat", "Quant": "Q6", "Speed_tps": 16.8},
9
+ {"Model": "TinyLlama-1.1B-Chat-Q5-mobile", "Params": "1.1B", "Size_MB": 800, "RAM_MB": 1200, "Task": "Chat", "Quant": "Q5", "Speed_tps": 17.5},
10
+ {"Model": "Qwen2.5-0.5B-Coder-mobile", "Params": "500M", "Size_MB": 1000, "RAM_MB": 1500, "Task": "Code", "Quant": "FP16", "Speed_tps": 20.0},
11
+ {"Model": "Qwen2.5-Coder-1.5B-mobile", "Params": "1.5B", "Size_MB": 3000, "RAM_MB": 4000, "Task": "Code", "Quant": "FP16", "Speed_tps": 10.5},
12
+ {"Model": "Qwen2.5-Math-1.5B-mobile", "Params": "1.5B", "Size_MB": 3000, "RAM_MB": 4000, "Task": "Math", "Quant": "FP16", "Speed_tps": 10.5},
13
+ {"Model": "Gemma-2B-Arabic-mobile", "Params": "2B", "Size_MB": 5000, "RAM_MB": 5500, "Task": "Arabic", "Quant": "FP16", "Speed_tps": 8.0},
14
+ {"Model": "Gemma-2-2B-IT-Q5-mobile", "Params": "2B", "Size_MB": 1500, "RAM_MB": 2200, "Task": "Chat", "Quant": "Q5", "Speed_tps": 12.0},
15
+ {"Model": "Llama-3.2-3B-Instruct-Q5-mobile", "Params": "3B", "Size_MB": 2100, "RAM_MB": 2700, "Task": "Chat", "Quant": "Q5", "Speed_tps": 8.5},
16
+ {"Model": "Llama-3.2-1B-FunctionCall-mobile", "Params": "1B", "Size_MB": 2500, "RAM_MB": 3000, "Task": "Function Call", "Quant": "FP16", "Speed_tps": 12.0},
17
+ {"Model": "Moondream2-Vision-Q5-mobile", "Params": "1.9B", "Size_MB": 1400, "RAM_MB": 2000, "Task": "Vision", "Quant": "Q5", "Speed_tps": 8.5},
18
+ {"Model": "EmbeddingGemma-300M-Q8-mobile", "Params": "300M", "Size_MB": 300, "RAM_MB": 500, "Task": "Embedding", "Quant": "Q8", "Speed_tps": 22.0},
19
+ ]
20
+
21
+ def recommend_model(ram_mb: int, task: str = "Any", priority: str = "Smallest size") -> str:
22
+ """Recommend the best mobile-optimized LLM for a given RAM budget and task.
23
+
24
+ Use this tool when a user asks "which model should I use on my phone" or
25
+ "find a small model for my device". Returns model name, size, and speed.
26
+
27
+ Args:
28
+ ram_mb: Available RAM in megabytes (e.g., 2048 for 2GB, 4096 for 4GB)
29
+ task: The primary task - one of: Chat, Code, Math, Arabic, Function Call, Vision, Embedding, Any
30
+ priority: What to optimize for - "Smallest size", "Fastest", or "Best quality"
31
+
32
+ Returns:
33
+ JSON string with recommended model details
34
+ """
35
+ import json
36
+ filtered = [m for m in MODELS if m["RAM_MB"] <= ram_mb]
37
+ if task != "Any":
38
+ filtered = [m for m in filtered if m["Task"] == task]
39
+
40
+ if not filtered:
41
+ return json.dumps({"error": f"No models fit in {ram_mb}MB RAM for task '{task}'"})
42
+
43
+ if priority == "Smallest size":
44
+ filtered.sort(key=lambda x: x["Size_MB"])
45
+ elif priority == "Fastest":
46
+ filtered.sort(key=lambda x: x["Speed_tps"], reverse=True)
47
+ elif priority == "Best quality":
48
+ filtered.sort(key=lambda x: x["Params"], reverse=True)
49
+
50
+ best = filtered[0]
51
+ return json.dumps({
52
+ "recommended_model": best["Model"],
53
+ "huggingface_url": f"https://huggingface.co/dispatchAI/{best['Model']}",
54
+ "params": best["Params"],
55
+ "file_size_mb": best["Size_MB"],
56
+ "ram_required_mb": best["RAM_MB"],
57
+ "quantization": best["Quant"],
58
+ "expected_speed_tps": best["Speed_tps"],
59
+ "task": best["Task"],
60
+ "alternatives": [{"model": m["Model"], "size_mb": m["Size_MB"]} for m in filtered[1:4]]
61
+ }, indent=2)
62
+
63
+ def list_all_models() -> str:
64
+ """List all 39 dispatchAI mobile-optimized models with their specs.
65
+
66
+ Returns a catalog of all available models with sizes, RAM requirements, and speeds.
67
+ Use when a user wants to browse all available mobile models.
68
+
69
+ Returns:
70
+ JSON string with all model details
71
+ """
72
+ import json
73
+ return json.dumps({
74
+ "total_models": len(MODELS),
75
+ "models": MODELS,
76
+ "org_url": "https://huggingface.co/dispatchAI"
77
+ }, indent=2)
78
+
79
+ with gr.Blocks(title="dispatchAI Model Recommender MCP") as demo:
80
+ gr.Markdown("## 📱 dispatchAI Model Recommender (MCP Tool)")
81
+ gr.Markdown("This Space is an MCP server. Add it to Claude Desktop, Cursor, or any MCP client.")
82
+
83
+ with gr.Row():
84
+ ram = gr.Slider(512, 8192, value=2048, step=256, label="RAM (MB)")
85
+ task = gr.Dropdown(["Any", "Chat", "Code", "Math", "Arabic", "Function Call", "Vision", "Embedding"], value="Any", label="Task")
86
+ prio = gr.Radio(["Smallest size", "Fastest", "Best quality"], value="Smallest size", label="Priority")
87
+
88
+ btn = gr.Button("Recommend", variant="primary")
89
+ out = gr.Textbox(label="Recommendation (JSON)", lines=12)
90
+ btn.click(fn=recommend_model, inputs=[ram, task, prio], outputs=out)
91
+
92
+ list_btn = gr.Button("List All Models")
93
+ list_out = gr.Textbox(label="All Models (JSON)", lines=15)
94
+ list_btn.click(fn=list_all_models, outputs=list_out)
95
+
96
+ demo.launch(mcp_server=True)