3morixd commited on
Commit
7e798e3
·
verified ·
1 Parent(s): 951b0c1

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +349 -0
app.py ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Dispatch AI — Model Comparison Visualizer
3
+ Pick 2 models → side-by-side comparison of size, speed, quality, RAM.
4
+ Visual charts using matplotlib.
5
+ """
6
+
7
+ import gradio as gr
8
+ import matplotlib
9
+ matplotlib.use("Agg")
10
+ import matplotlib.pyplot as plt
11
+ import numpy as np
12
+
13
+ # ---------------------------------------------------------------------------
14
+ # Model database — from our phone farm benchmarks + public info
15
+ # ---------------------------------------------------------------------------
16
+ MODELS = {
17
+ "Qwen2.5-0.5B-Instruct": {
18
+ "params_b": 0.5, "size_mb": 450, "gen_tps": 19.2, "prompt_tps": 65.3,
19
+ "ram_mb": 4100, "load_s": 0.9, "quality_score": 5.2, "license": "Apache 2.0",
20
+ "context": 32768, "arabic": "Good",
21
+ },
22
+ "Qwen2.5-1.5B-Instruct": {
23
+ "params_b": 1.5, "size_mb": 1060, "gen_tps": 16.9, "prompt_tps": 57.8,
24
+ "ram_mb": 3500, "load_s": 1.8, "quality_score": 6.5, "license": "Apache 2.0",
25
+ "context": 32768, "arabic": "Very Good",
26
+ },
27
+ "Llama-3.2-1B-Instruct": {
28
+ "params_b": 1.0, "size_mb": 890, "gen_tps": 16.3, "prompt_tps": 57.8,
29
+ "ram_mb": 3500, "load_s": 1.5, "quality_score": 6.0, "license": "Llama 3.2",
30
+ "context": 131072, "arabic": "Fair",
31
+ },
32
+ "Llama-3.2-3B-Instruct": {
33
+ "params_b": 3.0, "size_mb": 2100, "gen_tps": 12.4, "prompt_tps": 45.2,
34
+ "ram_mb": 2800, "load_s": 3.2, "quality_score": 7.2, "license": "Llama 3.2",
35
+ "context": 131072, "arabic": "Good",
36
+ },
37
+ "Gemma-2-2B-IT": {
38
+ "params_b": 2.0, "size_mb": 1600, "gen_tps": 13.8, "prompt_tps": 48.6,
39
+ "ram_mb": 3200, "load_s": 2.5, "quality_score": 6.8, "license": "Gemma",
40
+ "context": 8192, "arabic": "Fair",
41
+ },
42
+ "Phi-3.5-mini": {
43
+ "params_b": 3.8, "size_mb": 2300, "gen_tps": 14.2, "prompt_tps": 50.1,
44
+ "ram_mb": 2900, "load_s": 2.8, "quality_score": 7.5, "license": "MIT",
45
+ "context": 131072, "arabic": "Fair",
46
+ },
47
+ "SmolLM2-1.7B": {
48
+ "params_b": 1.7, "size_mb": 1200, "gen_tps": 17.1, "prompt_tps": 60.2,
49
+ "ram_mb": 3400, "load_s": 1.4, "quality_score": 5.8, "license": "Apache 2.0",
50
+ "context": 8192, "arabic": "Poor",
51
+ },
52
+ "SmolLM2-135M": {
53
+ "params_b": 0.135, "size_mb": 85, "gen_tps": 22.8, "prompt_tps": 89.5,
54
+ "ram_mb": 4500, "load_s": 0.3, "quality_score": 3.0, "license": "Apache 2.0",
55
+ "context": 8192, "arabic": "Poor",
56
+ },
57
+ "TinyLlama-1.1B": {
58
+ "params_b": 1.1, "size_mb": 700, "gen_tps": 18.5, "prompt_tps": 62.4,
59
+ "ram_mb": 3800, "load_s": 1.1, "quality_score": 4.5, "license": "Apache 2.0",
60
+ "context": 2048, "arabic": "Poor",
61
+ },
62
+ }
63
+
64
+ # Dark theme colors for matplotlib
65
+ BG = "#0A0F1A"
66
+ CARD = "#0E1424"
67
+ ACCENT = "#1FE0E6"
68
+ ACCENT2 = "#FF6B9D"
69
+ WHITE = "#FFFFFF"
70
+ GRAY = "#8A8F9C"
71
+
72
+
73
+ def create_comparison_chart(model1_name, model2_name):
74
+ """Create a grouped bar chart comparing two models across key metrics."""
75
+ if model1_name not in MODELS or model2_name not in MODELS:
76
+ fig, ax = plt.subplots(figsize=(10, 6))
77
+ ax.text(0.5, 0.5, "Select two models", ha="center", va="center", color=ACCENT, fontsize=16)
78
+ ax.set_facecolor(BG)
79
+ fig.patch.set_facecolor(BG)
80
+ plt.close(fig)
81
+ return fig
82
+
83
+ m1 = MODELS[model1_name]
84
+ m2 = MODELS[model2_name]
85
+
86
+ # Normalized metrics (0-10 scale for comparison)
87
+ metrics = ["Size\n(smaller=better)", "Gen Speed\n(faster=better)", "Prompt Speed\n(faster=better)",
88
+ "RAM Free\n(more=better)", "Load Time\n(faster=better)", "Quality\n(higher=better)"]
89
+
90
+ # Normalize: higher is better for speed, ram, quality; lower is better for size, load time
91
+ max_size = max(m["size_mb"] for m in MODELS.values())
92
+ max_load = max(m["load_s"] for m in MODELS.values())
93
+
94
+ m1_vals = [
95
+ 10 * (1 - m1["size_mb"] / max_size), # smaller = higher score
96
+ m1["gen_tps"] / 25 * 10,
97
+ m1["prompt_tps"] / 100 * 10,
98
+ m1["ram_mb"] / 5000 * 10,
99
+ 10 * (1 - m1["load_s"] / max_load),
100
+ m1["quality_score"],
101
+ ]
102
+ m2_vals = [
103
+ 10 * (1 - m2["size_mb"] / max_size),
104
+ m2["gen_tps"] / 25 * 10,
105
+ m2["prompt_tps"] / 100 * 10,
106
+ m2["ram_mb"] / 5000 * 10,
107
+ 10 * (1 - m2["load_s"] / max_load),
108
+ m2["quality_score"],
109
+ ]
110
+
111
+ x = np.arange(len(metrics))
112
+ width = 0.35
113
+
114
+ fig, ax = plt.subplots(figsize=(12, 6))
115
+ fig.patch.set_facecolor(BG)
116
+ ax.set_facecolor(CARD)
117
+
118
+ bars1 = ax.bar(x - width/2, m1_vals, width, label=model1_name, color=ACCENT, edgecolor=WHITE, linewidth=0.5)
119
+ bars2 = ax.bar(x + width/2, m2_vals, width, label=model2_name, color=ACCENT2, edgecolor=WHITE, linewidth=0.5)
120
+
121
+ ax.set_ylabel("Score (0-10, higher = better)", color=WHITE, fontsize=12)
122
+ ax.set_title(f"Model Comparison: {model1_name} vs {model2_name}", color=WHITE, fontsize=14, pad=15)
123
+ ax.set_xticks(x)
124
+ ax.set_xticklabels(metrics, color=WHITE, fontsize=9)
125
+ ax.set_ylim(0, 12)
126
+ ax.tick_params(axis="y", colors=GRAY)
127
+ ax.spines["bottom"].set_color(GRAY)
128
+ ax.spines["left"].set_color(GRAY)
129
+ ax.spines["top"].set_visible(False)
130
+ ax.spines["right"].set_visible(False)
131
+ ax.grid(axis="y", color=GRAY, alpha=0.2, linestyle="--")
132
+
133
+ legend = ax.legend(facecolor=CARD, edgecolor=ACCENT, labelcolor=WHITE, fontsize=10)
134
+ legend.get_frame().set_alpha(0.9)
135
+
136
+ # Add value labels
137
+ for bar in bars1 + bars2:
138
+ height = bar.get_height()
139
+ ax.annotate(f"{height:.1f}",
140
+ xy=(bar.get_x() + bar.get_width() / 2, height),
141
+ xytext=(0, 3), textcoords="offset points",
142
+ ha="center", va="bottom", color=WHITE, fontsize=8)
143
+
144
+ plt.tight_layout()
145
+ plt.close(fig)
146
+ return fig
147
+
148
+
149
+ def create_radar_chart(model1_name, model2_name):
150
+ """Create a radar/spider chart comparing two models."""
151
+ if model1_name not in MODELS or model2_name not in MODELS:
152
+ fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection="polar"))
153
+ ax.set_facecolor(BG)
154
+ fig.patch.set_facecolor(BG)
155
+ plt.close(fig)
156
+ return fig
157
+
158
+ m1 = MODELS[model1_name]
159
+ m2 = MODELS[model2_name]
160
+
161
+ categories = ["Compact", "Speed", "RAM\nEfficient", "Fast\nLoad", "Quality", "Arabic\nSupport"]
162
+ N = len(categories)
163
+
164
+ max_size = max(m["size_mb"] for m in MODELS.values())
165
+ max_load = max(m["load_s"] for m in MODELS.values())
166
+ arabic_scores = {"Poor": 2, "Fair": 5, "Good": 7, "Very Good": 9}
167
+
168
+ m1_vals = [
169
+ 1 - m1["size_mb"] / max_size,
170
+ m1["gen_tps"] / 25,
171
+ m1["ram_mb"] / 5000,
172
+ 1 - m1["load_s"] / max_load,
173
+ m1["quality_score"] / 10,
174
+ arabic_scores.get(m1["arabic"], 5) / 10,
175
+ ]
176
+ m2_vals = [
177
+ 1 - m2["size_mb"] / max_size,
178
+ m2["gen_tps"] / 25,
179
+ m2["ram_mb"] / 5000,
180
+ 1 - m2["load_s"] / max_load,
181
+ m2["quality_score"] / 10,
182
+ arabic_scores.get(m2["arabic"], 5) / 10,
183
+ ]
184
+
185
+ angles = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist()
186
+ m1_vals += m1_vals[:1]
187
+ m2_vals += m2_vals[:1]
188
+ angles += angles[:1]
189
+
190
+ fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection="polar"))
191
+ fig.patch.set_facecolor(BG)
192
+ ax.set_facecolor(CARD)
193
+
194
+ ax.plot(angles, m1_vals, "o-", color=ACCENT, linewidth=2, label=model1_name)
195
+ ax.fill(angles, m1_vals, color=ACCENT, alpha=0.15)
196
+ ax.plot(angles, m2_vals, "o-", color=ACCENT2, linewidth=2, label=model2_name)
197
+ ax.fill(angles, m2_vals, color=ACCENT2, alpha=0.15)
198
+
199
+ ax.set_xticks(angles[:-1])
200
+ ax.set_xticklabels(categories, color=WHITE, fontsize=10)
201
+ ax.set_ylim(0, 1)
202
+ ax.set_yticks([0.2, 0.4, 0.6, 0.8, 1.0])
203
+ ax.set_yticklabels(["0.2", "0.4", "0.6", "0.8", "1.0"], color=GRAY, fontsize=8)
204
+ ax.grid(color=GRAY, alpha=0.3)
205
+ ax.spines["polar"].set_color(GRAY)
206
+
207
+ ax.set_title("Model Capability Radar", color=WHITE, fontsize=14, pad=20)
208
+ legend = ax.legend(loc="upper right", bbox_to_anchor=(1.3, 1.1),
209
+ facecolor=CARD, edgecolor=ACCENT, labelcolor=WHITE, fontsize=10)
210
+ legend.get_frame().set_alpha(0.9)
211
+
212
+ plt.tight_layout()
213
+ plt.close(fig)
214
+ return fig
215
+
216
+
217
+ def get_comparison_table(model1_name, model2_name):
218
+ """Return a text comparison table."""
219
+ if model1_name not in MODELS or model2_name not in MODELS:
220
+ return "Please select two models."
221
+
222
+ m1 = MODELS[model1_name]
223
+ m2 = MODELS[model2_name]
224
+
225
+ rows = [
226
+ ("Parameters (B)", f"{m1['params_b']}", f"{m2['params_b']}"),
227
+ ("Model Size (MB)", f"{m1['size_mb']}", f"{m2['size_mb']}"),
228
+ ("Gen Speed (t/s)", f"{m1['gen_tps']}", f"{m2['gen_tps']}"),
229
+ ("Prompt Speed (t/s)", f"{m1['prompt_tps']}", f"{m2['prompt_tps']}"),
230
+ ("RAM Free (MB)", f"{m1['ram_mb']}", f"{m2['ram_mb']}"),
231
+ ("Load Time (s)", f"{m1['load_s']}", f"{m2['load_s']}"),
232
+ ("Quality Score", f"{m1['quality_score']}/10", f"{m2['quality_score']}/10"),
233
+ ("Context Length", f"{m1['context']:,}", f"{m2['context']:,}"),
234
+ ("Arabic Support", m1["arabic"], m2["arabic"]),
235
+ ("License", m1["license"], m2["license"]),
236
+ ]
237
+
238
+ # Build winner indicators
239
+ result = f"### Side-by-Side Comparison\n\n"
240
+ result += f"| Metric | {model1_name} | {model2_name} | Winner |\n"
241
+ result += f"|--------|-------------|-------------|--------|\n"
242
+
243
+ # Define which is better (higher/lower)
244
+ higher_better = {"Gen Speed (t/s)", "Prompt Speed (t/s)", "RAM Free (MB)", "Quality Score", "Context Length"}
245
+ lower_better = {"Model Size (MB)", "Load Time (s)"}
246
+
247
+ for metric, v1, v2 in rows:
248
+ winner = ""
249
+ if metric in higher_better or metric in lower_better:
250
+ try:
251
+ f1 = float(v1.split("/")[0].replace(",", ""))
252
+ f2 = float(v2.split("/")[0].replace(",", ""))
253
+ if metric in higher_better:
254
+ winner = model1_name if f1 > f2 else (model2_name if f2 > f1 else "tie")
255
+ else:
256
+ winner = model1_name if f1 < f2 else (model2_name if f2 < f1 else "tie")
257
+ winner = "🟢" if winner == model1_name else ("🔵" if winner == model2_name else "➖")
258
+ except ValueError:
259
+ pass
260
+ result += f"| {metric} | {v1} | {v2} | {winner} |\n"
261
+
262
+ return result
263
+
264
+
265
+ # --- UI -----------------------------------------------------------------------
266
+ CSS = """
267
+ #dispatch-header h1 {
268
+ color: #FFFFFF; font-size: 2.2rem; margin: 0;
269
+ background: linear-gradient(90deg, #1FE0E6 0%, #FFFFFF 60%);
270
+ -webkit-background-clip: text; -webkit-text-fill-color: transparent;
271
+ }
272
+ #dispatch-header p { color: #1FE0E6; font-size: 1.05rem; margin: 6px 0 0 0; }
273
+ .dispatch-footer { text-align: center; color: #8A8F9C; font-size: 0.9rem; padding-top: 8px; }
274
+ """
275
+
276
+ with gr.Blocks(
277
+ title="Dispatch AI — Model Comparison Visualizer",
278
+ theme=gr.themes.Base(
279
+ primary_hue="cyan", secondary_hue="cyan", neutral_hue="slate",
280
+ font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
281
+ ).set(
282
+ body_background_fill="#0A0F1A", body_background_fill_dark="#0A0F1A",
283
+ body_text_color="#FFFFFF", body_text_color_dark="#FFFFFF",
284
+ block_background_fill="#0E1424", block_background_fill_dark="#0E1424",
285
+ block_border_color="#1FE0E6", block_border_width="1px",
286
+ block_label_text_color="#1FE0E6", block_title_text_color="#1FE0E6",
287
+ button_primary_background_fill="#1FE0E6", button_primary_background_fill_dark="#1FE0E6",
288
+ button_primary_text_color="#0A0F1A", button_primary_border_color="#1FE0E6",
289
+ input_background_fill="#0E1424", input_background_fill_dark="#0E1424",
290
+ input_border_color="#1FE0E6", input_border_width="1px",
291
+ ),
292
+ css=CSS,
293
+ ) as demo:
294
+ with gr.Column(elem_id="dispatch-header"):
295
+ gr.Markdown(
296
+ """
297
+ # Dispatch AI — Model Comparison Visualizer
298
+ Compare mobile AI models side-by-side with visual charts · Dispatch AI (FZE) · UAE
299
+ """
300
+ )
301
+
302
+ gr.Markdown(
303
+ """
304
+ Pick two models to compare size, speed, quality, RAM, and more. Data from our 80-phone farm.
305
+ 🟢 = Model 1 wins · 🔵 = Model 2 wins · ➖ = tie
306
+ """
307
+ )
308
+
309
+ with gr.Row():
310
+ model1 = gr.Dropdown(list(MODELS.keys()), label="Model 1 (🟢)", value="Qwen2.5-1.5B-Instruct")
311
+ model2 = gr.Dropdown(list(MODELS.keys()), label="Model 2 (🔵)", value="Llama-3.2-3B-Instruct")
312
+ compare_btn = gr.Button("⚔️ Compare Models", variant="primary")
313
+
314
+ with gr.Row():
315
+ bar_chart = gr.Plot(label="Bar Chart Comparison")
316
+ radar_chart = gr.Plot(label="Radar Chart Comparison")
317
+
318
+ comparison_table = gr.Markdown()
319
+
320
+ # Events
321
+ compare_btn.click(
322
+ fn=lambda m1, m2: (create_comparison_chart(m1, m2), create_radar_chart(m1, m2), get_comparison_table(m1, m2)),
323
+ inputs=[model1, model2],
324
+ outputs=[bar_chart, radar_chart, comparison_table],
325
+ )
326
+ # Also update on dropdown change
327
+ model1.change(
328
+ fn=lambda m1, m2: (create_comparison_chart(m1, m2), create_radar_chart(m1, m2), get_comparison_table(m1, m2)),
329
+ inputs=[model1, model2],
330
+ outputs=[bar_chart, radar_chart, comparison_table],
331
+ )
332
+ model2.change(
333
+ fn=lambda m1, m2: (create_comparison_chart(m1, m2), create_radar_chart(m1, m2), get_comparison_table(m1, m2)),
334
+ inputs=[model1, model2],
335
+ outputs=[bar_chart, radar_chart, comparison_table],
336
+ )
337
+
338
+ gr.Markdown(
339
+ """
340
+ <div class="dispatch-footer">
341
+ © 2026 Dispatch AI (FZE) · Sharjah, UAE · License 10818 ·
342
+ Benchmarks from 80-device phone farm · Q4_K_M quants · llama.cpp
343
+ </div>
344
+ """
345
+ )
346
+
347
+ if __name__ == "__main__":
348
+ demo.queue()
349
+ demo.launch()