Spaces:
Sleeping
Sleeping
Download app.py from Pitt-iRiS-Lab/MedIRT: direct link, hf CLI and curl.
- Browser
- Download file 5 kB
-
https://huggingface.co/spaces/Pitt-iRiS-Lab/MedIRT/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pitt-iRiS-Lab/MedIRT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pitt-iRiS-Lab/MedIRT/resolve/main/app.py
5 kB
| import gradio as gr | |
| import pandas as pd | |
| from apscheduler.schedulers.background import BackgroundScheduler | |
| from src.about import ( | |
| CITATION_BUTTON_LABEL, | |
| CITATION_BUTTON_TEXT, | |
| INTRODUCTION_TEXT, | |
| LLM_BENCHMARKS_TEXT, | |
| TITLE, | |
| ) | |
| from src.display.css_html_js import custom_css | |
| from src.display.formatting import get_leaderboard_table_html | |
| from src.envs import API, REPO_ID, TOKEN | |
| # ============================================================================= | |
| # Helper Functions | |
| # ============================================================================= | |
| def restart_space(): | |
| """Restart the Hugging Face space""" | |
| API.restart_space(repo_id=REPO_ID) | |
| def load_medical_data(): | |
| """Load and preprocess medical LLM evaluation data""" | |
| try: | |
| df = pd.read_csv("data/llm_medical_model_M80_comparison.csv") | |
| df_ability = pd.read_csv("data/model_abilities_by_topic.csv") | |
| topics = [l.strip() for l in | |
| open("data/llm_medical_multi_run_topics.txt", encoding="utf-8") if | |
| l.strip()] | |
| # Medical specialty column mappings | |
| column_renames = { | |
| 'acc_Behavioral Health & Nervous Systems/Special Senses': 'Neuro', | |
| 'acc_Biostatistics & Epidemiology/Population Health': 'Biostats', | |
| 'acc_Blood & Lymphoreticular/Immune Systems': 'Hemo', | |
| 'acc_Cardiovascular System': 'Cardio', | |
| 'acc_Gastrointestinal System': 'GI', | |
| 'acc_Human Development': 'DevMed', | |
| 'acc_Multisystem Processes & Disorders': 'MultiSys', | |
| 'acc_Musculoskeletal, Skin & Subcutaneous Tissue': 'MSK', | |
| 'acc_Reproductive & Endocrine Systems': 'Repro', | |
| 'acc_Respiratory & Renal/Urinary Systems': 'Resp', | |
| 'acc_Social Sciences: Communication and Interpersonal Skills': 'Comm' | |
| } | |
| abbr = ["Comm", "Behav/Neuro", "Blood/Immune", "MSK/Skin", "Multi", "GI", "Dev", "Resp/Renal", "Cardio", | |
| "Repro/Endo", "Bio/Epi"] | |
| df = df.rename(columns=column_renames) | |
| column_renames_ability = { | |
| f"irt_ability_{i+1}": topics[i] for i in range(len(topics)) | |
| } | |
| df_ability = df_ability.rename(columns=column_renames_ability) | |
| new_cols = ["model_id", "Mean"]+ [v for v in column_renames_ability.values()] | |
| df_ability = df_ability[new_cols] | |
| # Sort by accuracy and add ranking | |
| # df = df.sort_values('accuracy', ascending=False) | |
| # df.insert(0, 'rank', range(1, len(df) + 1)) | |
| df_ability = df_ability.sort_values('Mean', ascending=False) | |
| df_ability.insert(0, 'rank', range(1, len(df_ability) + 1)) | |
| return df_ability | |
| except Exception as e: | |
| print(f"Error loading medical data: {e}") | |
| return pd.DataFrame() | |
| def init_leaderboard(dataframe): | |
| """Initialize leaderboard with custom HTML table""" | |
| if dataframe is None or dataframe.empty: | |
| raise ValueError("Leaderboard DataFrame is empty or None.") | |
| return get_leaderboard_table_html(dataframe) | |
| # ============================================================================= | |
| # Load Data | |
| # ============================================================================= | |
| LEADERBOARD_DF = load_medical_data() | |
| # ============================================================================= | |
| # Gradio Interface | |
| # ============================================================================= | |
| demo = gr.Blocks(css=custom_css) | |
| with demo: | |
| # Header | |
| gr.HTML(TITLE) | |
| gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text") | |
| with gr.Tabs(elem_classes="tab-buttons") as tabs: | |
| # Main Leaderboard Tab | |
| with gr.TabItem("🏥 LLM Leaderboard for Medical Abilities", elem_id="medical-llm-tab", id=0): | |
| leaderboard_html = init_leaderboard(LEADERBOARD_DF) | |
| gr.HTML(leaderboard_html) | |
| # Radar Charts Tab | |
| with gr.TabItem("🎯 Ability Radar Charts", elem_id="radar-tab", id=1): | |
| gr.Markdown("", elem_classes="markdown-text-radar") | |
| # About Tab | |
| with gr.TabItem("📝 About", elem_id="llm-benchmark-tab-table", id=2): | |
| gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text") | |
| # Citation Footer | |
| with gr.Row(): | |
| with gr.Accordion("📙 Citation", open=False): | |
| citation_button = gr.Textbox( | |
| value=CITATION_BUTTON_TEXT, | |
| label=CITATION_BUTTON_LABEL, | |
| lines=20, | |
| elem_id="citation-button", | |
| show_copy_button=True, | |
| ) | |
| # ============================================================================= | |
| # Application Launch | |
| # ============================================================================= | |
| # Schedule automatic space restart every 30 minutes | |
| scheduler = BackgroundScheduler() | |
| scheduler.add_job(restart_space, "interval", seconds=1800) | |
| scheduler.start() | |
| # Launch the application | |
| demo.queue(default_concurrency_limit=40).launch() |