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"""
| Dimension | ' for e in entries: result += f'{e["model"]} | ' result += '
|---|---|
| TrustScore | ' for e in entries: cs = e.get("composite_score", 0) result += f'{format_score(cs)} | ' result += '
| {label} | ' 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'{format_score(score)} | ' result += '
Winner: {best_entry["model"]} with TrustScore {format_score(best_entry.get("composite_score", 0))}/100
' 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'' with gr.Blocks(title="TrustModel — AI Evaluation", css=CUSTOM_CSS, theme=theme, head=FAVICON_HEAD) as demo: # Header gr.HTML(f"""""") 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('Unable to load models. Please refresh the page.
') 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('') 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("""