File size: 23,058 Bytes
9c56e37
 
 
 
 
 
a75f56e
9c56e37
 
01cea67
 
84fa20a
 
 
d55da55
68ecdd8
c6ae7c8
 
2322bf2
 
ceec99c
 
68ecdd8
5a899c1
68ecdd8
4c1f09b
 
5a899c1
ceec99c
 
 
 
 
 
 
 
 
 
 
 
 
9c56e37
0dd6bfb
 
 
 
 
 
 
 
 
cdf6932
 
 
 
 
 
 
 
84fa20a
b7aa921
 
ceec99c
3252680
 
 
e251f43
3252680
 
 
 
 
 
ceec99c
 
 
 
 
2322bf2
 
 
 
 
 
 
 
 
 
 
ceec99c
2322bf2
 
 
 
 
 
3252680
 
 
e120912
3252680
 
 
 
 
e120912
e251f43
3252680
 
 
 
 
 
 
 
 
 
 
 
 
 
e251f43
3252680
 
 
d55da55
2322bf2
ceec99c
 
 
 
84fa20a
 
 
9c56e37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2322bf2
9c56e37
 
84fa20a
 
 
 
 
f5c4a1c
84fa20a
 
 
 
 
d55da55
 
 
 
f5c4a1c
d55da55
 
 
 
 
 
f5c4a1c
d55da55
f5c4a1c
d55da55
 
 
 
f5c4a1c
d55da55
 
 
 
0dc9ad9
d55da55
68ecdd8
 
 
 
 
 
 
 
 
 
ceec99c
84fa20a
 
 
 
 
 
 
 
 
 
 
 
 
 
9c56e37
ceec99c
9c56e37
 
 
84fa20a
 
 
00be5eb
84fa20a
 
 
 
f5c4a1c
 
84fa20a
 
5a899c1
00be5eb
5a899c1
84fa20a
 
 
5a899c1
 
84fa20a
2322bf2
 
84fa20a
ceec99c
5a899c1
ceec99c
 
 
 
 
 
 
5a899c1
 
84fa20a
00be5eb
84fa20a
 
 
 
ceec99c
5a899c1
 
 
 
 
84fa20a
 
 
 
5a899c1
 
 
 
00be5eb
 
 
 
 
 
 
 
84fa20a
 
 
 
 
 
 
 
 
5a899c1
9c56e37
2322bf2
01cea67
2322bf2
 
ceec99c
2322bf2
 
 
 
 
 
 
 
 
 
 
 
 
01cea67
2322bf2
5a899c1
 
 
ceec99c
 
 
 
 
 
 
 
5a899c1
 
 
9c56e37
 
 
 
 
 
 
 
2322bf2
01cea67
9c56e37
01cea67
 
9c56e37
 
2322bf2
 
9c56e37
 
 
 
 
 
 
8f4e742
9c56e37
 
 
 
 
 
84fa20a
9c56e37
 
 
 
 
 
 
 
ceec99c
9c56e37
 
 
 
 
 
2322bf2
9c56e37
 
 
 
 
 
 
 
 
ceec99c
 
 
 
 
 
 
2322bf2
ceec99c
 
 
 
01cea67
ceec99c
 
 
 
 
 
 
 
 
 
 
01cea67
ceec99c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01cea67
ceec99c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01cea67
 
ceec99c
 
01cea67
 
 
 
 
 
 
ceec99c
84fa20a
01cea67
 
ceec99c
01cea67
ceec99c
 
 
 
01cea67
 
 
 
ceec99c
 
01cea67
ceec99c
68ecdd8
01cea67
ceec99c
 
01cea67
 
 
 
 
9c56e37
01cea67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceec99c
01cea67
 
 
 
ceec99c
01cea67
9c56e37
 
01cea67
9c56e37
 
 
01cea67
84fa20a
 
5a899c1
01cea67
 
 
 
2322bf2
 
01cea67
2322bf2
 
01cea67
 
 
84fa20a
 
01cea67
 
 
 
 
 
2322bf2
01cea67
 
9c56e37
b97be1a
2322bf2
ceec99c
 
 
 
 
 
 
 
 
 
 
84fa20a
9c56e37
5a899c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import streamlit as st
import pandas as pd
from dotenv import load_dotenv
from datasets import load_dataset
import json
import re
from openai import OpenAI
import os
from config import DATASETS, MODELS
import matplotlib.pyplot as plt
import altair as alt
from concurrent.futures import ThreadPoolExecutor, as_completed
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type
import threading
from anthropic import Anthropic
import google.generativeai as genai
import hmac
import hashlib
from uuid import uuid4
from datetime import datetime
from huggingface_hub import CommitScheduler, Repository
from pathlib import Path

load_dotenv()

st.set_page_config(page_title="LLM Healthcare Benchmarking", layout="wide")

WRITE_LOCK = threading.Lock()
DATA_DIR = Path("data")
DATA_DIR.mkdir(exist_ok=True)
RESULTS_FILE = DATA_DIR / "results.csv"


scheduler = CommitScheduler(
    repo_id=os.getenv("HF_REPO_ID"),       
    repo_type="dataset",
    folder_path=DATA_DIR,
    path_in_repo="data",                
    every=10,                               
    token=os.getenv("HF_TOKEN")           
)

def initialize_session_state():
    if 'api_configured' not in st.session_state:
        st.session_state.api_configured = False
    if 'togetherai_client' not in st.session_state:
        st.session_state.togetherai_client = None
    if 'openai_client' not in st.session_state:
        st.session_state.openai_client = None
    if 'anthropic_client' not in st.session_state:
        st.session_state.anthropic_client = None
    if 'all_results' not in st.session_state:
        st.session_state.all_results = {}
    if 'detailed_model' not in st.session_state:
        st.session_state.detailed_model = None
    if 'detailed_dataset' not in st.session_state:
        st.session_state.detailed_dataset = None
    if 'last_evaluated_dataset' not in st.session_state:
        st.session_state.last_evaluated_dataset = None

initialize_session_state()

def setup_api_clients():
    with st.sidebar:
        st.title("API Configuration")
        
        use_stored = st.checkbox("Use the stored API keys")
        
        if use_stored:
            username = st.text_input("Username")
            password = st.text_input("Password", type="password")
            
            if st.button("Verify Credentials"):
                stored_username = os.getenv("STREAMLIT_USERNAME", "")
                stored_password = os.getenv("STREAMLIT_PASSWORD", "")
                
                if (hmac.compare_digest(username, stored_username) and 
                    hmac.compare_digest(password, stored_password)):
                    try:
                        st.session_state.togetherai_client = OpenAI(
                            api_key=os.getenv('TOGETHERAI_API_KEY'),
                            base_url="https://api.together.xyz/v1"
                        )
                        st.session_state.openai_client = OpenAI(
                            api_key=os.getenv('OPENAI_API_KEY')
                        )
                        st.session_state.anthropic_client = Anthropic(
                            api_key=os.getenv('ANTHROPIC_API_KEY')
                        )
                        genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
                        
                        st.session_state.api_configured = True
                        st.success("Successfully configured the API clients with stored keys!")
                    except Exception as e:
                        st.error(f"Error initializing API clients: {str(e)}")
                        st.session_state.api_configured = False
                else:
                    st.error("Invalid credentials. Please try again or use your own API keys.")
                    st.session_state.api_configured = False
        else:
            st.subheader("Enter Your API Keys")
            togetherai_key = st.text_input("Together AI API Key", type="password", key="togetherai_key")
            openai_key = st.text_input("OpenAI API Key", type="password", key="openai_key")
            anthropic_key = st.text_input("Anthropic API Key", type="password", key="anthropic_key")
            gemini_key = st.text_input("Gemini API Key", type="password", key="gemini_key")
            
            if st.button("Initialize with the provided keys"):
                try:
                    st.session_state.togetherai_client = OpenAI(
                        api_key=togetherai_key,
                        base_url="https://api.together.xyz/v1"
                    )
                    st.session_state.openai_client = OpenAI(
                        api_key=openai_key
                    )
                    st.session_state.anthropic_client = Anthropic(
                        api_key=anthropic_key
                    )
                    genai.configure(api_key=gemini_key)
                    
                    st.session_state.api_configured = True
                    st.success("Successfully configured the API clients with provided keys!")
                except Exception as e:
                    st.error(f"Error initializing API clients: {str(e)}")
                    st.session_state.api_configured = False

setup_api_clients()


scheduler.start()

MAX_CONCURRENT_CALLS = 5
semaphore = threading.Semaphore(MAX_CONCURRENT_CALLS)

@st.cache_data
def load_dataset_by_name(dataset_name, split="train"):
    dataset_config = DATASETS[dataset_name]
    dataset = load_dataset(dataset_config["loader"])
    df = pd.DataFrame(dataset[split])
    df = df[df['choice_type'] == 'single']
    
    questions = []
    for _, row in df.iterrows():
        options = [row['opa'], row['opb'], row['opc'], row['opd']]
        correct_answer = options[row['cop']]
        
        question_dict = {
            'question': row['question'],
            'options': options,
            'correct_answer': correct_answer,
            'subject_name': row['subject_name'],
            'topic_name': row['topic_name'],
            'explanation': row['exp']
        }
        questions.append(question_dict)
    
    st.write(f"Loaded {len(questions)} single-select questions from `{dataset_name}`")
    return questions

@retry(
    wait=wait_exponential(multiplier=1, min=4, max=10),
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(Exception)
)
def get_model_response(question, options, prompt_template, model_name, clients):
    with semaphore:
        try:
            model_config = MODELS[model_name]
            options_text = "\n".join([f"{chr(65+i)}. {opt}" for i, opt in enumerate(options)])
            prompt = prompt_template.replace("{question}", question).replace("{options}", options_text)

            provider = model_config["provider"]

            if provider == "togetherai":
                response = clients["togetherai"].chat.completions.create(
                            model=model_config["model_id"],
                            messages=[{"role": "user", "content": prompt}]
                            )
                response_text = response.choices[0].message.content.strip()

            elif provider == "openai":
                response = clients["openai"].chat.completions.create(
                        model=model_config["model_id"],
                        messages=[{"role": "user", "content": prompt}]      
                    )
                response_text = response.choices[0].message.content.strip()

            elif provider == "anthropic":
                response = clients["anthropic"].messages.create(
                model=model_config["model_id"],
                messages=[{"role": "user", "content": prompt}],
                max_tokens=4096 
                )
                response_text = response.content[0].text

            elif provider == "google":
                model = genai.GenerativeModel(
                model_name=model_config["model_id"]
                )

                chat_session = model.start_chat(
                history=[]
                )
                response_text = chat_session.send_message(prompt).text

            # Extract JSON from response
            json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
            if not json_match:
                return f"Error: Invalid response format", response_text
            
            json_response = json.loads(json_match.group(0))
            answer = json_response.get('answer', '').strip()
            answer = re.sub(r'^[A-D]\.\s*', '', answer)
            
            if not any(answer.lower() == opt.lower() for opt in options):
                return f"Error: Answer '{answer}' does not match any options", response_text
            
            return answer, response_text
        except Exception as e:
            return f"Error: {str(e)}", str(e)


def evaluate_response(model_response, correct_answer):
    if model_response.startswith("Error:"):
        return False
    is_correct = model_response.lower().strip() == correct_answer.lower().strip()
    return is_correct

def process_single_evaluation(question, prompt_template, model_name, clients, last_evaluated_dataset):
    answer, response_text = get_model_response(
        question['question'],
        question['options'],
        prompt_template,
        model_name,
        clients 
    )
    is_correct = evaluate_response(answer, question['correct_answer'])
    result = {
        'dataset': last_evaluated_dataset, 
        'model': model_name,
        'question': question['question'],
        'correct_answer': question['correct_answer'],
        'subject': question['subject_name'],
        'options': ' | '.join(question['options']),
        'model_response': answer,
        'is_correct': is_correct,
        'explanation': question['explanation'],
        'timestamp': datetime.utcnow().isoformat()
    }
    
    with WRITE_LOCK:
        if RESULTS_FILE.exists():
            existing_df = pd.read_csv(RESULTS_FILE)
            updated_df = existing_df.append(result, ignore_index=True)
        else:
            updated_df = pd.DataFrame([result])
        
        updated_df.to_csv(RESULTS_FILE, index=False)
    
    return result

def process_evaluations_concurrently(questions, prompt_template, models_to_evaluate, progress_callback, clients, last_evaluated_dataset):
    results = []
    total_iterations = len(models_to_evaluate) * len(questions)
    current_iteration = 0

    if RESULTS_FILE.exists():
        existing_df = pd.read_csv(RESULTS_FILE)
        completed = set(zip(existing_df['model'], existing_df['question']))
    else:
        completed = set()

    with ThreadPoolExecutor(max_workers=MAX_CONCURRENT_CALLS) as executor:
        future_to_params = {}
        for model_name in models_to_evaluate:
            for question in questions:
                if (model_name, question['question']) in completed:
                    current_iteration += 1
                    progress_callback(current_iteration, total_iterations)
                    continue  # Skip already completed evaluations
                future = executor.submit(
                    process_single_evaluation, 
                    question, 
                    prompt_template, 
                    model_name, 
                    clients, 
                    last_evaluated_dataset
                )
                future_to_params[future] = (model_name, question)
        
        for future in as_completed(future_to_params):
            result = future.result()
            results.append(result)
            current_iteration += 1
            progress_callback(current_iteration, total_iterations)
    
    return results


def main():
    if 'all_results' not in st.session_state:
        st.session_state.all_results = {}
        st.session_state.last_evaluated_dataset = None
    if RESULTS_FILE.exists():
        existing_df = pd.read_csv(RESULTS_FILE)
        all_results = {}
        for _, row in existing_df.iterrows():
            model = row['model']
            result = row.to_dict()
            if model not in all_results:
                all_results[model] = []
            all_results[model].append(result)
        st.session_state.all_results = all_results
        st.session_state.last_evaluated_dataset = existing_df['dataset'].iloc[-1]
        st.info(f"Loaded existing results from `{RESULTS_FILE}`.")
    else:
        st.session_state.all_results = {}
        st.session_state.last_evaluated_dataset = None
        st.info(f"No existing results found. Ready to start fresh.")

    with st.sidebar:
        if st.button("Reset Results"):
            if RESULTS_FILE.exists():
                try:
                    RESULTS_FILE.unlink()
                    st.session_state.all_results = {}
                    st.session_state.last_evaluated_dataset = None
                    st.success("Results have been reset.")
                except Exception as e:
                    st.error(f"Error deleting file: {str(e)}")
            else:
                st.info("No results to reset.")

    col1, col2 = st.columns(2)
    with col1:
        selected_dataset = st.selectbox(
            "Select Dataset",
            options=list(DATASETS.keys()),
            help="Choose the dataset to evaluate on"
        )
    with col2:
        selected_models = st.multiselect(
            "Select Model(s)",
            options=list(MODELS.keys()),
            default=[list(MODELS.keys())[0]],
            help="Choose one or more models to evaluate."
        )

    models_to_evaluate = selected_models

    default_prompt = '''You are a medical AI assistant. Please answer the following multiple choice question.
Question: {question}

Options:
{options}

## Output Format:
Please provide your answer in JSON format that contains an "answer" field.
You may include any additional fields in your JSON response that you find relevant, such as:
- "choice reasoning": your detailed reasoning
- "elimination reasoning": why you ruled out other options

Example response format:
{
    "answer": "exact option text here(e.g., A. xxx, B. xxx, C. xxx)",
    "choice reasoning": "your detailed reasoning here",
    "elimination reasoning": "why you ruled out other options"
}

Important:
- Only the "answer" field will be used for evaluation
- Ensure your response is in valid JSON format'''

    # Customize Prompt Template
    col1, col2 = st.columns([2, 1])
    with col1:
        prompt_template = st.text_area(
            "Customize Prompt Template", 
            default_prompt, 
            height=400,
            help="Edit the prompt template before starting the evaluation."
        )
    
    with col2:
        st.markdown("""
        ### Prompt Variables
        - `{question}`: The medical question
        - `{options}`: The multiple choice options
        """)

    # Load Dataset
    if st.session_state.api_configured:
        with st.spinner("Loading dataset..."):
            questions = load_dataset_by_name(selected_dataset)
    else:
        st.warning("Please configure the API keys in the sidebar to load datasets and proceed.")
        questions = []

    # Filter by Subject
    if questions:
        subjects = sorted(list(set(q['subject_name'] for q in questions)))
        selected_subject = st.selectbox("Filter by subject", ["All"] + subjects)
        
        if selected_subject != "All":
            questions = [q for q in questions if q['subject_name'] == selected_subject]

        # Number of Questions to Evaluate
        num_questions = st.number_input(
            "Number of questions to evaluate", 
            min_value=1, 
            max_value=len(questions), 
            value=min(10, len(questions)), 
            step=1
        )

        # Start Evaluation Button
        if st.button("Start Evaluation"):
            if not models_to_evaluate:
                st.error("Please select at least one model to evaluate.")
            else:
                with st.spinner("Starting evaluation..."):
                    selected_questions = questions[:num_questions]
                    
                    clients = {
                        "togetherai": st.session_state["togetherai_client"],
                        "openai": st.session_state["openai_client"],
                        "anthropic": st.session_state["anthropic_client"]
                    }
                    
                    last_evaluated_dataset = st.session_state.last_evaluated_dataset if st.session_state.last_evaluated_dataset else selected_dataset

                    progress_container = st.container()
                    progress_bar = progress_container.progress(0)
                    status_text = progress_container.empty()
                    
                    def update_progress(current, total):
                        progress = current / total
                        progress_bar.progress(progress)
                        status_text.text(f"Progress: {current}/{total} evaluations completed")

                    results = process_evaluations_concurrently(
                        selected_questions,
                        prompt_template,
                        models_to_evaluate,
                        update_progress,
                        clients,
                        last_evaluated_dataset
                    )
                
                # Update Session State with New Results
                all_results = st.session_state.all_results.copy()
                for result in results:
                    model = result.pop('model')
                    if model not in all_results:
                        all_results[model] = []
                    all_results[model].append(result)
                
                st.session_state.all_results = all_results
                st.session_state.last_evaluated_dataset = selected_dataset

                # Set Default Detailed Model and Dataset if Not Set
                if st.session_state.detailed_model is None and all_results:
                    st.session_state.detailed_model = list(all_results.keys())[0]
                if st.session_state.detailed_dataset is None:
                    st.session_state.detailed_dataset = selected_dataset

                st.success("Evaluation completed!")
                st.experimental_rerun()

    # Display Evaluation Results
    if st.session_state.all_results:
        st.subheader("Evaluation Results")
        model_metrics = {}

        for model_name, results in st.session_state.all_results.items():
            df = pd.DataFrame(results)
            metrics = {
                'Accuracy': df['is_correct'].mean(),
            }
            model_metrics[model_name] = metrics

        metrics_df = pd.DataFrame(model_metrics).T.reset_index().rename(columns={'index': 'Model'})

        st.subheader("Model Performance Comparison")
        accuracy_chart = alt.Chart(
            metrics_df
        ).mark_bar().encode(
            x=alt.X('Model:N', title=None),
            y=alt.Y('Accuracy:Q', title='Accuracy', scale=alt.Scale(domain=[0, 1])),
            color=alt.Color('Model:N', scale=alt.Scale(scheme='blues')),
            tooltip=['Model:N', 'Accuracy:Q']
        ).properties(
            height=300,
            title={
                "text": "Model Accuracy",
                "anchor": "middle",
                "fontSize": 20
            }
        ).interactive()

        st.altair_chart(accuracy_chart, use_container_width=True)

    # Display Detailed Results
    if st.session_state.all_results:
        st.subheader("Detailed Results")
        
        def update_model():
            st.session_state.detailed_model = st.session_state.model_select
            
        def update_dataset():
            st.session_state.detailed_dataset = st.session_state.dataset_select

        col1, col2 = st.columns(2)
        with col1:
            selected_model_details = st.selectbox(
                "Select model",
                options=list(st.session_state.all_results.keys()),
                key="model_select",
                on_change=update_model,
                index=list(st.session_state.all_results.keys()).index(st.session_state.detailed_model) 
                    if st.session_state.detailed_model in st.session_state.all_results else 0
            )
        
        with col2:
            selected_dataset_details = st.selectbox(
                "Select dataset",
                options=[st.session_state.last_evaluated_dataset] if st.session_state.last_evaluated_dataset else [],
                key="dataset_select",
                on_change=update_dataset
            )

        if selected_model_details and selected_model_details in st.session_state.all_results:
            results = st.session_state.all_results[selected_model_details]
            df = pd.DataFrame(results)
            accuracy = df['is_correct'].mean()
            
            st.metric("Accuracy", f"{accuracy:.2%}")
            
            for idx, result in enumerate(results):
                with st.expander(f"Question {idx + 1} - {result['subject']}"):
                    st.write("**Question:**", result['question'])
                    st.write("**Options:**")
                    for i, opt in enumerate(result['options'].split(' | ')):
                        st.write(f"{chr(65+i)}. {opt}")
                    
                    col1, col2 = st.columns(2)
                    with col1:
                        st.write("**Model Response:**")
                        st.code(result.get('model_response', "N/A"))
                    with col2:
                        st.write("**Explanation:**")
                        st.code(result.get('explanation', "N/A"))
                    
                    col1, col2 = st.columns(2)
                    with col1:
                        st.write("**Correct Answer:**", result['correct_answer'])
                        st.write("**Model Answer:**", result['model_response'])
                    with col2:
                        if result['is_correct']:
                            st.success("Correct!")
                        else:
                            st.error("Incorrect")
                    
                    st.write("**Timestamp:**", result['timestamp'])
        else:
            st.info(f"No results available for {selected_model_details} on {selected_dataset_details}. Please run the evaluation first.")

        st.markdown("---")
        st.subheader("Download Results")
        if RESULTS_FILE.exists():
            csv_data = RESULTS_FILE.read_text(encoding='utf-8')
            st.download_button(
                label="Download All Results as CSV",
                data=csv_data,
                file_name=f"all_models_{st.session_state.last_evaluated_dataset}_results.csv",
                mime="text/csv", 
                key="download_all_results"
            )
        else:
            st.info("No data available to download.")

if __name__ == "__main__":
    main()