shanchen's picture
Update app.py
3ef98be verified
import pandas as pd
import numpy as np
import gradio as gr
import plotly.express as px
import plotly.graph_objects as go
# Creating data for explanation df in about section
explanation_data = {
"Accuracy Scores": [
"DrugMatchQA",
"MedMCQA: G2B",
"MedMCQA: Original",
"MedMCQA: Difference",
"MedQA: G2B",
"MedQA: Original",
"MedQA: Difference",
"Adjusted Robustness Score"
],
"Description": [
"A custom MC task where the model is asked to match a brand name to its generic counterpart and vice versa. This task is designed to test the model's ability to understand drug name synonyms. *Gemini results are missing due to their safety filters*",
"G2B Refers to the 'Generic' to 'Brand' name swap. This is model accuracy on MedMCQA task where generic drug names are substituted with brand names.",
"Model accuracy on MedMCQA task with original data. (Only includes questions that overlap with the g2b dataset)",
"Difference in MedMCQA accuracy for swapped and non-swapped datasets, highlighting the impact of G2B drug name substitution on performance.",
"Model accuracy on MedQA (4 options) task where generic drug names are substituted with brand names.",
"Model accuracy on MedQA (4 options) task with original data. (Only includes questions that overlap with the g2b dataset)",
"Difference in MedMCQA accuracy for swapped and non-swapped datasets, highlighting the impact of G2B drug name substitution on performance.",
"A score given by Avg Difference / Avg G2B Accuracy. A higher score indicates a model that is more robust to drug name synonym substitution."
]
}
explanation_df = pd.DataFrame(explanation_data)
#Loading and cleaning eval data processed by json2df.py
df = pd.read_csv("data/csv/models_data.csv")
df['average_g2b'] = df[['medmcqa_g2b', 'medqa_4options_g2b']].mean(axis=1).round(2)
df['average_original_acc'] = df[['medmcqa_orig_filtered', 'medqa_4options_orig_filtered']].mean(axis=1).round(2)
df['average_diff'] = df[['medmcqa_diff', 'medqa_diff']].mean(axis=1).round(2)
df.drop(columns=['b4b'], inplace=True)
#Rename columns for clarity
df.rename(columns={
'medmcqa_g2b': 'MedMCQA: G2B',
'medmcqa_orig_filtered': 'MedMCQA: Original',
'medmcqa_diff': 'MedMCQA: Difference',
'medqa_4options_g2b': 'MedQA: G2B',
'medqa_4options_orig_filtered': 'MedQA: Original',
'medqa_diff': 'MedQA: Difference',
'b4bqa': 'DrugMatchQA',
'average_g2b': 'Average G2B Accuracy',
'average_original_acc': 'Average Original Accuracy',
'average_diff': 'Average Difference'
}, inplace=True)
# Sort DataFrame by DrugMatchQA descending
df = df.sort_values(by='Average G2B Accuracy', ascending=False)
#Create adjusted robustness score that accounts for g2b accuracy and difference in accuracy
df['Average Accuracy (Original and G2B)'] = (df['Average G2B Accuracy'] + df['Average Original Accuracy']) / 2
#df['Adjusted Robustness Score'] = df['Average Accuracy (Original and G2B)'] - 0.25 - df['Average Difference'].abs()
#df['Adjusted Robustness Score'] = df['Adjusted Robustness Score'].round(2)
# Blog posts content with images
blog_posts = [
{"title": "TL;DR",
"content": '''
**Why Are Language Models Surprisingly Fragile to Drug Names? πŸ©ΊπŸ’Š**
Language models (LLMs) like GPT are transforming medicine with their data processing and decision-support capabilities. But there's a twist: they seem to be great at memorising but pretty bad at connecting concepts like drug names! Researchers behind the RABBITS dataset (Robust Assessment of Biomedical Benchmarks Involving drug Term Substitutions for Language Models) set out to evaluate how LLMs perform when brand names (like Advil) are swapped with their generic equivalents (like ibuprofen) in datasets like MedMCQA.
The results? A surprising performance drop of up to 10%! πŸ“‰ On average, accuracy dipped by 4% when brand names were swapped for generics. This is concerning because a simple drug name swap could lead to serious medical misinformation and errors. πŸš‘
**Why does this happen?**
1. **Data Contamination:** One major factor is that LLMs are often trained on datasets that include test data (Over 90% of MedQA questions appeared to some extent in Dolma Dataset), leading to inflated performance metrics. When faced with new, unseen data, their performance drops significantly.
2. **Memorization Over Understanding:** Larger models like Llama-3-70B show a greater drop in accuracy, suggesting they rely more on memorization than genuine comprehension. For instance, Llama-3-70B's accuracy fell from 76.6% to 69.7% with generic-to-brand swaps.
**The Study's Findings**
A key graph in the paper (Figure 2) shows the performance of different models on the original dataset versus the swapped (generic-to-brand) dataset. The dashed diagonal line represents the ideal scenario where synonym swaps don't affect performance. All open-source models from 7B parameters and above fell below this line, indicating decreased performance with drug name swaps.
![Performance Graph](file/b4b_tight.png)
**Conclusion**
The RABBITS study highlights a critical area for improvement in medical AI. While LLMs have enormous potential, they need to be more robust and accurate to avoid the pitfalls of drug name variability. So, the next time you ask an AI about meds, remember: it's still learning the ropes with all those tricky drug names! πŸ§ πŸ’‘
Check out the RABBITS leaderboard on Hugging Face to see how different models stack up!
'''},
{"title": "Motivation/Problem 🩺",
"content": '''
**Unveiling the Fragility of Language Models to Drug Names πŸ©ΊπŸ’Š**
### Motivation and Problem
Language models (LLMs) like GPT are touted as game-changers in the medical field, providing support in data processing and decision-making. However, there's a significant challenge: these models struggle with the variability in drug names. Patients often use brand names (like Tylenol) instead of generic equivalents (like acetaminophen), and this can confuse LLMs, leading to decreased accuracy and potential misinformation. This is a critical issue in healthcare, where factuality is paramount.
### What We Did
To tackle this problem, we developed a specialized dataset called RABBITS (Robust Assessment of Biomedical Benchmarks Involving drug Term Substitutions for Language Models). Here's what we did:
1. **Dataset Creation:** We used the RxNorm database to generate a comprehensive list of brand and generic drug pairs. This involved identifying 2,271 generic drugs and mapping them to 6,961 brands.
2. **Data Transformation:** Using regular expressions, we created two versions of medical QA datasets (MedQA and MedMCQA): one with brand names swapped to generics and one with generics swapped to brand names.
3. **Expert Review:** The transformed datasets were rigorously reviewed by physician experts to ensure accuracy and context consistency.
4. **Evaluation:** We evaluated various open-source and API-based LLMs on these transformed datasets using the EleutherAI lm-evaluation harness in a zero-shot setting. The goal was to measure performance differences when drug names were swapped.
![Workflow](file/workflow.png)
### Results
The performance of the models was summarized in a leaderboard, showcasing the impact of drug name variability on their accuracy. Here’s how they ranked:
| Model | Original Accuracy | Swapped Accuracy (g2b) | Difference |
|---------------------|-------------------|-------------------------|------------|
| GPT-3.5-turbo-0125 | 97.29% | 96.86% | -0.42% |
| GPT-4o | 90.36% | 87.42% | -2.94% |
| GPT4-0613 | 92.00% | 89.37% | -2.63% |
| Gemini 1 Pro | 69.36% | 73.44% | +4.07% |
| Gemini 1.5 Flash | 97.25% | 95.43% | -1.82% |
| Llama-3-70B | 76.64% | 69.71% | -6.93% |
| Mixtral-8x22B-v0.1 | 70.92% | 64.62% | -6.29% |
The leaderboard clearly shows that swapping drug names causes a notable drop in accuracy for most models, highlighting the need for more robust LLMs in the medical domain.
### Conclusion
The RABBITS dataset sheds light on a critical weakness in current language models' handling of drug names. While LLMs hold great promise, their ability to accurately interpret and respond to drug-related queries still needs significant improvement. Our research underscores the importance of robustness in AI for healthcare to ensure safe and reliable patient support.
Check out the full RABBITS leaderboard on Hugging Face to see how different models compare!
'''},
{"title": "DrugMatchQA task (b4bqa)",
"content": '''
**Exploring the DrugMatchQA Task: Uncovering Hidden Challenges in Language Models πŸ©ΊπŸ“Š**
### What We Did
We introduced the DrugMatchQA task (b4bqa) and leveraged the Dolma dataset for detailed analysis. Here's a breakdown of our approach:
1. **Creating the DrugMatchQA Task (b4bqa):**
- We developed a specialized benchmark by transforming existing medical QA datasets (MedQA and MedMCQA) to test LLMs' robustness in understanding drug name synonyms.
- Using regular expressions, we swapped brand names with their generic equivalents and vice versa, creating two new datasets: brand-to-generic (b2g) and generic-to-brand (g2b).
2. **Dolma Dataset Counting:**
- We analyzed the Dolma dataset, a massive collection of 3.1 trillion tokens, to understand how frequently brand and generic drug names appear.
- For the drugs found in the test sets of MedQA and MedMCQA, we found that generic names were much more common than brand names in Dolma.
- We found medical benchmark contamination in Dolma. For instance, 99.21% of MedQA test data and 34.13% of MedMCQA test data overlapped with Dolma.
### Results
Our evaluation focused on comparing LLM performance on the original datasets versus the transformed ones (g2b and b2g). Here's a summary of our findings:
#### Dataset Analysis
| Subset | Terms | Average | Median | Std. Dev |
|--------------|---------|----------|---------|--------------|
| Dolma | Generic | 564,151 | 136,682 | 2,399,928 |
| | Brand | 234,138 | 698 | 2,543,075 |
| Red Pajama | Generic | 161,227 | 42,549 | 620,393 |
| | Brand | 29,561 | 84 | 232,661 |
| Pile train | Generic | 96,309 | 28,074 | 325,307 |
| | Brand | 4,757 | 19 | 43,613 |
| C4 train | Generic | 27,973 | 5,454 | 144,162 |
| | Brand | 9,941 | 26 | 96,504 |
#### Contamination
| Dataset | Percentage |
|---------------|------------|
| MedQA Train | 86.92% |
| MedQA Val | 98.10% |
| MedQA Test | 99.21% |
| MedMCQA Train | 22.41% |
| MedMCQA Val/Test | 34.13% |
**Key Takeaways:**
- **Performance Drop:** Most models experienced a notable drop in accuracy with the g2b swaps, highlighting their reliance on memorization rather than true comprehension.
- **Contamination Impact:** The high overlap of test data with the Dolma dataset likely inflated original performance metrics, masking the true challenges LLMs face in handling drug name variability.
### Conclusion
The DrugMatchQA task and our Dolma dataset analysis reveal critical weaknesses in how current language models handle drug names. While LLMs offer significant potential in healthcare, they need to be more robust and accurate to ensure reliable patient support. Our findings underscore the importance of addressing dataset contamination and enhancing LLM robustness to meet the stringent demands of medical applications.
'''}
]
#if acc is 0 in DrugMatchQA column, set it to none
df['DrugMatchQA'] = df['DrugMatchQA'].apply(lambda x: None if x == 0 else x)
def remove_rows_with_strings(df, column, strings):
for string in strings:
df = df[~df[column].str.contains(string)]
return df
models_to_remove = ['microsoft-phi-1', 'microsoft-phi-1_5', 'meta-llama-Llama-2-7b-hf']
non_random_df = remove_rows_with_strings(df, 'Model', models_to_remove)
#Defining functions for filtering and plotting
filter_mapping = {
"all": "all",
"🟒 Pre-trained": "🟒",
"🟩 Continuously pre-trained": "🟩",
"πŸ”Ά Fine-tuned on domain-specific data": "πŸ”Ά",
"πŸ’¬ Chat-models (RLHF, DPO, IFT, ...)": "πŸ’¬"
}
def filter_items(df, query):
if query == "all":
return df
filter_value = filter_mapping[query]
return df[df["T"].str.contains(filter_value, na=False)]
def create_scatter_plot(df, x_col, y_col, title, x_title, y_title):
fig = px.scatter(df, x=x_col, y=y_col, color='Model', title=title)
fig.add_trace(
go.Scatter(
x=[0, 100],
y=[0, 100],
mode="lines",
name="y=x line",
line=dict(color='black', dash='dash')
)
)
fig.update_layout(
xaxis_title=x_title,
yaxis_title=y_title,
xaxis=dict(range=[0, 100]),
yaxis=dict(range=[0, 100]),
legend_title_text='Model'
)
fig.update_traces(marker=dict(size=10), selector=dict(mode='markers'))
return fig
def create_lm_plot(df, x_col, y_col, title, x_title, y_title):
fig = px.scatter(df, x=x_col, y=y_col, color='Model', title=title, trendline='ols')
fig.update_layout(
xaxis_title=x_title,
yaxis_title=y_title,
legend_title_text='Model'
)
fig.update_traces(marker=dict(size=10), selector=dict(mode='markers'))
return fig
def create_bar_plot(df, col, title):
sorted_df = df.sort_values(by=col, ascending=True)
fig = px.bar(sorted_df,
x=col,
y='Model',
orientation='h',
title=title,
color=col,
color_continuous_scale='Aggrnyl')
fig.update_layout(xaxis_title=col, yaxis_title='Model', height=600, coloraxis_showscale=False)
fig.update_xaxes(range=[-20, 20])
return fig
def create_bar_plot_drugmatchqa(df, col, title):
clean_df = df.dropna(subset=['DrugMatchQA'])
sorted_df = clean_df.sort_values(by=col, ascending=True)
fig = px.bar(sorted_df,
x=col,
y='Model',
orientation='h',
title=title,
color=col,
color_continuous_scale='Aggrnyl')
fig.update_layout(xaxis_title=col, yaxis_title='Model', height=600, coloraxis_showscale=False)
return fig
def create_bar_plot_adjusted(df, col, title):
sorted_df = df.sort_values(by=col, ascending=True)
fig = px.bar(sorted_df,
x=col,
y='Model',
orientation='h',
title=title,
color=col,
color_continuous_scale='Aggrnyl')
fig.update_layout(xaxis_title=col, yaxis_title='Model', height=600, coloraxis_showscale=False)
return fig
#Create UI/Layout
with gr.Blocks(css="custom.css") as demo:
with gr.Column():
gr.Markdown(
"""<div style="text-align: center;"><h1> <span style='color: #00BF63;'>🐰 RABBITS</span>: <span style='color: #00BF63;'>R</span>obust <span style='color: #00BF63;'>A</span>ssessment of <span style='color: #00BF63;'>B</span>iomedical <span style='color: #00BF63;'>B</span>enchmarks <span style='color: #00BF63;'>I</span>nvolving drug
<span style='color: #00BF63;'>T</span>erm <span style='color: #00BF63;'>S</span>ubstitutions<span style='color: #00BF63;'></span></h1></div>"""
)
with gr.Row():
gr.Markdown(""" """)
with gr.Row():
gr.Markdown(
"""<div style="text-align: justify;">
<p class='markdown-text'>Robust language models are crucial in the medical domain and the RABBITS project tests the robustness of LLMs by evaluating their handling of synonyms, specifically brand and generic drug names. We assessed 16 open-source language models from Hugging Face using systematic synonym substitution on MedQA and MedMCQA tasks. Our results show a consistent decline in performance across all model sizes, highlighting challenges in synonym comprehension. Additionally, we discovered significant dataset contamination by identifying overlaps between MedQA, MedMCQA test sets, and the Dolma 1.6 dataset using an 8-gram analysis. This highlights the need to improve model robustness and address contamination in open-source datasets.</p>
</div>"""
)
with gr.Row():
gr.Markdown(""" """)
with gr.Row():
gr.Markdown(""" """)
with gr.Row():
bar1 = gr.Plot(
value=create_bar_plot(df, "MedMCQA: Difference", "Impact of Generic2Brand swap on MedMCQA Accuracy"),
elem_id="bar1"
)
bar2 = gr.Plot(
value=create_bar_plot(df, "MedQA: Difference", "Impact of Generic2Brand swap on MedQA Accuracy"),
elem_id="bar2"
)
with gr.Row():
gr.Markdown(""" """)
#default_visible_columns = []
with gr.Tabs(elem_classes="tab-buttons"):
with gr.TabItem("πŸ” Evaluation table"):
with gr.Column():
with gr.Accordion("➑️ See All Columns", open=False):
shown_columns = gr.CheckboxGroup(
choices=df.columns.tolist(),
value=df.columns.tolist(),
label="Select Columns",
interactive=True,
)
with gr.Row():
search_bar = gr.Textbox(
placeholder="πŸ” Search for your model and press ENTER...",
show_label=False,
elem_id="search-bar"
)
filter_columns = gr.Radio(
label="⏚ Filter model types",
choices=[
"all",
"🟒 Pre-trained",
"🟩 Continuously pre-trained",
"πŸ”Ά Fine-tuned on domain-specific data",
"πŸ’¬ Chat-models (RLHF, DPO, IFT, ...)"
],
value="all",
elem_id="filter-columns",
)
leaderboard_df = gr.Dataframe(
value=df,
headers="keys",
datatype=["html" if col == "Model" else "str" for col in df.columns],
interactive=False,
elem_id="leaderboard-table"
)
with gr.Row():
gr.Dataframe(
value=explanation_df,
headers="keys",
datatype=["str", "str"],
interactive=False,
label="Explanation of Scores"
)
def update_leaderboard(search_query):
filtered_df = df[df["Model"].str.contains(search_query, case=False)]
return filtered_df
search_bar.submit(
update_leaderboard,
inputs=search_bar,
outputs=leaderboard_df
)
def filter_update(query):
filtered_df = filter_items(df, query)
return filtered_df
filter_columns.change(
filter_update,
inputs=filter_columns,
outputs=leaderboard_df
)
shown_columns.change(
lambda cols: df[cols],
inputs=shown_columns,
outputs=leaderboard_df
)
with gr.TabItem("πŸ“Š Evaluation Plots"):
with gr.Column():
with gr.Row():
scatter1 = gr.Plot(
value=create_scatter_plot(df, "MedMCQA: Original", "MedMCQA: G2B",
"MedMCQA: Orig vs G2B", "MedMCQA: Original", "MedMCQA: G2B"),
elem_id="scatter1"
)
scatter2 = gr.Plot(
value=create_scatter_plot(df, "MedQA: Original", "MedQA: G2B",
"MedQA: Orig vs G2B", "MedQA: Original", "MedQA: G2B"),
elem_id="scatter2"
)
with gr.TabItem("πŸ“ About"):
with gr.Column():
gr.Markdown(
"""<div style="text-align: center;">
<h2>About the RABBITS LLM Leaderboard</h2>
<p>The following is an overview of the framework, along with an explanation of scores in the evaluation table.</p>
</div>""",
elem_classes="markdown-text"
)
with gr.Row():
gr.Image(value="workflow-1-2.svg", width=200, height=450)
gr.Image(value="workflow-3-4.svg", width=200, height=450)
with gr.Row():
gr.Dataframe(
value=explanation_df,
headers="keys",
datatype=["str", "str"],
interactive=False,
label="Explanation of Scores"
)
with gr.TabItem("πŸš€ Submit Here!"):
gr.Markdown(
"""<div style="text-align: center;">
<h2>Submit Your Model Results</h2>
<p>If you have new model results that you would like to add to the leaderboard, please follow the submission guidelines below:</p>
<ul>
<li>COMING SOON</li>
</ul>
<p>COMING SOON</p>
</div>""",
elem_classes="markdown-text"
)
for post in blog_posts:
with gr.Accordion(post["title"], open=False):
gr.Markdown(post["content"])
with gr.Row():
with gr.Accordion("πŸ“™ Citation", open=False):
citation_button = gr.Textbox(
value="Copy the following snippet to cite these results/works",
label='''
@misc{gallifant2024language,
title={Language Models are Surprisingly Fragile to Drug Names in Biomedical Benchmarks},
author={Jack Gallifant and Shan Chen and Pedro Moreira and Nikolaj Munch and Mingye Gao and Jackson Pond and Leo Anthony Celi and Hugo Aerts and Thomas Hartvigsen and Danielle Bitterman},
year={2024},
eprint={2406.12066},
archivePrefix={arXiv},
primaryClass={id='cs.CL' full_name='Computation and Language' is_active=True alt_name='cmp-lg' in_archive='cs' is_general=False description='Covers natural language processing. Roughly includes material in ACM Subject Class I.2.7. Note that work on artificial languages (programming languages, logics, formal systems) that does not explicitly address natural-language issues broadly construed (natural-language processing, computational linguistics, speech, text retrieval, etc.) is not appropriate for this area.'}
}''',
# lines=8,
elem_id="citation-button",
show_copy_button=True,
)
with gr.Row():
bar3 = gr.Plot(
value=create_bar_plot_drugmatchqa(df, "DrugMatchQA", "Which LLMs are best at matching brand names to generic drug names?"),
elem_id="bar3"
)
bar4 = gr.Plot(
#remove model in model column
value=create_bar_plot_adjusted(non_random_df, "Average Difference", "Which LLMs are most robust to drug name synonym substitution?"),
elem_id="bar4"
)
if __name__ == "__main__":
demo.launch(allowed_paths=["/"])