Spaces:
Sleeping
Sleeping
Claude Opus 4.6 (1M context)
fix: Add !important to hero capsule styles for HuggingFace compatibility
d6e477e Download app.py from TrustModel/evaluate: direct link, hf CLI and curl.
- Browser
- Download file 17 kB
-
https://huggingface.co/spaces/TrustModel/evaluate/resolve/main/app.py
- Command line
-
hf download hf://spaces/TrustModel/evaluate/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TrustModel/evaluate/resolve/main/app.py
17 kB
| 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'[](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 · {vendor} · {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 & SDKs</a> · | |
| <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 →</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;">·</span> | |
| <a href="https://trustmodel.ai/leaderboard" target="_blank" class="footer-link">Leaderboard</a> | |
| <span style="font-size:0.75rem;color:#d1d5db;">·</span> | |
| <a href="https://trustmodel.ai/developers" target="_blank" class="footer-link">Developers</a> | |
| <span style="font-size:0.75rem;color:#d1d5db;">·</span> | |
| <a href="https://trustmodel.ai/contact" target="_blank" class="footer-link">Contact</a> | |
| </div> | |
| </div>""") | |
| demo.launch(theme=theme) | |