import os import sys # Completely disable experimental Gradio 5 SSR sidecar os.environ["GRADIO_SSR"] = "0" os.environ["GRADIO_SSR_MODE"] = "0" os.environ["GRADIO_ANALYTICS_ENABLED"] = "False" import json import gradio as gr import pandas as pd from gradio_leaderboard import Leaderboard, SelectColumns # Add current directory and src subdirectories to Python path CURRENT_DIR = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(0, CURRENT_DIR) for sub in ["src", "src/display", "src/submission", "src/leaderboard", "src/audit"]: p = os.path.join(CURRENT_DIR, sub) if os.path.exists(p): sys.path.insert(0, p) # DB Integration try: from src.db import mongo_get_all_certificates, mongo_save_certificate except ImportError: from db import mongo_get_all_certificates, mongo_save_certificate # About try: from src.about import CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, INTRODUCTION_TEXT, TITLE except ImportError: from about import CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT, INTRODUCTION_TEXT, TITLE # CSS try: from src.display.css_html_js import custom_css except ImportError: try: from src.css_html_js import custom_css except ImportError: try: from display.css_html_js import custom_css except ImportError: from css_html_js import custom_css # Formatting try: from src.display.formatting import build_top_3_cards_html, render_audit_details_panel except ImportError: try: from src.formatting import build_top_3_cards_html, render_audit_details_panel except ImportError: try: from display.formatting import build_top_3_cards_html, render_audit_details_panel except ImportError: from formatting import build_top_3_cards_html, render_audit_details_panel # Utils try: from src.display.utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields except ImportError: try: from src.utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields except ImportError: try: from display.utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields except ImportError: from utils import BENCHMARK_COLS, COLS, AutoEvalColumn, fields # Envs try: from src.envs import EVAL_RESULTS_PATH except ImportError: from envs import EVAL_RESULTS_PATH # Populate try: from src.populate import get_leaderboard_df, get_top_3_eval_cards except ImportError: from populate import get_leaderboard_df, get_top_3_eval_cards # Submit try: from src.submission.submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name except ImportError: try: from src.submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name except ImportError: try: from submission.submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name except ImportError: from submit import audit_or_search_model, clean_model_name, get_certificate_by_model_name def seed_demo_results(): if not os.path.exists(EVAL_RESULTS_PATH): os.makedirs(EVAL_RESULTS_PATH, exist_ok=True) existing_docs = mongo_get_all_certificates() if not existing_docs and not os.listdir(EVAL_RESULTS_PATH): mock_cert = { "status": "ok", "config": { "model_name": "Qwen/Qwen2.5-0.5B-Instruct", "model_sha": "main", "architecture": "Qwen2ForCausalLM", "params": 0.5, "precision": "bfloat16", "license": "apache-2.0", }, "audited_at": "2026-08-16 12:00:00", "spectral": { "stable_rank_mean": 86.74, "effective_rank_mean": 430.55, "condition_number_mean": 41650.41, "matrices_sampled": 48, }, "observer": { "tau": 0.95, "D": 896, "d": 428, "blind_fraction": 0.7718, "calibration_tokens": 10500, "calibration_texts": 200, "token_to_dim_ratio": 11.7, "sample_adequate": True, "min_ratio_floor": 5.0, }, "behavioral": { "n_probes": 18, "correct_count": 11, "factual_accuracy": 0.6111, "paraphrase_fidelity": 0.9058, "per_item": [], }, "composite": { "structural_risk": 43.3, "behavioral_risk": 38.9, "unvalidated_composite_score": 40.7, "verdict": "MODERATE RISK", "disclaimer": "Diagnostic signal only.", }, } with open(os.path.join(EVAL_RESULTS_PATH, "Qwen__Qwen2.5-0.5B-Instruct_main.json"), "w", encoding="utf-8") as f: json.dump(mock_cert, f, indent=2) mongo_save_certificate(mock_cert) seed_demo_results() LEADERBOARD_DF = get_leaderboard_df(EVAL_RESULTS_PATH, "", COLS, BENCHMARK_COLS) top_3_records = get_top_3_eval_cards(EVAL_RESULTS_PATH) initial_cert = get_certificate_by_model_name("Qwen/Qwen2.5-0.5B-Instruct") if top_3_records else {} def on_select_model(evt: gr.SelectData, df: pd.DataFrame): """Triggered whenever any cell or row is clicked in the Leaderboard.""" try: if evt.value: cleaned = clean_model_name(str(evt.value)) if cleaned: cert = get_certificate_by_model_name(cleaned) if cert and cert.get("status") == "ok": return render_audit_details_panel(cert) if isinstance(df, pd.DataFrame) and evt.index is not None: row_idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index if row_idx < len(df): model_col = AutoEvalColumn.model.name if model_col in df.columns: raw_model = str(df.iloc[row_idx][model_col]) cleaned = clean_model_name(raw_model) cert = get_certificate_by_model_name(cleaned) if cert: return render_audit_details_panel(cert) return render_audit_details_panel({}) except Exception as e: print(f"Selection error: {e}") return render_audit_details_panel({}) def on_top_card_click(model_name: str): """Triggered whenever one of the Top 3 Cards is clicked.""" cleaned = clean_model_name(model_name) cert = get_certificate_by_model_name(cleaned) if cert: return render_audit_details_panel(cert) return render_audit_details_panel({}) def init_leaderboard(dataframe): return Leaderboard( value=dataframe, datatype=[c.type for c in fields(AutoEvalColumn)], select_columns=SelectColumns( default_selection=[c.name for c in fields(AutoEvalColumn) if c.displayed_by_default], cant_deselect=[c.name for c in fields(AutoEvalColumn) if c.never_hidden], label="Select Columns to Display:", ), search_columns=[AutoEvalColumn.model.name, AutoEvalColumn.license.name], hide_columns=[c.name for c in fields(AutoEvalColumn) if c.hidden], interactive=False, ) def refresh_dashboard(): """Re-pulls leaderboard + top-3 data. Bound to demo.load() so that if the module-import-time fetch raced ahead of a still-connecting Mongo client on a cold start, the UI corrects itself once the page mounts.""" df = get_leaderboard_df(EVAL_RESULTS_PATH, "", COLS, BENCHMARK_COLS) top3 = get_top_3_eval_cards(EVAL_RESULTS_PATH) return df, build_top_3_cards_html(top3) custom_head_js = """ """ combined_css = custom_css + """ .hidden-bridge { position: absolute !important; opacity: 0 !important; pointer-events: none !important; height: 0px !important; width: 0px !important; overflow: hidden !important; margin: 0 !important; padding: 0 !important; } """ demo = gr.Blocks( theme=gr.themes.Soft(primary_hue="sky", secondary_hue="slate"), css=combined_css, head=custom_head_js, title="LLM-X-RAY Observatory", ) with demo: gr.HTML(TITLE) gr.Markdown(INTRODUCTION_TEXT) hidden_card_input = gr.Textbox(elem_id="hidden_card_model_input", elem_classes=["hidden-bridge"]) hidden_card_btn = gr.Button(elem_id="hidden_card_trigger_btn", elem_classes=["hidden-bridge"]) top_cards_display = gr.HTML(build_top_3_cards_html(top_3_records)) with gr.Row(): with gr.Column(scale=3): search_or_audit_input = gr.Textbox( label="🔍 Search Registry or Enter Hugging Face Model URL to Audit", placeholder="e.g. openbmb/MiniCPM-1B-sft-bf16 or SupraLabs/Supra2-Nano", lines=1, max_lines=1, show_label=True, ) trust_remote_code_checkbox = gr.Checkbox( label="Trust Remote Code (enabled for custom architectures like MiniCPM / Supra / Nanbeige)", value=True, ) audit_button = gr.Button("🔬 SCAN & AUDIT MODEL", variant="primary") status_box = gr.HTML() with gr.Column(scale=4): results_display_panel = gr.HTML(render_audit_details_panel(initial_cert)) leaderboard_table = init_leaderboard(LEADERBOARD_DF) # Audit & Search events audit_button.click( fn=audit_or_search_model, inputs=[search_or_audit_input, trust_remote_code_checkbox], outputs=[status_box, leaderboard_table, top_cards_display, results_display_panel], ) search_or_audit_input.submit( fn=audit_or_search_model, inputs=[search_or_audit_input, trust_remote_code_checkbox], outputs=[status_box, leaderboard_table, top_cards_display, results_display_panel], ) # Leaderboard row/cell click -> updates results display leaderboard_table.select( fn=on_select_model, inputs=[leaderboard_table], outputs=[results_display_panel], ) # Top 3 card click -> updates results display hidden_card_btn.click( fn=on_top_card_click, inputs=[hidden_card_input], outputs=[results_display_panel], ) # Page mount -> re-pull leaderboard + top-3 cards. # Self-healing refresh: guards against a cold-start race where the # module-level fetch above ran before the Mongo client finished # connecting, which would otherwise freeze a stale/incomplete # snapshot for every visitor until the container restarts. demo.load(fn=refresh_dashboard, outputs=[leaderboard_table, top_cards_display]) with gr.Row(): with gr.Accordion("📙 Methodology & Limitations", open=False): gr.Textbox(value=CITATION_BUTTON_TEXT, label=CITATION_BUTTON_LABEL, lines=8, show_copy_button=True) if __name__ == "__main__": # demo.queue() routes all events and generator streams through WebSockets to eliminate SvelteKit 405 errors demo.queue(default_concurrency_limit=5).launch() # if __name__ == "__main__": # demo.launch(server_name="0.0.0.0", server_port=7860) # if __name__ == "__main__": # demo.launch(server_name="0.0.0.0", server_port=7860)