File size: 43,916 Bytes
47a776e
 
 
 
 
 
 
 
 
 
 
 
 
6f7b9d9
 
47a776e
a88fc03
 
 
 
 
6f7b9d9
ffa453a
 
 
 
 
47a776e
 
 
 
 
 
 
a88fc03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f7b9d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a776e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7d6236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f7b9d9
 
 
 
 
 
 
 
 
 
 
e7d6236
 
 
 
 
 
6f7b9d9
 
 
 
 
 
e7d6236
6f7b9d9
 
e7d6236
6f7b9d9
e7d6236
 
 
 
 
 
 
 
6f7b9d9
 
e7d6236
6f7b9d9
e7d6236
 
6f7b9d9
47a776e
6f7b9d9
 
 
 
 
 
 
 
 
 
47a776e
 
 
6f7b9d9
 
 
 
 
a88fc03
 
bc26371
6f7b9d9
 
 
 
 
47a776e
37477f9
47a776e
37477f9
 
 
 
 
 
 
 
 
 
 
 
6f7b9d9
37477f9
 
 
 
 
 
 
 
6f7b9d9
37477f9
 
 
 
 
6f7b9d9
37477f9
 
 
6f7b9d9
e7d6236
 
 
 
6f7b9d9
e7d6236
 
 
6f7b9d9
e7d6236
 
 
 
6f7b9d9
e7d6236
6f7b9d9
e7d6236
 
6f7b9d9
e7d6236
 
 
 
6f7b9d9
e7d6236
 
 
 
 
 
 
 
 
6f7b9d9
e7d6236
6f7b9d9
e7d6236
 
6f7b9d9
e7d6236
 
 
 
 
 
 
 
 
 
6f7b9d9
e7d6236
 
 
 
 
6f7b9d9
e7d6236
 
 
 
 
 
37477f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f96986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37477f9
e7d6236
d715357
6665ca1
 
 
 
 
d715357
 
 
37477f9
d715357
 
 
37477f9
d715357
37477f9
 
 
 
 
6665ca1
d715357
37477f9
d715357
37477f9
 
d715357
 
6665ca1
37477f9
 
 
 
 
 
 
 
 
 
 
 
 
6665ca1
37477f9
e7d6236
37477f9
e7d6236
37477f9
d715357
37477f9
 
 
 
 
 
 
6665ca1
37477f9
 
 
 
 
 
 
 
d715357
 
37477f9
d715357
 
37477f9
47a776e
d715357
37477f9
d715357
6665ca1
37477f9
c650a86
 
 
 
 
37477f9
c650a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37477f9
c650a86
 
 
47a776e
c650a86
71f7801
 
 
 
 
 
 
c650a86
71f7801
 
 
 
 
 
 
 
 
 
 
 
 
c650a86
71f7801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c650a86
 
 
47a776e
c650a86
 
 
 
 
 
 
 
 
 
 
 
6f7b9d9
c650a86
 
 
 
 
 
 
 
 
 
 
 
47a776e
c650a86
 
47a776e
c650a86
 
 
 
 
 
 
47a776e
c650a86
 
 
 
 
47a776e
 
c650a86
47a776e
 
c650a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ecfcf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7d6236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b8a800
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a776e
 
7ecfcf2
 
 
 
47a776e
6f7b9d9
47a776e
 
6665ca1
 
 
 
 
 
47a776e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba2d700
 
 
47a776e
 
 
 
 
 
 
 
ba2d700
9b8a800
6665ca1
6f7b9d9
e7d6236
 
 
 
 
 
ba2d700
e7d6236
 
47a776e
6f7b9d9
47a776e
 
 
9b8a800
 
 
 
 
47a776e
9b8a800
 
47a776e
6665ca1
 
 
 
47a776e
6665ca1
 
47a776e
6665ca1
 
 
 
e7d6236
 
 
9b8a800
 
 
 
 
 
 
6665ca1
 
 
47a776e
e7d6236
47a776e
e7d6236
ba2d700
6665ca1
47a776e
ba2d700
47a776e
ba2d700
 
 
47a776e
 
6f7b9d9
47a776e
ba2d700
47a776e
 
 
e7d6236
ba2d700
47a776e
e7d6236
 
 
 
 
 
 
 
 
 
 
 
ba2d700
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
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
import os
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

import streamlit as st
import torch
import torch.nn.functional as F
import re
import requests
from embedding_processor import SentenceTransformerRetriever, process_data
import pickle
import logging
import sys
from llama_cpp import Llama
from tqdm import tqdm

# At the top of your script
os.environ['LLAMA_CPP_THREADS'] = '4'
os.environ['LLAMA_CPP_BATCH_SIZE'] = '512'
os.environ['LLAMA_CPP_MODEL_PATH'] = os.path.join("models", "mistral-7b-v0.1.Q4_K_M.gguf")

# Set page config first
st.set_page_config(
    page_title="The Sport Chatbot",
    page_icon="πŸ†",
    layout="wide"
)

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[logging.StreamHandler(sys.stdout)]
)
# Add this at the top level of your script, after imports
@st.cache_resource
def get_llama_model():
    model_path = os.path.join("models", "mistral-7b-v0.1.Q4_K_M.gguf")
    os.makedirs(os.path.dirname(model_path), exist_ok=True)
    
    if not os.path.exists(model_path):
        st.info("Downloading model... This may take a while.")
        direct_url = "https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q4_K_M.gguf"
        download_file_with_progress(direct_url, model_path)

    llm_config = {
        "model_path": model_path,
        "n_ctx": 2048,
        "n_threads": 4,
        "n_batch": 512,
        "n_gpu_layers": 0,
        "verbose": False,
        "use_mlock": True
    }
    
    return Llama(**llm_config)
def download_file_with_progress(url: str, filename: str):
    """Download a file with progress bar using requests"""
    response = requests.get(url, stream=True)
    total_size = int(response.headers.get('content-length', 0))
    
    with open(filename, 'wb') as file, tqdm(
        desc=filename,
        total=total_size,
        unit='iB',
        unit_scale=True,
        unit_divisor=1024,
    ) as progress_bar:
        for data in response.iter_content(chunk_size=1024):
            size = file.write(data)
            progress_bar.update(size)

@st.cache_data
def load_from_drive(file_id: str):
    """Load pickle file directly from Google Drive"""
    try:
        url = f"https://drive.google.com/uc?id={file_id}&export=download"
        session = requests.Session()
        response = session.get(url, stream=True)
        
        for key, value in response.cookies.items():
            if key.startswith('download_warning'):
                url = f"{url}&confirm={value}"
                response = session.get(url, stream=True)
                break
        
        content = response.content
        print(f"Successfully downloaded {len(content)} bytes")
        return pickle.loads(content)
        
    except Exception as e:
        print(f"Detailed error: {str(e)}")
        st.error(f"Error loading file from Drive: {str(e)}")
        return None

# @st.cache_resource(show_spinner=False)
# def load_llama_model():
#     """Load Llama model with caching"""
#     try:
#         model_path = "mistral-7b-v0.1.Q4_K_M.gguf"
        
#         if not os.path.exists(model_path):
#             st.info("Downloading model... This may take a while.")
#             direct_url = "https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q4_K_M.gguf"
#             download_file_with_progress(direct_url, model_path)

#         llm_config = {
#             "model_path": model_path,
#             "n_ctx": 2048,
#             "n_threads": 4,
#             "n_batch": 512,
#             "n_gpu_layers": 0,
#             "verbose": False
#         }
        
#         model = Llama(**llm_config)
#         st.success("Model loaded successfully!")
#         return model
#     except Exception as e:
#         st.error(f"Error loading model: {str(e)}")
#         raise

@st.cache_resource(show_spinner=False)
def load_llama_model():
    """Load Llama model with caching"""
    try:
        model_path = "mistral-7b-v0.1.Q4_K_M.gguf"
        
        if not os.path.exists(model_path):
            st.info("Downloading model... This may take a while.")
            direct_url = "https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q4_K_M.gguf"
            download_file_with_progress(direct_url, model_path)

        if not os.path.exists(model_path):
            raise FileNotFoundError("Model file not found after download attempt")

        if os.path.getsize(model_path) < 1000000:  # Less than 1MB
            raise ValueError("Model file is too small, likely corrupted")

        llm_config = {
            "model_path": model_path,
            "n_ctx": 2048,
            "n_threads": 4,
            "n_batch": 512,
            "n_gpu_layers": 0,
            "verbose": True  # Enable verbose mode for debugging
        }
        
        logging.info("Initializing Llama model...")
        model = Llama(**llm_config)
        
        # Test the model
        logging.info("Testing model...")
        test_response = model("Test", max_tokens=10)
        if not test_response:
            raise RuntimeError("Model test failed")
            
        logging.info("Model loaded and tested successfully")
        st.success("Model loaded successfully!")
        return model
        
    except Exception as e:
        logging.error(f"Error loading model: {str(e)}")
        logging.error("Full error details: ", exc_info=True)
        raise

def check_environment():
    """Check if the environment is properly set up"""
    try:
        import torch
        import sentence_transformers
        return True
    except ImportError as e:
        st.error(f"Missing required package: {str(e)}")
        st.stop()
        return False

class RAGPipeline:
    def __init__(self, data_folder: str, k: int = 5):
        self.data_folder = data_folder
        self.k = k
        self.retriever = SentenceTransformerRetriever()
        self.documents = []
        self.device = torch.device("cpu")
        # Use the cached model directly
        self.llm = get_llama_model()

    def preprocess_query(self, query: str) -> str:
        """Clean and prepare the query"""
        query = query.lower().strip()
        query = re.sub(r'\s+', ' ', query)
        return query

    ### Added on Nov 2, 2024

    # def postprocess_response(self, response: str) -> str:
    #     """Clean up the generated response"""
    #     response = response.strip()
    #     response = re.sub(r'\s+', ' ', response)
    #     response = re.sub(r'\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}(?:\+\d{2}:?\d{2})?', '', response)
    #     return response

    # def query_model(self, prompt: str) -> str:
    #     """Query the local Llama model"""
    #     try:
    #         if self.llm is None:
    #             raise RuntimeError("Model not initialized")
                
    #         response = self.llm(
    #             prompt,
    #             max_tokens=512,
    #             temperature=0.4,
    #             top_p=0.95,
    #             echo=False,
    #             stop=["Question:", "\n\n"]
    #         )
            
    #         if response and 'choices' in response and len(response['choices']) > 0:
    #             text = response['choices'][0].get('text', '').strip()
    #             return text
    #         else:
    #             raise ValueError("No valid response generated")
                
    #     except Exception as e:
    #         logging.error(f"Error in query_model: {str(e)}")
    #         raise

    # def process_query(self, query: str, placeholder) -> str:
    #     try:
    #         # Preprocess query
    #         query = self.preprocess_query(query)
            
    #         # Show retrieval status
    #         status = placeholder.empty()
    #         status.write("πŸ” Finding relevant information...")
            
    #         # Get embeddings and search
    #         query_embedding = self.retriever.encode([query])
    #         similarities = F.cosine_similarity(query_embedding, self.retriever.doc_embeddings)
    #         scores, indices = torch.topk(similarities, k=min(self.k, len(self.documents)))
            
    #         relevant_docs = [self.documents[idx] for idx in indices.tolist()]
            
    #         # Update status
    #         status.write("πŸ’­ Generating response...")
            
    #         # Prepare context and prompt
    #         context = "\n".join(relevant_docs[:3])
    #         prompt = f"""Context information is below:
    #         {context}
            
    #         Given the context above, please answer the following question:
    #         {query}

    #         Guidelines:
    #         - If you cannot answer based on the context, say so politely
    #         - Keep the response concise and focused
    #         - Only include sports-related information
    #         - No dates or timestamps in the response
    #         - Use clear, natural language
            
    #         Answer:"""
            
    #         # Generate response
    #         response_placeholder = placeholder.empty()
            
    #         try:
    #             response_text = self.query_model(prompt)
    #             if response_text:
    #                 final_response = self.postprocess_response(response_text)
    #                 response_placeholder.markdown(final_response)
    #                 return final_response
    #             else:
    #                 message = "No relevant answer found. Please try rephrasing your question."
    #                 response_placeholder.warning(message)
    #                 return message
                    
    #         except Exception as e:
    #             logging.error(f"Generation error: {str(e)}")
    #             message = "Had some trouble generating the response. Please try again."
    #             response_placeholder.warning(message)
    #             return message
                
    #     except Exception as e:
    #         logging.error(f"Process error: {str(e)}")
    #         message = "Something went wrong. Please try again with a different question."
    #         placeholder.warning(message)
    #         return message

    # def process_query(self, query: str, placeholder) -> str:
    #     try:
    #         # Preprocess query
    #         query = self.preprocess_query(query)
    #         logging.info(f"Processing query: {query}")
        
    #         # Show retrieval status
    #         status = placeholder.empty()
    #         status.write("πŸ” Finding relevant information...")
        
    #         # Get embeddings and search
    #         query_embedding = self.retriever.encode([query])
    #         similarities = F.cosine_similarity(query_embedding, self.retriever.doc_embeddings)
    #         scores, indices = torch.topk(similarities, k=min(self.k, len(self.documents)))
        
    #         # Log similarity scores
    #         for idx, score in zip(indices.tolist(), scores.tolist()):
    #             logging.info(f"Score: {score:.4f} | Document: {self.documents[idx][:100]}...")
        
    #         relevant_docs = [self.documents[idx] for idx in indices.tolist()]
        
    #         # Update status
    #         status.write("πŸ’­ Generating response...")
        
    #         # Prepare context and prompt
    #         context = "\n".join(relevant_docs[:3])
    #         prompt = f"""Context information is below:
    #         {context}
        
    #         Given the context above, please answer the following question:
    #         {query}

    #         Guidelines:
    #         - If you cannot answer based on the context, say so politely
    #         - Keep the response concise and focused
    #         - Only include sports-related information
    #         - No dates or timestamps in the response
    #         - Use clear, natural language
        
    #         Answer:"""
        
    #         # Generate response
    #         response_placeholder = placeholder.empty()
        
    #         try:
    #             # Add logging for model state
    #             logging.info("Model state check - Is None?: " + str(self.llm is None))
            
    #             # Directly use Llama model
    #             response = self.llm(
    #                 prompt,
    #                 max_tokens=512,
    #                 temperature=0.4,
    #                 top_p=0.95,
    #                 echo=False,
    #                 stop=["Question:", "\n\n"]
    #             )
            
    #             logging.info(f"Raw model response: {response}")
            
    #             if response and isinstance(response, dict) and 'choices' in response:
    #                 generated_text = response['choices'][0].get('text', '').strip()
    #                 if generated_text:
    #                     final_response = self.postprocess_response(generated_text)
    #                     response_placeholder.markdown(final_response)
    #                     return final_response
            
    #             message = "No relevant answer found. Please try rephrasing your question."
    #             response_placeholder.warning(message)
    #             return message
            
    #         except Exception as e:
    #             logging.error(f"Generation error: {str(e)}")
    #             logging.error(f"Full error details: ", exc_info=True)
    #             message = f"Had some trouble generating the response: {str(e)}"
    #             response_placeholder.warning(message)
    #             return message
            
    #     except Exception as e:
    #         logging.error(f"Process error: {str(e)}")
    #         logging.error(f"Full error details: ", exc_info=True)
    #         message = f"Something went wrong: {str(e)}"
    #         placeholder.warning(message)
    #         return message

    ### Added on Nov 2, 2024
    def postprocess_response(self, response: str) -> str:
        """Clean up the generated response"""
        try:
            # Remove datetime patterns and other unwanted content
            response = re.sub(r'\d{4}-\d{2}-\d{2}(?:T|\s)\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?', '', response)
            response = re.sub(r'User \d+:.*?(?=User \d+:|$)', '', response)
            response = re.sub(r'\d{2}:\d{2}(?::\d{2})?(?:\s?(?:AM|PM))?', '', response)
            response = re.sub(r'\d{1,2}[-/]\d{1,2}[-/]\d{2,4}', '', response)
            response = re.sub(r'(?m)^User \d+:', '', response)
            
            # Clean up spacing but preserve intentional paragraph breaks
            # Replace multiple newlines with two newlines (one paragraph break)
            response = re.sub(r'\n\s*\n\s*\n+', '\n\n', response)
            # Replace multiple spaces with single space
            response = re.sub(r' +', ' ', response)
            # Clean up beginning/end
            response = response.strip()
            
            return response
        except Exception as e:
            logging.error(f"Error in postprocess_response: {str(e)}")
            return response

    def process_query(self, query: str, placeholder) -> str:
        try:
            # Verify this is the current query being processed
            if hasattr(st.session_state, 'current_query') and query != st.session_state.current_query:
                logging.warning(f"Skipping outdated query: {query}")
                return ""
                
            query = self.preprocess_query(query)
            status = placeholder.empty()
            status.write("πŸ” Finding relevant information...")
            
            query_embedding = self.retriever.encode([query])
            similarities = F.cosine_similarity(query_embedding, self.retriever.doc_embeddings)
            scores, indices = torch.topk(similarities, k=min(self.k, len(self.documents)))
            
            relevant_docs = [self.documents[idx] for idx in indices.tolist()]
            cleaned_docs = []
            for doc in relevant_docs[:3]:
                cleaned_text = self.postprocess_response(doc)
                if cleaned_text:
                    cleaned_docs.append(cleaned_text)
    
            status.write("πŸ’­ Generating response...")
            
            prompt = f"""Context information is below:
            {' '.join(cleaned_docs)}
            
            Given the context above, please answer the following question:
            {query}
    
            Guidelines for your response:
            - Structure your response in clear, logical paragraphs
            - Start a new paragraph for each new main point or aspect
            - If listing multiple items, use separate paragraphs
            - Keep each paragraph focused on a single topic or point
            - Use natural paragraph breaks where the content shifts focus
            - Maintain clear transitions between paragraphs
            - If providing statistics or achievements, group them logically
            - If describing different aspects (e.g., career, playing style, achievements), use separate paragraphs
            - Keep paragraphs concise but complete
            - Exclude any dates, timestamps, or user comments
            - Focus on factual sports information
            - If you cannot answer based on the context, say so politely
    
            Format your response with proper paragraph breaks where appropriate.
            
            Answer:"""
            
            response_placeholder = placeholder.empty()
            
            try:
                response_text = self.query_model(prompt)
                if response_text:
                    # Clean up the response while preserving paragraph structure
                    final_response = self.postprocess_response(response_text)
                    
                    # Convert cleaned response to markdown with proper paragraph spacing
                    markdown_response = final_response.replace('\n\n', '\n\n&nbsp;\n\n')
                    
                    response_placeholder.markdown(markdown_response)
                    return final_response
                else:
                    message = "No relevant answer found. Please try rephrasing your question."
                    response_placeholder.warning(message)
                    return message
                    
            except Exception as e:
                logging.error(f"Generation error: {str(e)}")
                message = "Had some trouble generating the response. Please try again."
                response_placeholder.warning(message)
                return message
                
        except Exception as e:
            logging.error(f"Process error: {str(e)}")
            message = "Something went wrong. Please try again with a different question."
            placeholder.warning(message)
            return message

    # def query_model(self, prompt: str) -> str:
    #     """Query the local Llama model"""
    #     try:
    #         if self.llm is None:
    #             raise RuntimeError("Model not initialized")
                
    #         response = self.llm(
    #             prompt,
    #             max_tokens=512,
    #             temperature=0.4,
    #             top_p=0.95,
    #             echo=False,
    #             stop=["Question:", "Context:", "Guidelines:"],  # Removed \n\n from stop tokens to allow paragraphs
    #             repeat_penalty=1.1  # Added to encourage more diverse text
    #         )
            
    #         if response and 'choices' in response and len(response['choices']) > 0:
    #             text = response['choices'][0].get('text', '').strip()
    #             return text
    #         else:
    #             raise ValueError("No valid response generated")
                
    #     except Exception as e:
    #         logging.error(f"Error in query_model: {str(e)}")
    #         raise

    def query_model(self, prompt: str) -> str:
        """Query the local Llama model"""
        try:
            if self.llm is None:
                raise RuntimeError("Model not initialized")
                
            # Log the prompt for debugging
            logging.info(f"Sending prompt to model...")
            
            # Generate response with more explicit parameters
            response = self.llm(
                prompt,
                max_tokens=512,        # Maximum length of the response
                temperature=0.7,       # Slightly increased for more dynamic responses
                top_p=0.95,           # Nucleus sampling parameter
                top_k=50,             # Top-k sampling parameter
                echo=False,           # Don't include prompt in response
                stop=["Question:", "Context:", "Guidelines:"],  # Stop tokens
                repeat_penalty=1.1,    # Penalize repetition
                presence_penalty=0.5,  # Encourage topic diversity
                frequency_penalty=0.5  # Discourage word repetition
            )
            
            # Log the raw response for debugging
            logging.info(f"Raw model response: {response}")
            
            if response and isinstance(response, dict) and 'choices' in response and response['choices']:
                generated_text = response['choices'][0].get('text', '').strip()
                if generated_text:
                    logging.info(f"Generated text: {generated_text[:100]}...")  # Log first 100 chars
                    return generated_text
                else:
                    logging.warning("Model returned empty response")
                    raise ValueError("Empty response from model")
            else:
                logging.warning(f"Unexpected response format: {response}")
                raise ValueError("Invalid response format from model")
                
        except Exception as e:
            logging.error(f"Error in query_model: {str(e)}")
            logging.error("Full error details: ", exc_info=True)
            raise

def initialize_model(self):
    """Initialize the model with proper error handling and verification"""
    try:
        if not os.path.exists(self.model_path):
            st.info("Downloading model... This may take a while.")
            direct_url = "https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q4_K_M.gguf"
            download_file_with_progress(direct_url, self.model_path)

        # Verify file exists and has content
        if not os.path.exists(self.model_path):
            raise FileNotFoundError(f"Model file {self.model_path} not found after download attempts")
        
        if os.path.getsize(self.model_path) < 1000000:  # Less than 1MB
            os.remove(self.model_path)
            raise ValueError("Downloaded model file is too small, likely corrupted")
        
        # Updated model configuration
        llm_config = {
            "model_path": self.model_path,
            "n_ctx": 4096,          # Increased context window
            "n_threads": 4,
            "n_batch": 512,
            "n_gpu_layers": 0,
            "verbose": True,        # Enable verbose mode for debugging
            "use_mlock": False,     # Disable memory locking
            "last_n_tokens_size": 64,  # Token window size for repeat penalty
            "seed": -1              # Random seed for reproducibility
        }
        
        logging.info("Initializing Llama model...")
        self.llm = Llama(**llm_config)
        
        # Test the model
        test_response = self.llm(
            "Test response",
            max_tokens=10,
            temperature=0.7,
            echo=False
        )
        
        if not test_response or 'choices' not in test_response:
            raise RuntimeError("Model initialization test failed")
            
        logging.info("Model initialized and tested successfully")
        return self.llm
        
    except Exception as e:
        logging.error(f"Error initializing model: {str(e)}")
        raise

# @st.cache_resource(show_spinner=False)
# def initialize_rag_pipeline():
#     """Initialize the RAG pipeline once"""
#     try:
#         # Create necessary directories
#         os.makedirs("ESPN_data", exist_ok=True)
        
#         # Load embeddings from Drive
#         drive_file_id = "1MuV63AE9o6zR9aBvdSDQOUextp71r2NN"
#         with st.spinner("Loading embeddings from Google Drive..."):
#             cache_data = load_from_drive(drive_file_id)
#             if cache_data is None:
#                 st.error("Failed to load embeddings from Google Drive")
#                 st.stop()
        
#         # Initialize pipeline
#         data_folder = "ESPN_data"
#         rag = RAGPipeline(data_folder)
        
#         # Store embeddings
#         rag.documents = cache_data['documents']
#         rag.retriever.store_embeddings(cache_data['embeddings'])
        
#         return rag
        
#     except Exception as e:
#         logging.error(f"Pipeline initialization error: {str(e)}")
#         st.error(f"Failed to initialize the system: {str(e)}")
#         raise

@st.cache_resource(show_spinner=False)
def initialize_rag_pipeline():
    """Initialize the RAG pipeline once"""
    try:
        data_folder = "ESPN_data"
        if not os.path.exists(data_folder):
            os.makedirs(data_folder, exist_ok=True)
            
        # Load embeddings first
        drive_file_id = "1MuV63AE9o6zR9aBvdSDQOUextp71r2NN"
        with st.spinner("Loading data..."):
            cache_data = load_from_drive(drive_file_id)
            if cache_data is None:
                st.error("Failed to load embeddings from Google Drive")
                st.stop()
        
        # Initialize pipeline
        rag = RAGPipeline(data_folder)
        
        # Store embeddings
        rag.documents = cache_data['documents']
        rag.retriever.store_embeddings(cache_data['embeddings'])
        
        return rag
        
    except Exception as e:
        logging.error(f"Pipeline initialization error: {str(e)}")
        st.error(f"Failed to initialize the system: {str(e)}")
        raise

# def main():
#     try:
#         # Environment check
#         if not check_environment():
#             return

#         # Improved CSS styling
#         st.markdown("""
#             <style>
#             /* Container styling */
#             .block-container {
#                 padding-top: 2rem;
#                 padding-bottom: 2rem;
#             }
            
#             /* Text input styling */
#             .stTextInput > div > div > input {
#                 width: 100%;
#             }
            
#             /* Button styling */
#             .stButton > button {
#                 width: 200px;
#                 margin: 0 auto;
#                 display: block;
#                 background-color: #FF4B4B;
#                 color: white;
#                 border-radius: 5px;
#                 padding: 0.5rem 1rem;
#             }
            
#             /* Title styling */
#             .main-title {
#                 text-align: center;
#                 padding: 1rem 0;
#                 font-size: 3rem;
#                 color: #1F1F1F;
#             }
            
#             .sub-title {
#                 text-align: center;
#                 padding: 0.5rem 0;
#                 font-size: 1.5rem;
#                 color: #4F4F4F;
#             }
            
#             /* Description styling */
#             .description {
#                 text-align: center;
#                 color: #666666;
#                 padding: 0.5rem 0;
#                 font-size: 1.1rem;
#                 line-height: 1.6;
#                 margin-bottom: 1rem;
#             }

#             /* Answer container styling */
#             .stMarkdown {
#                 max-width: 100%;
#             }

#             /* Streamlit default overrides */
#             .st-emotion-cache-16idsys p {
#                 font-size: 1.1rem;
#                 line-height: 1.6;
#             }
            
#             /* Container for main content */
#             .main-content {
#                 max-width: 1200px;
#                 margin: 0 auto;
#                 padding: 0 1rem;
#             }
#             </style>
#         """, unsafe_allow_html=True)

#         # Header section
#         st.markdown("<h1 class='main-title'>πŸ† The Sport Chatbot</h1>", unsafe_allow_html=True)
#         st.markdown("<h3 class='sub-title'>Using ESPN API</h3>", unsafe_allow_html=True)
#         st.markdown("""
#             <p class='description'>
#                 Hey there! πŸ‘‹ I can help you with information on Ice Hockey, Baseball, American Football, Soccer, and Basketball. 
#                 With access to the ESPN API, I'm up to date with the latest details for these sports up until October 2024.
#             </p>
#             <p class='description'>
#                 Got any general questions? Feel free to askβ€”I'll do my best to provide answers based on the information I've been trained on!
#             </p>
#         """, unsafe_allow_html=True)

#         # Initialize the pipeline
#         if 'rag' not in st.session_state:
#             with st.spinner("Loading resources..."):
#                 st.session_state.rag = initialize_rag_pipeline()

#         # Create columns for layout
#         col1, col2, col3 = st.columns([1, 6, 1])
        
#         with col2:
#             # Query input
#             query = st.text_input("What would you like to know about sports?")
            
#             if st.button("Get Answer"):
#                 if query:
#                     response_placeholder = st.empty()
#                     try:
#                         response = st.session_state.rag.process_query(query, response_placeholder)
#                         logging.info(f"Generated response: {response}")
#                     except Exception as e:
#                         logging.error(f"Query processing error: {str(e)}")
#                         response_placeholder.warning("Unable to process your question. Please try again.")
#                 else:
#                     st.warning("Please enter a question!")

#         # Footer
#         st.markdown("<br><br>", unsafe_allow_html=True)
#         st.markdown("---")
#         st.markdown("""
#             <p style='text-align: center; color: #666666; padding: 1rem 0;'>
#                 Powered by ESPN Data & Mistral AI πŸš€
#             </p>
#         """, unsafe_allow_html=True)

#     except Exception as e:
#         logging.error(f"Application error: {str(e)}")
#         st.error("An unexpected error occurred. Please check the logs and try again.")

# def main():
#     try:
#         # Environment check
#         if not check_environment():
#             return

#         # Improved CSS styling
#         st.markdown("""
#             <style>
#             /* Container styling */
#             .block-container {
#                 padding-top: 2rem;
#                 padding-bottom: 2rem;
#             }
            
#             /* Text input styling */
#             .stTextInput > div > div > input {
#                 width: 100%;
#             }
            
#             /* Button styling */
#             .stButton > button {
#                 width: 200px;
#                 margin: 0 auto;
#                 display: block;
#                 background-color: #FF4B4B;
#                 color: white;
#                 border-radius: 5px;
#                 padding: 0.5rem 1rem;
#             }
            
#             /* Title styling */
#             .main-title {
#                 text-align: center;
#                 padding: 1rem 0;
#                 font-size: 3rem;
#                 color: #1F1F1F;
#             }
            
#             .sub-title {
#                 text-align: center;
#                 padding: 0.5rem 0;
#                 font-size: 1.5rem;
#                 color: #4F4F4F;
#             }
            
#             /* Description styling */
#             .description {
#                 text-align: center;
#                 color: #666666;
#                 padding: 0.5rem 0;
#                 font-size: 1.1rem;
#                 line-height: 1.6;
#                 margin-bottom: 1rem;
#             }

#             /* Answer container styling */
#             .stMarkdown {
#                 max-width: 100%;
#             }

#             /* Streamlit default overrides */
#             .st-emotion-cache-16idsys p {
#                 font-size: 1.1rem;
#                 line-height: 1.6;
#             }
            
#             /* Container for main content */
#             .main-content {
#                 max-width: 1200px;
#                 margin: 0 auto;
#                 padding: 0 1rem;
#             }
#             </style>
#         """, unsafe_allow_html=True)

#         # Header section
#         st.markdown("<h1 class='main-title'>πŸ† The Sport Chatbot</h1>", unsafe_allow_html=True)
#         st.markdown("<h3 class='sub-title'>Using ESPN API</h3>", unsafe_allow_html=True)
#         st.markdown("""
#             <p class='description'>
#                 Hey there! πŸ‘‹ I can help you with information on Ice Hockey, Baseball, American Football, Soccer, and Basketball. 
#                 With access to the ESPN API, I'm up to date with the latest details for these sports up until October 2024.
#             </p>
#             <p class='description'>
#                 Got any general questions? Feel free to askβ€”I'll do my best to provide answers based on the information I've been trained on!
#             </p>
#         """, unsafe_allow_html=True)

#         # Initialize the pipeline with better error handling
#         if 'rag' not in st.session_state:
#             try:
#                 with st.spinner("Loading resources..."):
#                     st.session_state.rag = initialize_rag_pipeline()
#                     logging.info("Pipeline initialized successfully")
#             except Exception as e:
#                 logging.error(f"Pipeline initialization error: {str(e)}")
#                 st.error("Failed to initialize the system. Please check the logs.")
#                 st.stop()
#                 return

#         # Create columns for layout
#         col1, col2, col3 = st.columns([1, 6, 1])
        
#         with col2:
#             # Query input
#             query = st.text_input("What would you like to know about sports?")
            
#             if st.button("Get Answer"):
#                 if query:
#                     response_placeholder = st.empty()
#                     try:
#                         # Log query processing start
#                         logging.info(f"Processing query: {query}")
                        
#                         # Process query and get response
#                         response = st.session_state.rag.process_query(query, response_placeholder)
                        
#                         # Log successful response
#                         logging.info(f"Generated response: {response}")
#                     except Exception as e:
#                         # Log error details
#                         logging.error(f"Query processing error: {str(e)}")
#                         logging.error("Full error details: ", exc_info=True)
#                         response_placeholder.warning("Unable to process your question. Please try again.")
#                 else:
#                     st.warning("Please enter a question!")

#         # Footer
#         st.markdown("<br><br>", unsafe_allow_html=True)
#         st.markdown("---")
#         st.markdown("""
#             <p style='text-align: center; color: #666666; padding: 1rem 0;'>
#                 Powered by ESPN Data & Mistral AI πŸš€
#             </p>
#         """, unsafe_allow_html=True)

#     except Exception as e:
#         logging.error(f"Application error: {str(e)}")
#         logging.error("Full error details: ", exc_info=True)
#         st.error("An unexpected error occurred. Please check the logs and try again.")

# if __name__ == "__main__":
#     # Configure logging
#     logging.basicConfig(
#         level=logging.INFO,
#         format='%(asctime)s - %(levelname)s - %(message)s'
#     )
    
#     try:
#         main()
#     except Exception as e:
#         logging.error(f"Fatal error: {str(e)}")
#         logging.error("Full error details: ", exc_info=True)
#         st.error("A fatal error occurred. Please check the logs and try again.")

# if __name__ == "__main__":
#     main()


def main():
    try:
        # First, check if model exists
        model_path = os.path.join("models", "mistral-7b-v0.1.Q4_K_M.gguf")
        if not os.path.exists(model_path):
            st.warning("⚠️ First-time setup: The model will be downloaded. This takes a few minutes but only happens once.")
        # Environment check
        if not check_environment():
            return

        # Initialize session state variables
        if 'current_query' not in st.session_state:
            st.session_state.current_query = None
        if 'processing' not in st.session_state:
            st.session_state.processing = False

        # Improved CSS styling
        st.markdown("""
            <style>
            /* Container styling */
            .block-container {
                padding-top: 2rem;
                padding-bottom: 2rem;
            }
            
            /* Text input styling */
            .stTextInput > div > div > input {
                width: 100%;
            }
            
            /* Button styling */
            .stButton > button {
                width: 200px;
                margin: 0 auto;
                display: block;
                background-color: #FF4B4B;
                color: white;
                border-radius: 5px;
                padding: 0.5rem 1rem;
            }
            
            /* Title styling */
            .main-title {
                text-align: center;
                padding: 1rem 0;
                font-size: 3rem;
                color: #1F1F1F;
            }
            
            .sub-title {
                text-align: center;
                padding: 0.5rem 0;
                font-size: 1.5rem;
                color: #4F4F4F;
            }
            
            /* Description styling */
            .description {
                text-align: center;
                color: #666666;
                padding: 0.5rem 0;
                font-size: 1.1rem;
                line-height: 1.6;
                margin-bottom: 1rem;
            }

            /* Answer container styling */
            .stMarkdown {
                max-width: 100%;
            }

            /* Streamlit default overrides */
            .st-emotion-cache-16idsys p {
                font-size: 1.1rem;
                line-height: 1.6;
            }
            
            /* Container for main content */
            .main-content {
                max-width: 1200px;
                margin: 0 auto;
                padding: 0 1rem;
            }
            </style>
        """, unsafe_allow_html=True)

        # Header section
        st.markdown("<h1 class='main-title'>πŸ† The Sport Chatbot</h1>", unsafe_allow_html=True)
        st.markdown("<h3 class='sub-title'>Using ESPN API</h3>", unsafe_allow_html=True)
        st.markdown("""
            <p class='description'>
                Hey there! πŸ‘‹ I can help you with information on Ice Hockey, Baseball, American Football, Soccer, and Basketball. 
                With access to the ESPN API, I'm up to date with the latest details for these sports up until October 2024.
            </p>
            <p class='description'>
                Got any general questions? Feel free to askβ€”I'll do my best to provide answers based on the information I've been trained on!
            </p>
        """, unsafe_allow_html=True)

        # Initialize the pipeline
        if 'rag' not in st.session_state:
            try:
                with st.spinner("Loading resources..."):
                    st.session_state.rag = initialize_rag_pipeline()
                    logging.info("Pipeline initialized successfully")
            except Exception as e:
                logging.error(f"Pipeline initialization error: {str(e)}")
                st.error("Failed to initialize the system. Please check the logs.")
                st.stop()
                return

        # Create columns for layout
        col1, col2, col3 = st.columns([1, 6, 1])
        
        with col2:
            # Query input with unique key
            query = st.text_input(
                "What would you like to know about sports?",
                key="sports_query"
            )
            
            # Centered button with unique key
            if st.button("Get Answer", key="answer_button"):
                if query:
                    # Clear any previous response
                    if 'response_placeholder' in st.session_state:
                        st.session_state.response_placeholder.empty()
                        
                    response_placeholder = st.empty()
                    st.session_state.response_placeholder = response_placeholder
                    
                    try:
                        # Update current query and processing state
                        st.session_state.current_query = query
                        st.session_state.processing = True
                        
                        # Log query processing start
                        logging.info(f"Processing query: {query}")
                        
                        with st.spinner("Processing your question..."):
                            # Process query and get response
                            response = st.session_state.rag.process_query(query, response_placeholder)
                            
                            # Log successful response
                            logging.info(f"Generated response: {response}")
                            
                        # Reset processing state
                        st.session_state.processing = False
                            
                    except Exception as e:
                        # Log error details
                        logging.error(f"Query processing error: {str(e)}")
                        logging.error("Full error details: ", exc_info=True)
                        response_placeholder.warning("Unable to process your question. Please try again.")
                        st.session_state.processing = False
                else:
                    st.warning("Please enter a question!")

        # Footer
        st.markdown("<br><br>", unsafe_allow_html=True)
        st.markdown("---")
        st.markdown("""
            <p style='text-align: center; color: #666666; padding: 1rem 0;'>
                Powered by ESPN Data & Mistral AI πŸš€
            </p>
        """, unsafe_allow_html=True)

    except Exception as e:
        logging.error(f"Application error: {str(e)}")
        logging.error("Full error details: ", exc_info=True)
        st.error("An unexpected error occurred. Please check the logs and try again.")

if __name__ == "__main__":
    # Configure logging
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s'
    )
    
    try:
        main()
    except Exception as e:
        logging.error(f"Fatal error: {str(e)}")
        logging.error("Full error details: ", exc_info=True)
        st.error("A fatal error occurred. Please check the logs and try again.")