File size: 16,991 Bytes
daa297c
9c28362
 
 
daa297c
 
 
 
 
 
ca5b23e
9c28362
 
 
 
ca5b23e
 
9c28362
ca5b23e
 
 
9c28362
ca5b23e
 
 
9c28362
 
571b306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6e477e
 
571b306
 
d6e477e
571b306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c28362
 
 
 
 
ca5b23e
9c28362
ca5b23e
9c28362
 
 
 
 
 
 
 
 
 
 
 
 
 
571b306
 
 
 
 
 
 
 
 
 
 
 
9c28362
571b306
9c28362
571b306
9c28362
571b306
9c28362
 
 
 
571b306
9c28362
 
 
571b306
9c28362
 
 
571b306
 
 
9c28362
571b306
 
 
 
 
 
 
 
 
 
 
 
 
9c28362
 
571b306
 
 
 
 
 
 
 
9c28362
571b306
 
 
 
 
 
9c28362
571b306
9c28362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
571b306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c28362
571b306
 
9c28362
571b306
 
9c28362
571b306
 
 
9c28362
 
571b306
9c28362
 
571b306
9c28362
571b306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c28362
 
 
 
 
 
 
571b306
 
 
 
 
 
 
 
 
daa297c
 
 
 
 
 
 
571b306
 
 
daa297c
 
571b306
 
 
 
 
 
 
 
 
4c40941
571b306
 
9c28362
 
571b306
 
4c40941
 
 
9c28362
 
 
 
 
571b306
 
 
9c28362
 
571b306
9c28362
571b306
9c28362
 
 
571b306
9c28362
 
571b306
9c28362
 
 
571b306
9c28362
 
 
 
 
 
571b306
 
9c28362
 
 
571b306
 
9c28362
571b306
9c28362
 
 
 
 
 
571b306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import base64
import gradio as gr
import requests

# Base64 encode logo for inline embedding (Gradio /file= route is unreliable)
# Read SVG and replace black fill with white for dark banner background
with open("logo-full.svg", "r") as _f:
    _svg = _f.read().replace('fill="black"', 'fill="white"')
LOGO_B64 = base64.b64encode(_svg.encode()).decode()

API_URL = "https://api.trustmodel.ai/api/leaderboards/latest/"
LEADERBOARD_URL = "https://trustmodel.ai/leaderboard"
BADGE_BASE_URL = "https://trustmodel.ai/badge/model"

DIMENSIONS = [
    ("bias_safety", "Bias & Safety"),
    ("security_vulnerability", "Security & Vulnerability"),
    ("hallucination", "Hallucination"),
    ("context_awareness", "Context Awareness"),
    ("instruction_adherence", "Instruction Adherence"),
    ("reasoning", "Reasoning"),
    ("accuracy", "Accuracy"),
    ("enterprise_alignment", "Enterprise Alignment"),
    ("specialization", "Specialization"),
    ("cost_latency", "Cost & Latency"),
]

# Generate CSS classes for every possible bar width (0-100) and each color
BAR_COLORS = {
    "green-hi": "#059669",
    "green": "#10b981",
    "yellow": "#f59e0b",
    "red": "#ef4444",
}

bar_width_css = ""
for w in range(101):
    bar_width_css += f".bar-w-{w} {{ width: {w}% !important; }}\n"

bar_color_css = ""
for name, hex_val in BAR_COLORS.items():
    bar_color_css += f".bar-{name} {{ background-color: {hex_val} !important; }}\n"

CUSTOM_CSS = f"""
{bar_width_css}
{bar_color_css}

.header-banner {{
    background: linear-gradient(180deg, hsl(210,55%,12%) 0%, hsl(210,50%,18%) 50%, hsl(210,55%,14%) 100%);
    border-radius: 16px;
    padding: 40px 32px;
    margin-bottom: 8px;
    text-align: center;
    position: relative;
    overflow: hidden;
}}
.header-banner::before {{
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse at center, hsla(190,100%,50%,0.08) 0%, transparent 70%);
    pointer-events: none;
}}
.header-banner h1 {{
    color: #fff !important;
    font-size: 1.8rem !important;
    margin: 0 0 8px 0 !important;
    font-weight: 800 !important;
    position: relative;
}}
.header-banner p {{
    color: rgba(255,255,255,0.6) !important;
    font-size: 0.95rem !important;
    margin: 0 !important;
    position: relative;
}}
.header-banner a {{
    color: hsl(190,100%,60%) !important;
    text-decoration: none !important;
    font-weight: 600 !important;
}}
.header-banner a:hover {{
    text-decoration: underline !important;
}}
.stat-row {{
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 20px;
    flex-wrap: wrap;
    position: relative;
}}
.stat-pill {{
    background: rgba(255,255,255,0.12) !important;
    border: 1px solid rgba(255,255,255,0.25) !important;
    border-radius: 20px;
    padding: 5px 14px;
    color: rgba(255,255,255,0.9) !important;
    font-size: 0.75rem;
    font-weight: 500;
}}
.score-hero {{
    text-align: center;
    padding: 24px;
    border-radius: 12px;
    background: linear-gradient(135deg, #f0fdf4 0%, #ecfeff 100%);
    border: 1px solid #d1fae5;
    margin: 12px 0;
}}
.score-value {{
    font-size: 3rem !important;
    font-weight: 800 !important;
    color: #059669 !important;
    line-height: 1 !important;
}}
.score-label {{
    font-size: 1rem !important;
    color: #6b7280 !important;
    margin-top: 4px !important;
}}
.trust-level {{
    display: inline-block;
    margin-top: 8px;
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 600;
}}
.level-safe {{ background: #d1fae5 !important; color: #065f46 !important; }}
.level-trusted {{ background: #a7f3d0 !important; color: #064e3b !important; }}
.level-caution {{ background: #fef3c7 !important; color: #92400e !important; }}
.level-risk {{ background: #fee2e2 !important; color: #991b1b !important; }}
.dimension-bar {{
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 6px 0;
}}
.dim-label {{
    width: 180px;
    font-size: 0.85rem !important;
    color: #374151 !important;
    font-weight: 500 !important;
    text-align: right;
    flex-shrink: 0;
}}
.dim-track {{
    flex: 1;
    height: 10px;
    background: #e5e7eb !important;
    border-radius: 5px;
    overflow: hidden;
}}
.dim-fill {{
    height: 100%;
    border-radius: 5px;
}}
.dim-score {{
    width: 45px;
    font-size: 0.8rem !important;
    color: #6b7280 !important;
    font-weight: 600 !important;
}}
.badge-card {{
    background: #f8fafc;
    border: 2px dashed #cbd5e1;
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    margin: 12px 0;
}}
.badge-card img {{
    margin: 12px auto;
    display: block;
}}
.badge-label {{
    font-size: 0.85rem !important;
    color: #64748b !important;
    margin-bottom: 8px;
    font-weight: 600 !important;
}}
.badge-hint {{
    font-size: 0.75rem !important;
    color: #94a3b8 !important;
    margin-top: 8px;
}}
.footer-bar {{
    text-align: center;
    padding: 16px;
    margin-top: 8px;
    font-size: 0.8rem;
    color: #9ca3af;
}}
.footer-bar a {{
    color: #6b7280 !important;
    text-decoration: none !important;
    margin: 0 8px;
}}
.footer-bar a:hover {{
    color: #0ea5e9 !important;
}}
.footer-link {{
    font-size: 0.75rem !important;
    color: #94a3b8 !important;
    text-decoration: none !important;
    font-weight: 500 !important;
    transition: color 0.2s ease !important;
}}
.footer-link:hover {{
    color: #0ea5e9 !important;
}}
.lookup-row {{
    display: flex;
    align-items: flex-end !important;
    gap: 12px;
}}
.compare-card {{
    text-align: center;
    padding: 16px 24px;
    border-radius: 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    min-width: 160px;
}}
.compare-score {{
    font-size: 2rem !important;
    font-weight: 800 !important;
    color: #059669 !important;
}}
.compare-name {{
    font-size: 0.8rem !important;
    color: #64748b !important;
    font-weight: 600 !important;
}}
"""


def fetch_leaderboard():
    try:
        response = requests.get(API_URL, timeout=10)
        response.raise_for_status()
        data = response.json()
        return data.get("leaderboard", [])
    except Exception:
        return []


def get_model_names(leaderboard):
    return [entry["model"] for entry in leaderboard]


def format_score(score):
    if score is None:
        return "N/A"
    return f"{score:.1f}"


def get_trust_level(score):
    if score >= 90:
        return "Highly Trusted", "level-trusted"
    elif score >= 75:
        return "Generally Safe", "level-safe"
    elif score >= 60:
        return "Use with Caution", "level-caution"
    else:
        return "High Risk", "level-risk"


def get_bar_color_hex(score):
    if score >= 90:
        return "#22c55e"
    elif score >= 75:
        return "#3b82f6"
    elif score >= 60:
        return "#f97316"
    else:
        return "#ef4444"


def lookup_model(model_name, leaderboard):
    if not model_name:
        return "Please select a model.", "", ""

    entry = next((e for e in leaderboard if e["model"] == model_name), None)
    if not entry:
        return "Model not found.", "", ""

    scores = entry.get("category_scores", {})
    composite = entry.get("composite_score", 0)
    trust_level, level_class = get_trust_level(composite)
    vendor = entry.get("vendor", "Unknown")
    total_tests = entry.get("total_tests", 0)

    slug = entry["model"].lower().replace(" ", "-").replace("(", "").replace(")", "")
    badge_url = f"{BADGE_BASE_URL}/{slug}.svg"
    badge_code = f'[![TrustScore]({badge_url})](https://trustmodel.ai/leaderboard)'

    # Score hero
    result = f"""<div class="score-hero">
<div class="score-value">{format_score(composite)}</div>
<div class="score-label">out of 100 &middot; {vendor} &middot; {total_tests} tests</div>
<div class="trust-level {level_class}">{trust_level}</div>
</div>\n\n"""

    # Dimension bars with inline styles
    result += "<h3>Dimension Scores</h3>\n"
    for key, label in DIMENSIONS:
        score = scores.get(key)
        if score is not None:
            color = get_bar_color_hex(score)
            w = int(round(score))
            result += f'''<div style="display:flex;align-items:center;gap:12px;padding:6px 0;">
<span style="width:180px;font-size:0.85rem;color:#374151;font-weight:500;text-align:right;flex-shrink:0;">{label}</span>
<span style="flex:1;height:10px;background:#e5e7eb;border-radius:5px;overflow:hidden;"><span style="display:block;height:100%;width:{w}%;background-color:{color};border-radius:5px;"></span></span>
<span style="width:45px;font-size:0.8rem;color:#6b7280;font-weight:600;">{score:.1f}</span>
</div>\n'''

    # Badge preview
    result += f"""\n<div class="badge-card">
<div class="badge-label">Badge Preview</div>
<a href="https://trustmodel.ai/leaderboard"><img src="{badge_url}" alt="TrustScore: {format_score(composite/10)}/10"></a>
<div class="badge-hint">Copy the markdown below to add this badge to your model card</div>
</div>"""

    return result, badge_code, badge_url


def compare_models(model_1, model_2, model_3, leaderboard):
    models = [m for m in [model_1, model_2, model_3] if m]
    if len(models) < 2:
        return "Please select at least 2 models to compare."

    entries = []
    for name in models:
        entry = next((e for e in leaderboard if e["model"] == name), None)
        if entry:
            entries.append(entry)

    if len(entries) < 2:
        return "Could not find selected models."

    # Score cards
    result = '<div style="display:flex;gap:16px;justify-content:center;margin-bottom:20px;flex-wrap:wrap;">'
    for e in entries:
        composite = e.get("composite_score", 0)
        trust_level, level_class = get_trust_level(composite)
        score_color = get_bar_color_hex(composite)
        result += f"""<div style="text-align:center;padding:16px 24px;border-radius:12px;background:#f8fafc;border:1px solid #e2e8f0;min-width:160px;">
<div style="font-size:0.8rem;color:#64748b;font-weight:600;">{e['model']}</div>
<div style="font-size:2rem;font-weight:800;color:{score_color};">{format_score(composite)}</div>
<span class="trust-level {level_class}">{trust_level}</span>
</div>"""
    result += "</div>\n\n"

    # Comparison table as HTML
    result += '<h3>Dimension Comparison</h3>'
    result += '<table style="width:100%;border-collapse:collapse;font-size:0.85rem;margin-top:12px;">'
    result += '<thead><tr style="border-bottom:2px solid #e2e8f0;text-align:left;">'
    result += '<th style="padding:8px 12px;">Dimension</th>'
    for e in entries:
        result += f'<th style="padding:8px 12px;text-align:center;">{e["model"]}</th>'
    result += '</tr></thead><tbody>'

    result += '<tr style="background:#f0fdf4;font-weight:700;border-bottom:1px solid #e2e8f0;">'
    result += '<td style="padding:8px 12px;">TrustScore</td>'
    for e in entries:
        cs = e.get("composite_score", 0)
        result += f'<td style="padding:8px 12px;text-align:center;color:{get_bar_color_hex(cs)};">{format_score(cs)}</td>'
    result += '</tr>'

    for key, label in DIMENSIONS:
        scores_list = []
        for e in entries:
            score = e.get("category_scores", {}).get(key)
            scores_list.append(score)

        best = max((s for s in scores_list if s is not None), default=None)

        result += '<tr style="border-bottom:1px solid #f1f5f9;">'
        result += f'<td style="padding:8px 12px;color:#374151;">{label}</td>'
        for score in scores_list:
            color = get_bar_color_hex(score) if score is not None else "#6b7280"
            is_best = score is not None and score == best and len(entries) > 1
            weight = "700" if is_best else "600"
            result += f'<td style="padding:8px 12px;text-align:center;font-weight:{weight};color:{color};">{format_score(score)}</td>'
        result += '</tr>'

    result += '</tbody></table>'

    best_entry = max(entries, key=lambda e: e.get("composite_score", 0))
    result += f'<p style="margin-top:16px;font-weight:700;font-size:0.95rem;">Winner: {best_entry["model"]} with TrustScore {format_score(best_entry.get("composite_score", 0))}/100</p>'

    return result


# Fetch leaderboard data on startup
leaderboard_data = fetch_leaderboard()
model_names = get_model_names(leaderboard_data)
model_count = len(model_names)

theme = gr.themes.Soft(
    primary_hue="cyan",
    secondary_hue="blue",
    neutral_hue="slate",
    font=("Inter", "system-ui", "sans-serif"),
)

# Base64 encode favicon SVG for head injection
with open("favicon.svg", "r") as _f:
    FAVICON_B64 = base64.b64encode(_f.read().encode()).decode()

FAVICON_HEAD = f'<link rel="icon" type="image/svg+xml" href="data:image/svg+xml;base64,{FAVICON_B64}">'

with gr.Blocks(title="TrustModel — AI Evaluation", css=CUSTOM_CSS, theme=theme, head=FAVICON_HEAD) as demo:

    # Header
    gr.HTML(f"""<div class="header-banner">
<img src="data:image/svg+xml;base64,{LOGO_B64}" alt="TrustModel" style="height:40px;margin:0 auto 14px;display:block;position:relative;">
<h1>AI Evaluation Space</h1>
<p>Look up TrustScores, compare AI models, and get badge embed code for your model card.</p>
<div class="stat-row">
    <span class="stat-pill">Free — No Account Needed</span>
    <span class="stat-pill">{model_count} Foundation Models</span>
    <span class="stat-pill">OpenAI · Google · Anthropic · Meta · MistralAI</span>
</div>
<p style="margin-top:12px !important;font-size:0.85rem !important;">
    <a href="https://trustmodel.ai">TrustModel.ai</a>
    <a href="https://trustmodel.ai/developers">API &amp; SDKs</a> &nbsp;&middot;&nbsp;
    <a href="https://trustmodel.ai/leaderboard">Full Leaderboard</a>
</p>
</div>""")

    with gr.Tab("Model Lookup"):
        gr.Markdown("Select a model to view its TrustScore across all 10 trust dimensions and get a badge for your model card.")

        if not model_names:
            gr.HTML('<p style="color:#ef4444;font-weight:600;text-align:center;">Unable to load models. Please refresh the page.</p>')

        model_dropdown = gr.Dropdown(
            choices=model_names,
            label="Select a Model",
            interactive=True,
        )

        result_md = gr.HTML()

        badge_section = gr.Column(visible=False)
        with badge_section:
            gr.Markdown("### Add to Your Model Card")
            badge_code = gr.Code(
                label="Copy this markdown and paste into your HuggingFace model card",
                language=None,
                interactive=False,
            )
            gr.HTML('<div style="text-align:center;margin-top:12px;"><a href="https://trustmodel.ai/leaderboard" target="_blank" style="display:inline-block;padding:8px 24px;background:#0ea5e9;color:#fff;border-radius:8px;font-size:0.9rem;font-weight:600;text-decoration:none;">View Full Rankings on TrustModel.ai &rarr;</a></div>')

        def on_lookup(model_name):
            result, badge, badge_url = lookup_model(model_name, leaderboard_data)
            show = bool(badge)
            return result, badge, gr.update(visible=show)

        model_dropdown.change(
            fn=on_lookup,
            inputs=[model_dropdown],
            outputs=[result_md, badge_code, badge_section],
        )

    with gr.Tab("Compare Models"):
        gr.Markdown("Select 2 or 3 models to compare their TrustScores side by side. Best score per dimension is highlighted.")

        with gr.Row():
            compare_1 = gr.Dropdown(choices=model_names, label="Model 1", interactive=True)
            compare_2 = gr.Dropdown(choices=model_names, label="Model 2", interactive=True)
            compare_3 = gr.Dropdown(choices=model_names, label="Model 3", interactive=True)

        compare_btn = gr.Button("Compare Models", variant="primary")
        compare_result = gr.HTML()
        compare_btn.click(
            fn=lambda m1, m2, m3: compare_models(m1, m2, m3, leaderboard_data),
            inputs=[compare_1, compare_2, compare_3],
            outputs=[compare_result],
        )

    # Footer
    gr.HTML("""<div style="position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid #e2e8f0;padding:12px 0;text-align:center;z-index:100;">
<div style="display:flex;align-items:center;justify-content:center;gap:8px;flex-wrap:wrap;">
    <strong style="font-size:0.8rem;color:#334155;">TrustModel Inc.</strong>
    <a href="https://trustmodel.ai" target="_blank" class="footer-link">Website</a>
    <span style="font-size:0.75rem;color:#d1d5db;">&middot;</span>
    <a href="https://trustmodel.ai/leaderboard" target="_blank" class="footer-link">Leaderboard</a>
    <span style="font-size:0.75rem;color:#d1d5db;">&middot;</span>
    <a href="https://trustmodel.ai/developers" target="_blank" class="footer-link">Developers</a>
    <span style="font-size:0.75rem;color:#d1d5db;">&middot;</span>
    <a href="https://trustmodel.ai/contact" target="_blank" class="footer-link">Contact</a>
</div>
</div>""")

demo.launch(theme=theme)