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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
import streamlit as st
import pandas as pd
import plotly.express as px
import numpy as np
from datetime import datetime, timedelta
import json
from pymongo import MongoClient
from dotenv import load_dotenv
import os
import bcrypt
from openai import OpenAI
from streamlit_plotly_events import plotly_events
from pinecone import Pinecone, ServerlessSpec
import threading  # {{ edit_25: Import threading for background processing }}
import tiktoken
from tiktoken.core import Encoding
from runner import run_model
from bson.objectid import ObjectId
import traceback  # Add this import at the top of your file
import umap
import plotly.graph_objs as go
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import plotly.colors as plc

# Add this helper function at the beginning of your file
def extract_prompt_text(prompt):
    if isinstance(prompt, dict):
        return prompt.get('prompt', '')
    elif isinstance(prompt, str):
        return prompt
    else:
        return str(prompt)

# Set page configuration to wide mode
st.set_page_config(layout="wide")

# Load environment variables
load_dotenv()

# MongoDB connection
mongodb_uri = os.getenv('MONGODB_URI')
mongo_client = MongoClient(mongodb_uri)  # {{ edit_11: Rename MongoDB client to 'mongo_client' }}
db = mongo_client['llm_evaluation_system']
users_collection = db['users']
results_collection = db['evaluation_results']

# Remove or comment out this line if it exists
# openai_client = OpenAI()

# Instead, use the openai_client from runner.py
from runner import openai_client

# Initialize Pinecone
pinecone_client = Pinecone(api_key=os.getenv('PINECONE_API_KEY'))  # {{ edit_13: Initialize Pinecone client using Pinecone class }}

# Initialize the tokenizer
tokenizer: Encoding = tiktoken.get_encoding("cl100k_base")  # This is suitable for GPT-4 and recent models

# Authentication functions
def hash_password(password):
    return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())

def verify_password(password, hashed_password):
    return bcrypt.checkpw(password.encode('utf-8'), hashed_password)

def authenticate(username, password):
    user = users_collection.find_one({"username": username})
    if user and verify_password(password, user['password']):
        return True
    return False

def signup(username, password):
    if users_collection.find_one({"username": username}):
        return False
    hashed_password = hash_password(password)
    # {{ edit_1: Initialize models list for the new user }}
    users_collection.insert_one({
        "username": username,
        "password": hashed_password,
        "models": []  # List to store user's models
    })
    return True
def upload_model(file):
    return "Model uploaded successfully!"

# Function to perform evaluation (placeholder)
def evaluate_model(model_identifier, metrics, username):
    # {{ edit_4: Differentiate between Custom and Named models }}
    user = users_collection.find_one({"username": username})
    models = user.get("models", [])
    selected_model = next((m for m in models if (m['model_name'] == model_identifier) or (m['model_id'] == model_identifier)), None)
    
    if selected_model:
        if selected_model.get("model_type") == "named":
            # For Named Models, use RAG-based evaluation
            return evaluate_named_model(model_identifier, prompt, context_dataset)
        else:
            # For Custom Models, proceed with existing evaluation logic
            results = {metric: round(np.random.rand() * 100, 2) for metric in metrics}
            return results
    else:
        st.error("Selected model not found.")
        return None

# Function to generate response using GPT-4-mini
def generate_response(prompt, context):
    try:
        response = openai_client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": f"Context: {context}\n\nPrompt: {prompt}"}
            ]
        )
        return response.choices[0].message.content
    except Exception as e:
        st.error(f"Error generating response: {str(e)}")
        return None
    
# Add this function to update the context for a model
def update_model_context(username, model_id, context):
    users_collection.update_one(
        {"username": username, "models.model_id": model_id},
        {"$set": {"models.$.context": context}}
    )


# Function to clear the results database
def clear_results_database(username, model_identifier=None):
    try:
        if model_identifier:
            # Clear results for the specific model
            results_collection.delete_many({
                "username": username,
                "$or": [
                    {"model_name": model_identifier},
                    {"model_id": model_identifier}
                ]
            })
        else:
            # Clear all results for the user
            results_collection.delete_many({"username": username})
        return True
    except Exception as e:
        st.error(f"Error clearing results database: {str(e)}")
        return False

# Function to generate embeddings using the specified model
def generate_embedding(text):
    try:
        embedding_response = openai_client.embeddings.create(
            model="text-embedding-3-large",  # {{ edit_3: Use the specified embedding model }}
            input=text,
            encoding_format="float"
        )
        embedding = embedding_response["data"][0]["embedding"]
        return embedding
    except Exception as e:
        st.error(f"Error generating embedding: {str(e)}")
        return None

# Function to handle Named Model Evaluation using RAG
def evaluate_named_model(model_name, prompt, context_dataset):
    # {{ edit_4: Implement evaluation using RAG and Pinecone with the specified embedding model }}
    try:
        # Initialize Pinecone index
        index = pinecone_client.Index(os.getenv('PINECONE_INDEX_NAME'))
        
        # Generate embedding for the prompt
        prompt_embedding = generate_embedding(prompt)
        if not prompt_embedding:
            st.error("Failed to generate embedding for the prompt.")
            return None
        
        # Retrieve relevant context using RAG by querying Pinecone with the embedding
        query_response = index.query(
            top_k=5,
            namespace=model_name,
            include_metadata=True,
            vector=prompt_embedding  # {{ edit_5: Use embedding vector for querying }}
        )
        
        # Aggregate retrieved context
        retrieved_context = " ".join([item['metadata']['text'] for item in query_response['matches']])
        
        # Generate response using the retrieved context
        response = generate_response(prompt, retrieved_context)
        
        # Evaluate the response
        evaluation = teacher_evaluate(prompt, retrieved_context, response)
        
        # Save the results
        save_results(model_name, prompt, retrieved_context, response, evaluation)
        
        return evaluation
    
    except Exception as e:
        st.error(f"Error in evaluating named model: {str(e)}")
        return None

# Example: When indexing data to Pinecone, generate embeddings using the specified model
def index_context_data(model_name, texts):
    try:
        index = pinecone_client.Index(os.getenv('PINECONE_INDEX_NAME'))
        for text in texts:
            embedding = generate_embedding(text)
            if embedding:
                index.upsert([
                    {
                        "id": f"{model_name}_{hash(text)}",
                        "values": embedding,
                        "metadata": {"text": text}
                    }
                ])
    except Exception as e:
        st.error(f"Error indexing data to Pinecone: {str(e)}")
def upload_model(file, username, model_type):
    # {{ edit_5: Modify upload_model to handle model_type }}
    model_id = f"{username}_model_{int(datetime.now().timestamp())}"
    if model_type == "custom":
        # Save the model file as needed
        model_path = os.path.join("models", f"{model_id}.bin")
        with open(model_path, "wb") as f:
            f.write(file.getbuffer())
        
        # Update user's models list
        users_collection.update_one(
            {"username": username},
            {"$push": {"models": {
                "model_id": model_id,
                "file_path": model_path,
                "uploaded_at": datetime.now(),
                "model_type": "custom"
            }}}
        )
        return f"Custom Model {model_id} uploaded successfully!"
    elif model_type == "named":
        # For Named Models, assume the model is managed externally (e.g., via Pinecone)
        users_collection.update_one(
            {"username": username},
            {"$push": {"models": {
                "model_id": model_id,
                "model_name": None,
                "file_path": None,
                "model_link": None,
                "uploaded_at": datetime.now(),
                "model_type": "named"
            }}}
        )
        return f"Named Model {model_id} registered successfully!"
    else:
        return "Invalid model type specified."

# Function to save results to MongoDB
def save_results(username, model, prompt, context, response, evaluation):  # {{ edit_29: Add 'username' parameter }}
    result = {
        "username": username,  # Use the passed 'username' parameter
        "model_id": model['model_id'],  # {{ edit_19: Associate results with 'model_id' }}
        "model_name": model.get('model_name'),
        "model_type": model.get('model_type', 'custom'),  # {{ edit_20: Include 'model_type' in results }}
        "prompt": prompt,
        "context": context,
        "response": response,
        "evaluation": evaluation,
        "timestamp": datetime.now()
    }
    results_collection.insert_one(result)

# Modify the run_custom_evaluations function
def run_custom_evaluations(data, selected_model, username):
    try:
        model_name = selected_model['model_name']
        model_id = selected_model['model_id']
        model_type = selected_model.get('model_type', 'Unknown').lower()
        
        if model_type == 'simple':
            # For simple models, data is already in the correct format
            test_cases = data
        else:
            # For other models, data is split into context_dataset and questions
            context_dataset, questions = data
            test_cases = [
                {
                    "prompt": extract_prompt_text(question),
                    "context": context_dataset,
                    "response": ""  # This will be filled by the model
                }
                for question in questions
            ]
        
        for test_case in test_cases:
            prompt_text = test_case["prompt"]
            context = test_case["context"]
            
            # Get the student model's response using runner.py
            try:
                answer = run_model(model_name, prompt_text)
                if answer is None or answer == "":
                    st.warning(f"No response received from the model for prompt: {prompt_text}")
                    answer = "No response received from the model."
            except Exception as model_error:
                st.error(f"Error running model for prompt: {prompt_text}")
                st.error(f"Error details: {str(model_error)}")
                answer = f"Error: {str(model_error)}"
            
            # Get the teacher's evaluation
            try:
                evaluation = teacher_evaluate(prompt_text, context, answer)
                if evaluation is None:
                    st.warning(f"No evaluation received for prompt: {prompt_text}")
                    evaluation = {"Error": "No evaluation received"}
            except Exception as eval_error:
                st.error(f"Error in teacher evaluation for prompt: {prompt_text}")
                st.error(f"Error details: {str(eval_error)}")
                evaluation = {"Error": str(eval_error)}
            
            # Save the results
            save_results(username, selected_model, prompt_text, context, answer, evaluation)
        
        st.success("Evaluation completed successfully!")
    except Exception as e:
        st.error(f"Error in custom evaluation: {str(e)}")
        st.error(f"Detailed error: {traceback.format_exc()}")

# Function for teacher model evaluation
def teacher_evaluate(prompt, context, response):
    try:
        evaluation_prompt = f"""
        Evaluate the following response based on the given prompt and context. 
        Rate each factor on a scale of 0 to 1, where 1 is the best (or least problematic for negative factors like Hallucination and Bias).
        Please provide scores with two decimal places, and avoid extreme scores of exactly 0 or 1 unless absolutely necessary.

        Context: {context}
        Prompt: {prompt}
        Response: {response}

        Factors to evaluate:
        1. Accuracy: How factually correct is the response?
        2. Hallucination: To what extent does the response contain made-up information? (Higher score means less hallucination)
        3. Groundedness: How well is the response grounded in the given context and prompt?
        4. Relevance: How relevant is the response to the prompt?
        5. Recall: How much of the relevant information from the context is included in the response?
        6. Precision: How precise and focused is the response in addressing the prompt?
        7. Consistency: How consistent is the response with the given information and within itself?
        8. Bias Detection: To what extent is the response free from bias? (Higher score means less bias)

        Provide the evaluation as a JSON object. Each factor should be a key mapping to an object containing 'score' and 'explanation'. 
        Do not include any additional text, explanations, or markdown formatting.
        """

        evaluation_response = openai_client.chat.completions.create(
            model="gpt-4o-mini",  
            messages=[
                {"role": "system", "content": "You are an expert evaluator of language model responses."},
                {"role": "user", "content": evaluation_prompt}
            ]
        )

        content = evaluation_response.choices[0].message.content.strip()

        # Ensure the response starts and ends with curly braces
        if not (content.startswith("{") and content.endswith("}")):
            st.error("Teacher evaluation did not return a valid JSON object.")
            st.error(f"Response content: {content}")
            return None

        try:
            evaluation = json.loads(content)
            return evaluation
        except json.JSONDecodeError as e:
            st.error(f"Error decoding evaluation response: {str(e)}")
            st.error(f"Response content: {content}")
            return None

    except Exception as e:
        st.error(f"Error in teacher evaluation: {str(e)}")
        return None

# Function to generate dummy data for demonstration
def generate_dummy_data():
    dates = pd.date_range(end=datetime.now(), periods=30).tolist()
    metrics = ['Accuracy', 'Precision', 'Recall', 'F1 Score', 'Consistency', 'Bias']
    data = {
        'Date': dates * len(metrics),
        'Metric': [metric for metric in metrics for _ in range(len(dates))],
        'Value': np.random.rand(len(dates) * len(metrics)) * 100
    }
    return pd.DataFrame(data)

# Function to count tokens
def count_tokens(text: str) -> int:
    return len(tokenizer.encode(text))

# Sidebar Navigation
st.sidebar.title("LLM Evaluation System")

# Session state
if 'user' not in st.session_state:
    st.session_state.user = None

# Authentication
if not st.session_state.user:
    auth_option = st.sidebar.radio("Choose an option", ["Login", "Signup"])
    
    username = st.sidebar.text_input("Username")
    password = st.sidebar.text_input("Password", type="password")
    
    if auth_option == "Login":
        if st.sidebar.button("Login"):
            if authenticate(username, password):
                st.session_state.user = username
                st.rerun()
            else:
                st.sidebar.error("Invalid username or password")
    else:
        if st.sidebar.button("Signup"):
            if signup(username, password):
                st.sidebar.success("Signup successful. Please login.")
            else:
                st.sidebar.error("Username already exists")
else:
    st.sidebar.success(f"Welcome, {st.session_state.user}!")
    if st.sidebar.button("Logout"):
        st.session_state.user = None
        st.rerun()



# App content
if st.session_state.user:
    app_mode = st.sidebar.selectbox("Choose the section", ["Dashboard", "Model Upload", "Evaluation", "Prompt Testing", "Manage Models", "History"])  # {{ edit_add: Added "History" to the sidebar navigation }}

    if app_mode == "Dashboard":
        st.title("Dashboard")
        st.write("### Real-time Metrics and Performance Insights")
        
        # Fetch the user from the database
        user = users_collection.find_one({"username": st.session_state.user})
        if user is None:
            st.error("User not found in the database.")
            st.stop()
        user_models = user.get("models", [])
        
        if user_models:
            model_options = [model['model_name'] if model['model_name'] else model['model_id'] for model in user_models]
            selected_model = st.selectbox("Select Model to View Metrics", ["All Models"] + model_options)
            st.session_state['selected_model'] = selected_model  # Store the selected model in session state

            # Add delete dataset button
            if selected_model != "All Models":
                if st.button("Delete Dataset"):
                    if st.session_state['selected_model']:
                        if clear_results_database(st.session_state.user, st.session_state['selected_model']):
                            st.success(f"All evaluation results for {st.session_state['selected_model']} have been deleted.")
                            st.rerun()  # Rerun the app to refresh the dashboard
                        else:
                            st.error("Failed to delete the dataset. Please try again.")
                    else:
                        st.error("No model selected. Please select a model to delete its dataset.")
        else:
            st.error("You have no uploaded models.")
            selected_model = "All Models"
            st.session_state['selected_model'] = selected_model
        
        try:
            query = {"username": st.session_state.user}
            if selected_model != "All Models":
                query["model_name"] = selected_model
                if not selected_model:
                    query = {"username": st.session_state.user, "model_id": selected_model}
            results = list(results_collection.find(query))
            if results:
                df = pd.DataFrame(results)
                
                # Check if required columns exist
                required_columns = ['prompt', 'context', 'response', 'evaluation']
                missing_columns = [col for col in required_columns if col not in df.columns]
                if missing_columns:
                    st.error(f"Error: Missing columns in the data: {', '.join(missing_columns)}")
                    st.error("Please check the database schema and ensure all required fields are present.")
                    st.stop()

                # Extract prompt text if needed
                df['prompt'] = df['prompt'].apply(extract_prompt_text)

                # Safely count tokens for prompt, context, and response
                def safe_count_tokens(text):
                    if isinstance(text, str):
                        return count_tokens(text)
                    else:
                        return 0  # or some default value

                df['prompt_tokens'] = df['prompt'].apply(safe_count_tokens)
                df['context_tokens'] = df['context'].apply(safe_count_tokens)
                df['response_tokens'] = df['response'].apply(safe_count_tokens)
                
                # Calculate total tokens for each row
                df['total_tokens'] = df['prompt_tokens'] + df['context_tokens'] + df['response_tokens']
                
                # Safely extract evaluation metrics
                metrics = ["Accuracy", "Hallucination", "Groundedness", "Relevance", "Recall", "Precision", "Consistency", "Bias Detection"]
                for metric in metrics:
                    df[metric] = df['evaluation'].apply(lambda x: x.get(metric, {}).get('score', 0) if isinstance(x, dict) else 0) * 100

                df['timestamp'] = pd.to_datetime(df['timestamp'])
                df['query_number'] = range(1, len(df) + 1)  # Add query numbers
                
                # Set the threshold for notifications
                notification_threshold = st.slider("Set Performance Threshold for Notifications (%)", min_value=0, max_value=100, value=50)

                # Define the metrics to check
                metrics_to_check = metrics  # Or allow the user to select specific metrics

                # Check for evaluations where any of the metrics are below the threshold
                low_performance_mask = df[metrics_to_check].lt(notification_threshold).any(axis=1)
                low_performing_evaluations = df[low_performance_mask]

                # Display Notifications
                if not low_performing_evaluations.empty:
                    st.warning(f"⚠️ You have {len(low_performing_evaluations)} evaluations with metrics below {notification_threshold}%.")
                    with st.expander("View Low-Performing Evaluations"):
                        # Display the low-performing evaluations in a table
                        display_columns = ['timestamp', 'model_name', 'prompt', 'response'] + metrics_to_check
                        low_perf_display_df = low_performing_evaluations[display_columns].copy()
                        low_perf_display_df['timestamp'] = low_perf_display_df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S')
                        
                        # Apply styling to highlight low scores
                        def highlight_low_scores(val):
                            if isinstance(val, float):
                                if val < notification_threshold:
                                    return 'background-color: red; color: white'
                            return ''
                        
                        styled_low_perf_df = low_perf_display_df.style.applymap(highlight_low_scores, subset=metrics_to_check)
                        styled_low_perf_df = styled_low_perf_df.format({metric: "{:.2f}%" for metric in metrics_to_check})
                        
                        st.dataframe(
                            styled_low_perf_df.set_properties(**{
                                'text-align': 'left',
                                'border': '1px solid #ddd'
                            }).set_table_styles([
                                {'selector': 'th', 'props': [('background-color', '#333'), ('color', 'white')]},
                                {'selector': 'td', 'props': [('vertical-align', 'top')]}
                            ]), 
                            use_container_width=True
                        )
                else:
                    st.success("🎉 All your evaluations have metrics above the threshold!")

                @st.cache_data
                def create_metrics_graph(df, metrics):
                    fig = px.line(
                        df, 
                        x='query_number',  # Use query numbers on x-axis
                        y=metrics, 
                        title='Metrics Over Queries',
                        labels={metric: f"{metric} (%)" for metric in metrics},
                        markers=True,
                        template='plotly_dark',
                    )
                    color_discrete_sequence = px.colors.qualitative.Dark24
                    for i, metric in enumerate(metrics):
                        fig.data[i].line.color = color_discrete_sequence[i % len(color_discrete_sequence)]
                        fig.data[i].marker.color = color_discrete_sequence[i % len(color_discrete_sequence)]
                    fig.update_layout(
                        xaxis_title="Query Number",
                        yaxis_title="Metric Score (%)",
                        legend_title="Metrics",
                        hovermode="x unified",
                        margin=dict(l=50, r=50, t=100, b=50),
                        height=700  # Increase the height of the graph
                    )
                    return fig
                
                fig = create_metrics_graph(df, metrics)

                st.plotly_chart(fig, use_container_width=True)

                # Latest Metrics
                st.subheader("Latest Metrics")
                latest_metrics = df[metrics].mean()  # Calculate the average of all metrics

                cols = st.columns(4)
                for i, (metric, value) in enumerate(latest_metrics.items()):
                    with cols[i % 4]:
                        color = 'green' if value >= 75 else 'orange' if value >= 50 else 'red'
                        st.metric(label=metric, value=f"{value:.2f}%", delta=None)
                        st.progress(value / 100)

                # Add an explanation for the metrics
                st.info("These metrics represent the average scores across all evaluations.")

                # Detailed Data View
                st.subheader("Detailed Data View")

                # Calculate aggregate metrics
                total_spans = len(df)
                total_tokens = df['total_tokens'].sum()

                # Display aggregate metrics
                col1, col2 = st.columns(2)
                with col1:
                    st.metric("Total Spans", f"{total_spans:,}")
                with col2:
                    st.metric("Total Tokens", f"{total_tokens:,.2f}M" if total_tokens >= 1e6 else f"{total_tokens:,}")

                # Prepare the data for display
                display_data = []
                for _, row in df.iterrows():
                    prompt_text = extract_prompt_text(row.get('prompt', ''))
                    display_row = {
                        "Prompt": prompt_text[:50] + "..." if prompt_text else "N/A",
                        "Context": str(row.get('context', ''))[:50] + "..." if row.get('context') else "N/A",
                        "Response": str(row.get('response', ''))[:50] + "..." if row.get('response') else "N/A",
                    }
                    # Add metrics to the display row
                    for metric in metrics:
                        display_row[metric] = row.get(metric, 0)  # Use get() with a default value
                    
                    display_data.append(display_row)

                # Convert to DataFrame for easy display
                display_df = pd.DataFrame(display_data)

                # Function to color cells based on score
                def color_cells(val):
                    if isinstance(val, float):
                        if val >= 80:
                            color = 'green'
                        elif val >= 60:
                            color = '#90EE90'  # Light green
                        else:
                            color = 'red'
                        return f'background-color: {color}; color: black'
                    return ''

                # Apply the styling only to metric columns
                styled_df = display_df.style.applymap(color_cells, subset=metrics)

                # Format metric columns as percentages
                for metric in metrics:
                    styled_df = styled_df.format({metric: "{:.2f}%"})

                # Display the table with custom styling
                st.dataframe(
                    styled_df.set_properties(**{
                        'color': 'white',
                        'border': '1px solid #ddd'
                    }).set_table_styles([
                        {'selector': 'th', 'props': [('background-color', '#4CAF50'), ('color', 'white')]},
                        {'selector': 'td', 'props': [('text-align', 'left')]},
                        # Keep background white for non-metric columns
                        {'selector': 'td:nth-child(-n+3)', 'props': [('background-color', 'white !important')]}
                    ]), 
                    use_container_width=True,
                    height=400  # Set a fixed height with scrolling
                )
                
                # UMAP Visualization with Clustering
                st.subheader("UMAP Visualization with Clustering")

                if len(df) > 2:
                    # Allow user to select metrics to include
                    metrics = ['Accuracy', 'Hallucination', 'Groundedness', 'Relevance', 'Recall', 'Precision', 'Consistency', 'Bias Detection']
                    selected_metrics = st.multiselect("Select Metrics to Include in UMAP", metrics, default=metrics)

                    if len(selected_metrics) < 2:
                        st.warning("Please select at least two metrics for UMAP.")
                    else:
                        # Allow user to select number of dimensions
                        n_components = st.radio("Select UMAP Dimensions", [2, 3], index=1)

                        # Allow user to adjust UMAP parameters
                        n_neighbors = st.slider("n_neighbors", min_value=2, max_value=50, value=15)
                        min_dist = st.slider("min_dist", min_value=0.0, max_value=1.0, value=0.1, step=0.01)

                        # Prepare data for UMAP
                        X = df[selected_metrics].values

                        # Normalize the data
                        scaler = StandardScaler()
                        X_scaled = scaler.fit_transform(X)

                        # Perform UMAP dimensionality reduction
                        reducer = umap.UMAP(n_neighbors=n_neighbors, min_dist=min_dist, n_components=n_components, random_state=42)
                        embedding = reducer.fit_transform(X_scaled)

                        # Allow user to select the number of clusters
                        num_clusters = st.slider("Select Number of Clusters", min_value=2, max_value=10, value=3)

                        # Perform KMeans clustering on the UMAP embeddings
                        kmeans = KMeans(n_clusters=num_clusters, random_state=42)
                        cluster_labels = kmeans.fit_predict(embedding)

                        # Create a DataFrame with the UMAP results and cluster labels
                        umap_columns = [f'UMAP{i+1}' for i in range(n_components)]
                        umap_data = {col: embedding[:, idx] for idx, col in enumerate(umap_columns)}
                        umap_data['Cluster'] = cluster_labels
                        umap_data['Model'] = df['model_name']
                        umap_data['Prompt'] = df['prompt']
                        umap_data['Response'] = df['response']
                        umap_data['Timestamp'] = df['timestamp']
                        umap_df = pd.DataFrame(umap_data)

                        # Include selected metrics in umap_df for hover info
                        for metric in selected_metrics:
                            umap_df[metric] = df[metric]

                        # Prepare customdata for hovertemplate
                        customdata_columns = ['Model', 'Prompt', 'Cluster'] + selected_metrics
                        umap_df['customdata'] = umap_df[customdata_columns].values.tolist()

                        # Build hovertemplate
                        hovertemplate = '<b>Model:</b> %{customdata[0]}<br>' + \
                                        '<b>Prompt:</b> %{customdata[1]}<br>' + \
                                        '<b>Cluster:</b> %{customdata[2]}<br>'
                        for idx, metric in enumerate(selected_metrics):
                            hovertemplate += f'<b>{metric}:</b> %{{customdata[{idx+3}]:.2f}}<br>'
                        hovertemplate += '<extra></extra>'  # Hide trace info

                        # Define color palette for clusters
                        cluster_colors = plc.qualitative.Plotly
                        num_colors = len(cluster_colors)
                        if num_clusters > num_colors:
                            cluster_colors = plc.sample_colorscale('Rainbow', [n/(num_clusters-1) for n in range(num_clusters)])
                        else:
                            cluster_colors = cluster_colors[:num_clusters]

                        # Map cluster labels to colors
                        cluster_color_map = {label: color for label, color in zip(range(num_clusters), cluster_colors)}
                        umap_df['Color'] = umap_df['Cluster'].map(cluster_color_map)

                        # Create the UMAP plot
                        if n_components == 3:
                            # 3D plot
                            fig = go.Figure()

                            for cluster_label in sorted(umap_df['Cluster'].unique()):
                                cluster_data = umap_df[umap_df['Cluster'] == cluster_label]
                                fig.add_trace(go.Scatter3d(
                                    x=cluster_data['UMAP1'],
                                    y=cluster_data['UMAP2'],
                                    z=cluster_data['UMAP3'],
                                    mode='markers',
                                    name=f'Cluster {cluster_label}',
                                    marker=dict(
                                        size=5,
                                        color=cluster_data['Color'],  # Color according to cluster
                                        opacity=0.8,
                                        line=dict(width=0.5, color='white')
                                    ),
                                    customdata=cluster_data['customdata'],
                                    hovertemplate=hovertemplate
                                ))

                            fig.update_layout(
                                title='3D UMAP Visualization with Clustering',
                                scene=dict(
                                    xaxis_title='UMAP Dimension 1',
                                    yaxis_title='UMAP Dimension 2',
                                    zaxis_title='UMAP Dimension 3'
                                ),
                                hovermode='closest',
                                template='plotly_dark',
                                height=800,
                                legend_title='Clusters'
                            )
                            st.plotly_chart(fig, use_container_width=True)
                        else:
                            # 2D plot
                            fig = go.Figure()

                            for cluster_label in sorted(umap_df['Cluster'].unique()):
                                cluster_data = umap_df[umap_df['Cluster'] == cluster_label]
                                fig.add_trace(go.Scatter(
                                    x=cluster_data['UMAP1'],
                                    y=cluster_data['UMAP2'],
                                    mode='markers',
                                    name=f'Cluster {cluster_label}',
                                    marker=dict(
                                        size=8,
                                        color=cluster_data['Color'],  # Color according to cluster
                                        opacity=0.8,
                                        line=dict(width=0.5, color='white')
                                    ),
                                    customdata=cluster_data['customdata'],
                                    hovertemplate=hovertemplate
                                ))

                            fig.update_layout(
                                title='2D UMAP Visualization with Clustering',
                                xaxis_title='UMAP Dimension 1',
                                yaxis_title='UMAP Dimension 2',
                                hovermode='closest',
                                template='plotly_dark',
                                height=800,
                                legend_title='Clusters'
                            )
                            st.plotly_chart(fig, use_container_width=True)

                        # Selectable Data Points
                        st.subheader("Cluster Analysis")

                        # Show cluster counts
                        cluster_counts = umap_df['Cluster'].value_counts().sort_index().reset_index()
                        cluster_counts.columns = ['Cluster', 'Number of Points']
                        st.write("### Cluster Summary")
                        st.dataframe(cluster_counts)

                        # Allow user to select clusters to view details
                        selected_clusters = st.multiselect("Select Clusters to View Details", options=sorted(umap_df['Cluster'].unique()), default=sorted(umap_df['Cluster'].unique()))

                        if selected_clusters:
                            selected_data = umap_df[umap_df['Cluster'].isin(selected_clusters)]
                            st.write("### Details of Selected Clusters")
                            st.dataframe(selected_data[['Model', 'Prompt', 'Response', 'Cluster'] + selected_metrics])
                        else:
                            st.info("Select clusters to view their details.")

                        st.info("""
                        **UMAP Visualization with Clustering**

                        This visualization includes clustering of the evaluation data points in the UMAP space.

                        **Features:**

                        - **Clustering Algorithm**: KMeans clustering is applied on the UMAP embeddings.
                        - **Cluster Selection**: Choose the number of clusters to identify patterns in the data.
                        - **Color Coding**: Each cluster is represented by a distinct color in the plot.
                        - **Interactive Exploration**: Hover over points to see detailed information, including the cluster label.
                        - **Cluster Analysis**: View summary statistics and details of selected clusters.

                        **Instructions:**

                        - **Select Metrics**: Choose which evaluation metrics to include in the UMAP calculation.
                        - **Adjust UMAP Parameters**: Fine-tune `n_neighbors` and `min_dist` for clustering granularity.
                        - **Choose Number of Clusters**: Use the slider to set how many clusters to identify.
                        - **Interact with the Plot**: Hover and click on clusters to explore data points.

                        **Interpreting Clusters:**

                        - **Cluster Composition**: Clusters group evaluations with similar metric profiles.
                        - **Model Performance**: Analyze clusters to identify strengths and weaknesses of models.
                        - **Data Patterns**: Use clustering to uncover hidden patterns in your evaluation data.

                        **Tips:**

                        - Experiment with different numbers of clusters to find meaningful groupings.
                        - Adjust UMAP parameters to see how the clustering changes with different embeddings.
                        - Use the cluster details to investigate specific evaluations and prompts.

                        Enjoy exploring your evaluation data with clustering!
                        """)
                else:
                    st.info("Not enough data for UMAP visualization. Please run more evaluations.")

                # Worst Performing Slice Analysis
                st.subheader("Worst Performing Slice Analysis")

                # Allow the user to select metrics to analyze
                metrics = ['Accuracy', 'Hallucination', 'Groundedness', 'Relevance', 'Recall', 'Precision', 'Consistency', 'Bias Detection']
                selected_metrics = st.multiselect("Select Metrics to Analyze", metrics, default=metrics)

                if selected_metrics:
                    # Set a threshold for "poor performance"
                    threshold = st.slider("Performance Threshold (%)", min_value=0, max_value=100, value=50)

                    # Filter data where any of the selected metrics are below the threshold
                    mask = df[selected_metrics].lt(threshold).any(axis=1)
                    worst_performing_df = df[mask]

                    if not worst_performing_df.empty:
                        st.write(f"Found {len(worst_performing_df)} evaluations below the threshold of {threshold}% in the selected metrics.")

                        # Display the worst-performing prompts and their metrics
                        st.write("### Worst Performing Evaluations")
                        display_columns = ['prompt', 'response'] + selected_metrics + ['timestamp']
                        worst_performing_display_df = worst_performing_df[display_columns].copy()
                        worst_performing_display_df['timestamp'] = worst_performing_display_df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S')
                        
                        # Apply styling to highlight low scores
                        def highlight_low_scores(val):
                            if isinstance(val, float):
                                if val < threshold:
                                    return 'background-color: red; color: white'
                            return ''
                        
                        styled_worst_df = worst_performing_display_df.style.applymap(highlight_low_scores, subset=selected_metrics)
                        styled_worst_df = styled_worst_df.format({metric: "{:.2f}%" for metric in selected_metrics})

                        st.dataframe(
                            styled_worst_df.set_properties(**{
                                'text-align': 'left',
                                'border': '1px solid #ddd'
                            }).set_table_styles([
                                {'selector': 'th', 'props': [('background-color', '#333'), ('color', 'white')]},
                                {'selector': 'td', 'props': [('vertical-align', 'top')]}
                            ]), 
                            use_container_width=True
                        )

                        # Analyze the worst-performing slices based on prompt characteristics
                        st.write("### Analysis by Prompt Length")

                        # Add a column for prompt length
                        worst_performing_df['Prompt Length'] = worst_performing_df['prompt'].apply(lambda x: len(x.split()))

                        # Define bins for prompt length ranges
                        bins = [0, 5, 10, 20, 50, 100, 1000]
                        labels = ['0-5', '6-10', '11-20', '21-50', '51-100', '100+']
                        worst_performing_df['Prompt Length Range'] = pd.cut(worst_performing_df['Prompt Length'], bins=bins, labels=labels, right=False)

                        # Group by 'Prompt Length Range' and calculate average metrics
                        group_metrics = worst_performing_df.groupby('Prompt Length Range')[selected_metrics].mean().reset_index()

                        # Display the average metrics per prompt length range
                        st.write("#### Average Metrics per Prompt Length Range")
                        group_metrics = group_metrics.sort_values('Prompt Length Range')
                        st.dataframe(group_metrics.style.format({metric: "{:.2f}%" for metric in selected_metrics}))

                        # Visualization of average metrics per prompt length range
                        st.write("#### Visualization of Metrics by Prompt Length Range")
                        melted_group_metrics = group_metrics.melt(id_vars='Prompt Length Range', value_vars=selected_metrics, var_name='Metric', value_name='Average Score')
                        fig = px.bar(
                            melted_group_metrics, 
                            x='Prompt Length Range', 
                            y='Average Score', 
                            color='Metric', 
                            barmode='group',
                            title='Average Metric Scores by Prompt Length Range',
                            labels={'Average Score': 'Average Score (%)'},
                            height=600
                        )
                        st.plotly_chart(fig, use_container_width=True)

                        # Further analysis: show counts of worst-performing evaluations per model
                        st.write("### Worst Performing Evaluations per Model")
                        model_counts = worst_performing_df['model_name'].value_counts().reset_index()
                        model_counts.columns = ['Model Name', 'Count of Worst Evaluations']
                        st.dataframe(model_counts)

                        # Allow user to download the worst-performing data
                        csv = worst_performing_df.to_csv(index=False)
                        st.download_button(
                            label="Download Worst Performing Data as CSV",
                            data=csv,
                            file_name='worst_performing_data.csv',
                            mime='text/csv',
                        )
                    else:
                        st.info("No evaluations found below the specified threshold.")
                else:
                    st.warning("Please select at least one metric to analyze.")

            else:
                st.info("No evaluation results available for the selected model.")
        except Exception as e:
            st.error(f"Error processing data from database: {str(e)}")
            st.error("Detailed error information:")
            st.error(traceback.format_exc())
            st.stop()

    elif app_mode == "Model Upload":
        st.title("Upload Your Model")
        model_type = st.radio("Select Model Type", ["Custom", "Named"])  # {{ edit_6: Select model type }}
        uploaded_file = st.file_uploader("Choose a model file", type=[".pt", ".h5", ".bin"]) if model_type == "custom" else None
        
        if st.button("Upload Model"):
            if model_type == "custom" and uploaded_file is not None:
                result = upload_model(uploaded_file, st.session_state.user, model_type="custom")
                st.success(result)
            elif model_type == "named":
                result = upload_model(None, st.session_state.user, model_type="named")
                st.success(result)
            else:
                st.error("Please upload a valid model file for Custom models.")

    elif app_mode == "Evaluation":
        st.title("Evaluate Your Model")
        st.write("### Select Model and Evaluation Metrics")
        
        # Fetch the user from the database
        user = users_collection.find_one({"username": st.session_state.user})
        if user is None:
            st.error("User not found in the database.")
            st.stop()
        user_models = user.get("models", [])
        
        if not user_models:
            st.error("You have no uploaded models. Please upload a model first.")
        else:
            # {{ edit_1: Display model_name instead of model_id }}
            model_identifier = st.selectbox(
                "Choose a Model to Evaluate",
                [model['model_name'] if model['model_name'] else model['model_id'] for model in user_models]
            )
            
            # {{ edit_2: Remove metrics selection and set fixed metrics }}
            fixed_metrics = ["Accuracy", "Hallucination", "Groundedness", "Relevance", "Recall", "Precision", "Consistency", "Bias Detection"]
            st.write("### Evaluation Metrics")
            st.write(", ".join(fixed_metrics))
            
            # Modify the evaluation function call to use fixed_metrics
            if st.button("Start Evaluation"):
                with st.spinner("Evaluation in progress..."):
                    # {{ edit_3: Use fixed_metrics instead of user-selected metrics }}
                    results = evaluate_model(model_identifier, fixed_metrics, st.session_state.user)
                    # Fetch the current model document
                    current_model = next((m for m in user_models if (m['model_name'] == model_identifier) or (m['model_id'] == model_identifier)), None)
                    if current_model:
                        save_results(st.session_state.user, current_model, prompt, context, response, results)  # {{ edit_21: Pass current_model to save_results }}
                        st.success("Evaluation Completed!")
                        st.json(results)
                    else:
                        st.error("Selected model not found.")

    elif app_mode == "Prompt Testing":
        st.title("Prompt Testing")
        
        model_selection_option = st.radio("Select Model Option:", ["Choose Existing Model", "Add New Model"])
        
        if model_selection_option == "Choose Existing Model":
            user = users_collection.find_one({"username": st.session_state.user})
            user_models = user.get("models", [])
            
            if not user_models:
                st.error("You have no uploaded models. Please upload a model first.")
            else:
                model_options = [
                    f"{model['model_name']} ({model.get('model_type', 'Unknown').capitalize()})" 
                    for model in user_models
                ]
                selected_model = st.selectbox("Select a Model for Testing", model_options)
                
                model_name = selected_model.split(" (")[0]
                model_type = selected_model.split(" (")[1].rstrip(")")
        else:
            # Code for adding a new model (unchanged)
            ...

        st.subheader("Input for Model Testing")
        
        # For simple models, we'll use a single JSON file
        if model_type.lower() == "simple":
            st.write("For simple models, please upload a single JSON file containing prompts, contexts, and responses.")
            json_file = st.file_uploader("Upload Test Data JSON", type=["json"])
            
            if json_file is not None:
                try:
                    test_data = json.load(json_file)
                    st.success("Test data JSON file uploaded successfully!")
                    
                    # Display a preview of the test data
                    st.write("Preview of test data:")
                    st.json(test_data[:3] if len(test_data) > 3 else test_data)
                    
                except json.JSONDecodeError:
                    st.error("Invalid JSON format. Please check your file.")
            else:
                test_data = None
        else:
            # For other model types, keep the existing separate inputs for context and questions
            context_input_method = st.radio("Choose context input method:", ["Text Input", "File Upload"])
            if context_input_method == "Text Input":
                context_dataset = st.text_area("Enter Context Dataset (txt):", height=200)
            else:
                context_file = st.file_uploader("Upload Context Dataset", type=["txt"])
                if context_file is not None:
                    context_dataset = context_file.getvalue().decode("utf-8")
                    st.success("Context file uploaded successfully!")
                else:
                    context_dataset = None

            questions_input_method = st.radio("Choose questions input method:", ["Text Input", "File Upload"])
            if questions_input_method == "Text Input":
                questions_json = st.text_area("Enter Questions (JSON format):", height=200)
            else:
                questions_file = st.file_uploader("Upload Questions JSON", type=["json"])
                if questions_file is not None:
                    questions_json = questions_file.getvalue().decode("utf-8")
                    st.success("Questions file uploaded successfully!")
                else:
                    questions_json = None
        
        if st.button("Run Test"):
            if not model_name:
                st.error("Please select or add a valid Model.")
            elif model_type.lower() == "simple" and test_data is None:
                st.error("Please upload a valid test data JSON file.")
            elif model_type.lower() != "simple" and (not context_dataset or not questions_json):
                st.error("Please provide both context dataset and questions JSON.")
            else:
                try:
                    selected_model = next(
                        (m for m in user_models if m['model_name'] == model_name),
                        None
                    )
                    if selected_model:
                        with st.spinner("Starting evaluations..."):
                            if model_type.lower() == "simple":
                                evaluation_thread = threading.Thread(
                                    target=run_custom_evaluations, 
                                    args=(test_data, selected_model, st.session_state.user)
                                )
                            else:
                                questions = json.loads(questions_json)
                                evaluation_thread = threading.Thread(
                                    target=run_custom_evaluations, 
                                    args=((context_dataset, questions), selected_model, st.session_state.user)
                                )
                            evaluation_thread.start()
                            st.success("Evaluations are running in the background. You can navigate away or close the site.")
                    else:
                        st.error("Selected model not found.")
                except json.JSONDecodeError:
                    st.error("Invalid JSON format. Please check your input.")

    elif app_mode == "Manage Models":
        st.title("Manage Your Models")
        # Fetch the user from the database
        user = users_collection.find_one({"username": st.session_state.user})
        if user is None:
            st.error("User not found in the database.")
            st.stop()
        user_models = user.get("models", [])
        
        # Update existing models to ensure they have a model_type
        for model in user_models:
            if 'model_type' not in model:
                model['model_type'] = 'simple'  # Default to 'simple' for existing models
        users_collection.update_one(
            {"username": st.session_state.user},
            {"$set": {"models": user_models}}
        )
        
        st.subheader("Add a New Model")
        model_type = st.radio("Select Model Type:", ["Simple Model", "Custom Model"])
        
        if model_type == "Simple Model":
            new_model_name = st.text_input("Enter New Model Name:")
            if st.button("Add Simple Model") or st.button("Add Custom Model"):
                if new_model_name or selected_custom_model:
                    model_id = f"{st.session_state.user}_model_{int(datetime.now().timestamp())}"
                    model_data = {
                        "model_id": model_id,
                        "model_name": new_model_name if model_type == "Simple Model" else selected_custom_model,
                        "model_type": "simple" if model_type == "Simple Model" else "custom",
                        "file_path": None,
                        "model_link": None,
                        "uploaded_at": datetime.now(),
                        "context": None  # We'll update this when running evaluations
                    }
                    users_collection.update_one(
                        {"username": st.session_state.user},
                        {"$push": {"models": model_data}}
                    )
                    st.success(f"Model '{model_data['model_name']}' added successfully as {model_id}!")
                else:
                    st.error("Please enter a valid model name or select a custom model.")
        
        else:  # Custom Model
            custom_model_options = ["gpt-4o", "gpt-4o-mini"]
            selected_custom_model = st.selectbox("Select Custom Model:", custom_model_options)
            
            if st.button("Add Custom Model"):
                model_id = f"{st.session_state.user}_model_{int(datetime.now().timestamp())}"
                users_collection.update_one(
                    {"username": st.session_state.user},
                    {"$push": {"models": {
                        "model_id": model_id,
                        "model_name": selected_custom_model,
                        "model_type": "custom",
                        "file_path": None,
                        "model_link": None,
                        "uploaded_at": datetime.now()
                    }}}
                )
                st.success(f"Custom Model '{selected_custom_model}' added successfully as {model_id}!")
        
        st.markdown("---")
        
        if user_models:
            st.subheader("Your Models")
            for model in user_models:
                st.markdown(f"**Model ID:** {model['model_id']}")
                st.write(f"**Model Type:** {model.get('model_type', 'simple').capitalize()}")
                if model.get("model_name"):
                    st.write(f"**Model Name:** {model['model_name']}")
                if model.get("file_path"):
                    st.write(f"**File Path:** {model['file_path']}")
                st.write(f"**Uploaded at:** {model['uploaded_at']}")
                
                # Add delete option
                if st.button(f"Delete {model['model_id']}"):
                    # Delete the model file if exists and it's a Custom model
                    if model['file_path'] and os.path.exists(model['file_path']):
                        os.remove(model['file_path'])
                    # Remove model from user's models list
                    users_collection.update_one(
                        {"username": st.session_state.user},
                        {"$pull": {"models": {"model_id": model['model_id']}}}
                    )
                    st.success(f"Model {model['model_id']} deleted successfully!")
        else:
            st.info("You have no uploaded models.")

    elif app_mode == "History":  # {{ edit_add: Enhanced History UI }}
        st.title("History")
        st.write("### Your Evaluation History")
        
        try:
            # Fetch all evaluation results for the current user from MongoDB
            user_results = list(results_collection.find({"username": st.session_state.user}).sort("timestamp", -1))
            
            if user_results:
                # Convert results to a pandas DataFrame
                df = pd.DataFrame(user_results)
                
                # Extract prompt text using the helper function
                df['prompt'] = df['prompt'].apply(extract_prompt_text)
                
                # Normalize the evaluation JSON into separate columns
                eval_df = df['evaluation'].apply(pd.Series)
                for metric in ["Accuracy", "Hallucination", "Groundedness", "Relevance", "Recall", "Precision", "Consistency", "Bias Detection"]:
                    if metric in eval_df.columns:
                        df[metric + " Score"] = eval_df[metric].apply(lambda x: x.get('score', 0) * 100 if isinstance(x, dict) else 0)
                        df[metric + " Explanation"] = eval_df[metric].apply(lambda x: x.get('explanation', '') if isinstance(x, dict) else '')
                    else:
                        df[metric + " Score"] = 0
                        df[metric + " Explanation"] = ""
                
                # Select relevant columns to display
                display_df = df[[
                    "timestamp", "model_name", "prompt", "context", "response", 
                    "Accuracy Score", "Hallucination Score", "Groundedness Score",
                    "Relevance Score", "Recall Score", "Precision Score",
                    "Consistency Score", "Bias Detection Score"
                ]]
                
                # Rename columns for better readability
                display_df = display_df.rename(columns={
                    "timestamp": "Timestamp",
                    "model_name": "Model Name",
                    "prompt": "Prompt",
                    "context": "Context",
                    "response": "Response",
                    "Accuracy Score": "Accuracy (%)",
                    "Hallucination Score": "Hallucination (%)",
                    "Groundedness Score": "Groundedness (%)",
                    "Relevance Score": "Relevance (%)",
                    "Recall Score": "Recall (%)",
                    "Precision Score": "Precision (%)",
                    "Consistency Score": "Consistency (%)",
                    "Bias Detection Score": "Bias Detection (%)"
                })
                
                # Convert timestamp to a readable format
                display_df['Timestamp'] = pd.to_datetime(display_df['Timestamp']).dt.strftime('%Y-%m-%d %H:%M:%S')
                
                st.subheader("Evaluation Results")
                
                # Display the DataFrame with enhanced styling
                st.dataframe(
                    display_df.style.set_properties(**{
                        'background-color': '#f0f8ff',
                        'color': '#333',
                        'border': '1px solid #ddd'
                    }).set_table_styles([
                        {'selector': 'th', 'props': [('background-color', '#f5f5f5'), ('text-align', 'center')]},
                        {'selector': 'td', 'props': [('text-align', 'left'), ('vertical-align', 'top')]}
                    ]).format({
                        "Accuracy (%)": "{:.2f}",
                        "Hallucination (%)": "{:.2f}",
                        "Groundedness (%)": "{:.2f}",
                        "Relevance (%)": "{:.2f}",
                        "Recall (%)": "{:.2f}",
                        "Precision (%)": "{:.2f}",
                        "Consistency (%)": "{:.2f}",
                        "Bias Detection (%)": "{:.2f}"
                    }), use_container_width=True
                )
                
            else:
                st.info("You have no evaluation history yet.")
        
        except Exception as e:
            st.error(f"Error fetching history data: {e}")

# Add a footer
st.sidebar.markdown("---")
st.sidebar.info("LLM Evaluation System - v0.2")