File size: 24,846 Bytes
3875c87
 
38fe9c5
3875c87
38fe9c5
3875c87
 
 
 
 
 
 
 
 
 
 
38fe9c5
 
 
 
3875c87
38fe9c5
 
 
3875c87
38fe9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875c87
38fe9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875c87
 
38fe9c5
 
 
 
 
 
 
 
 
 
 
3875c87
 
38fe9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875c87
38fe9c5
 
3875c87
38fe9c5
 
3875c87
38fe9c5
 
 
3875c87
38fe9c5
3875c87
 
 
 
 
 
38fe9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875c87
38fe9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875c87
 
38fe9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875c87
38fe9c5
 
 
52807fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709f6b7
 
3875c87
709f6b7
6ccf2cb
709f6b7
52807fc
709f6b7
 
 
 
 
 
52807fc
 
 
 
3875c87
 
 
 
52807fc
83e3bd0
 
3875c87
52807fc
3875c87
 
 
 
52807fc
709f6b7
52807fc
83e3bd0
3875c87
52807fc
 
 
 
 
 
3875c87
52807fc
 
709f6b7
3875c87
52807fc
709f6b7
3875c87
6956d92
3875c87
 
 
6956d92
3875c87
 
6956d92
 
 
3794b5e
52807fc
3794b5e
52807fc
 
6956d92
52807fc
3794b5e
6956d92
3794b5e
6956d92
 
52807fc
6956d92
 
3875c87
 
6956d92
709f6b7
 
6956d92
 
 
3875c87
 
 
6956d92
 
 
 
 
 
709f6b7
6956d92
52807fc
6956d92
 
 
 
 
3875c87
6956d92
 
 
 
 
 
 
3875c87
6956d92
3875c87
6956d92
 
709f6b7
6956d92
52807fc
709f6b7
6956d92
 
 
3875c87
52807fc
 
709f6b7
3875c87
709f6b7
 
 
 
 
 
3875c87
 
52807fc
 
 
3875c87
 
52807fc
 
3875c87
 
 
52807fc
 
 
 
 
 
 
 
 
3875c87
b38d37f
 
3875c87
7c797e6
b38d37f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c797e6
 
 
b38d37f
52807fc
 
7c797e6
 
 
 
3875c87
7c797e6
52807fc
38fe9c5
b38d37f
52807fc
 
 
3875c87
 
83e3bd0
3875c87
 
 
52807fc
 
 
 
 
 
 
83e3bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b38d37f
 
 
83e3bd0
52807fc
 
3875c87
52807fc
 
 
3875c87
52807fc
 
 
3875c87
 
52807fc
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
# import os
# import logging
# import math
# import streamlit as st
# import fitz  # PyMuPDF
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
# from langchain_community.document_loaders import PDFMinerLoader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_community.embeddings import SentenceTransformerEmbeddings
# from langchain_community.vectorstores import Chroma
# from langchain_community.llms import HuggingFacePipeline
# from langchain.chains import RetrievalQA

# # Set up logging
# logging.basicConfig(level=logging.INFO)

# # Define global variables
# device = 'cpu'
# persist_directory = "db"
# uploaded_files_dir = "uploaded_files"

# # Streamlit app configuration
# st.set_page_config(page_title="Audit Assistant", layout="wide")
# st.title("Audit Assistant")

# # Load the model
# checkpoint = "MBZUAI/LaMini-T5-738M"
# tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# base_model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)

# # Helper Functions

# def extract_text_from_pdf(file_path):
#     """Extract text from a PDF using PyMuPDF (fitz)."""
#     try:
#         doc = fitz.open(file_path)
#         text = ""
#         for page_num in range(doc.page_count):
#             page = doc.load_page(page_num)
#             text += page.get_text("text")
#         return text
#     except Exception as e:
#         logging.error(f"Error reading PDF {file_path}: {e}")
#         return None

# def data_ingestion():
#     """Function to load PDFs and create embeddings with improved error handling and efficiency."""
#     try:
#         logging.info("Starting data ingestion")

#         if not os.path.exists(uploaded_files_dir):
#             os.makedirs(uploaded_files_dir)

#         documents = []  
#         for filename in os.listdir(uploaded_files_dir):
#             if filename.endswith(".pdf"):
#                 file_path = os.path.join(uploaded_files_dir, filename)
#                 logging.info(f"Processing file: {file_path}")
                
#                 try:
#                     loader = PDFMinerLoader(file_path)
#                     loaded_docs = loader.load()
#                     if not loaded_docs:
#                         logging.warning(f"Skipping file with missing or invalid metadata: {file_path}")
#                         continue
                    
#                     for doc in loaded_docs:
#                         if hasattr(doc, 'page_content') and len(doc.page_content.strip()) > 0:
#                             documents.append(doc)
#                         else:
#                             logging.warning(f"Skipping invalid document structure in {file_path}")
#                 except ValueError as e:
#                     logging.error(f"Skipping {file_path}: {str(e)}")
#                     continue

#         if not documents:
#             logging.error("No valid documents found to process.")
#             return

#         logging.info(f"Total valid documents: {len(documents)}")

#         # Proceed with splitting and embedding documents
#         text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
#         texts = text_splitter.split_documents(documents)

#         logging.info(f"Total text chunks created: {len(texts)}")
        
#         if not texts:
#             logging.error("No valid text chunks to create embeddings.")
#             return

#         embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
        
#         # Proceed to split and embed the documents
#         MAX_BATCH_SIZE = 5461  
#         total_batches = math.ceil(len(texts) / MAX_BATCH_SIZE)
        
#         logging.info(f"Processing {len(texts)} text chunks in {total_batches} batches...")

#         db = None
#         for i in range(total_batches):
#             batch_start = i * MAX_BATCH_SIZE
#             batch_end = min((i + 1) * MAX_BATCH_SIZE, len(texts))
#             text_batch = texts[batch_start:batch_end]
            
#             logging.info(f"Processing batch {i + 1}/{total_batches}, size: {len(text_batch)}")

#             if db is None:
#                 db = Chroma.from_documents(text_batch, embeddings, persist_directory=persist_directory)
#             else:
#                 db.add_documents(text_batch)

#         db.persist()
#         logging.info("Data ingestion completed successfully")
        
#     except Exception as e:
#         logging.error(f"Error during data ingestion: {str(e)}")
#         raise

# def llm_pipeline():
#     """Set up the language model pipeline."""
#     logging.info("Setting up LLM pipeline")
#     pipe = pipeline(
#         'text2text-generation',
#         model=base_model,
#         tokenizer=tokenizer,
#         max_length=256,
#         do_sample=True,
#         temperature=0.3,
#         top_p=0.95,
#         device=device
#     )
#     local_llm = HuggingFacePipeline(pipeline=pipe)
#     logging.info("LLM pipeline setup complete")
#     return local_llm

# def qa_llm():
#     """Set up the question-answering chain."""
#     logging.info("Setting up QA model")
#     llm = llm_pipeline()
#     embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
#     db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
#     retriever = db.as_retriever()  # Set up the retriever for the vector store
#     qa = RetrievalQA.from_chain_type(
#         llm=llm,
#         chain_type="stuff",
#         retriever=retriever,
#         return_source_documents=True
#     )
#     logging.info("QA model setup complete")
#     return qa

# def process_answer(user_question):
#     """Generate an answer to the user’s question."""
#     try:
#         logging.info("Processing user question")
#         qa = qa_llm() 

#         tailored_prompt = f"""
#         You are an expert chatbot designed to assist Chartered Accountants (CAs) in the field of audits. 
#         Your goal is to provide accurate and comprehensive answers to any questions related to audit policies, procedures, 
#         and accounting standards based on the provided PDF documents. 
#         Please respond effectively and refer to the relevant standards and policies whenever applicable.

#         User question: {user_question}
#         """

#         generated_text = qa({"query": tailored_prompt})
#         answer = generated_text['result']

#         if "not provide" in answer or "no information" in answer:
#             return "The document does not provide sufficient information to answer your question."

#         logging.info("Answer generated successfully")
#         return answer

#     except Exception as e:
#         logging.error(f"Error during answer generation: {str(e)}")
#         return "Error processing the question."

# # Streamlit UI Setup
# st.sidebar.header("File Upload")
# uploaded_files = st.sidebar.file_uploader("Upload your PDF files", type=["pdf"], accept_multiple_files=True)

# if uploaded_files:
#     # Save uploaded files
#     if not os.path.exists(uploaded_files_dir):
#         os.makedirs(uploaded_files_dir)

#     for uploaded_file in uploaded_files:
#         file_path = os.path.join(uploaded_files_dir, uploaded_file.name)
#         with open(file_path, "wb") as f:
#             f.write(uploaded_file.getbuffer())
    
#     st.sidebar.success(f"Uploaded {len(uploaded_files)} file(s) successfully!")

#     # Run data ingestion when files are uploaded
#     data_ingestion()

#     # Display UI for Q&A
#     st.header("Ask a Question")
#     user_question = st.text_input("Enter your question here:")

#     if user_question:
#         answer = process_answer(user_question)
#         st.write(answer)

# else:
#     st.sidebar.info("Upload PDF files to get started!")

# # -------this is the second code!!!
# import os
# import logging
# import math
# import streamlit as st
# import fitz  # PyMuPDF
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
# # from langchain_community.document_loaders import PDFMinerLoader
# from langchain_community.document_loaders import PyMuPDFLoader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_community.embeddings import SentenceTransformerEmbeddings
# from langchain_community.vectorstores import Chroma
# from langchain_community.llms import HuggingFacePipeline
# from langchain.chains import RetrievalQA

# device = 'cpu'
# persist_directory = "db"
# uploaded_files_dir = "uploaded_files"


# logging.basicConfig(level=logging.INFO)

# # for main Page Setup
# st.set_page_config(page_title="RAG Chatbot", layout="wide")
# st.title("📚 RAG-based PDF Assistant")

# # Load my model
# checkpoint = "MBZUAI/LaMini-T5-738M"
# tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# base_model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)

# # ------------------------------- #

# def extract_outline_from_pdf(path):
#     try:
#         doc = fitz.open(path)
#         outline_text = ""
#         for page_num in range(len(doc)):
#             page = doc[page_num]
#             outline_text += f"### Page {page_num+1}:\n{page.get_text('text')[:500]}\n---\n"
#         return outline_text if outline_text else "No preview available."
#     except Exception as e:
#         return f"Could not preview PDF: {e}"

# def data_ingestion():
#     """Load PDFs, validate content, and generate embeddings."""
#     try:
#         logging.info("Starting data ingestion")

#         if not os.path.exists(uploaded_files_dir):
#             os.makedirs(uploaded_files_dir)

#         documents = []  
#         for filename in os.listdir(uploaded_files_dir):
#             if filename.endswith(".pdf"):
#                 file_path = os.path.join(uploaded_files_dir, filename)
#                 logging.info(f"Processing file: {file_path}")
                
#                 try:
#                     loader = PyMuPDFLoader(file_path)
#                     loaded_docs = loader.load()
                    
#                     # Check if any content exists in loaded_docs
#                     if not loaded_docs or len(loaded_docs[0].page_content.strip()) == 0:
#                         logging.warning(f"No readable text found in {file_path}. Might be a scanned image or unsupported format.")
#                         continue

#                     for doc in loaded_docs:
#                         if hasattr(doc, 'page_content') and len(doc.page_content.strip()) > 0:
#                             documents.append(doc)
#                         else:
#                             logging.warning(f"Skipping invalid document structure in {file_path}")

#                 except Exception as e:
#                     logging.error(f"Skipping {file_path}: {str(e)}")
#                     continue

#         if not documents:
#             logging.error("No valid documents found to process.")
#             return

#         logging.info(f"Total valid documents: {len(documents)}")

#         # Proceed with splitting and embedding documents
#         text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
#         texts = text_splitter.split_documents(documents)

#         logging.info(f"Total text chunks created: {len(texts)}")
        
#         if not texts:
#             logging.error("No valid text chunks to create embeddings.")
#             return

#         embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
        
#         MAX_BATCH_SIZE = 5461  
#         total_batches = math.ceil(len(texts) / MAX_BATCH_SIZE)
        
#         logging.info(f"Processing {len(texts)} text chunks in {total_batches} batches...")

#         db = None
#         for i in range(total_batches):
#             batch_start = i * MAX_BATCH_SIZE
#             batch_end = min((i + 1) * MAX_BATCH_SIZE, len(texts))
#             text_batch = texts[batch_start:batch_end]
            
#             logging.info(f"Processing batch {i + 1}/{total_batches}, size: {len(text_batch)}")

#             if db is None:
#                 db = Chroma.from_documents(text_batch, embeddings, persist_directory=persist_directory)
#             else:
#                 db.add_documents(text_batch)

#         db.persist()
#         logging.info("Data ingestion completed successfully")

#     except Exception as e:
#         logging.error(f"Error during data ingestion: {str(e)}")
#         raise


# def llm_pipeline():
#     pipe = pipeline(
#         'text2text-generation',
#         model=base_model,
#         tokenizer=tokenizer,
#         max_length=256,
#         do_sample=True,
#         temperature=0.3,
#         top_p=0.95,
#         device=device
#     )
#     return HuggingFacePipeline(pipeline=pipe)

# def qa_llm():
#     llm = llm_pipeline()
#     embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
#     db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
#     retriever = db.as_retriever()
#     return RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)

# def process_answer(user_question):
#     """Generate an answer to the user’s question using a general RAG-based prompt."""
#     try:
#         logging.info("Processing user question")
#         qa = qa_llm()  # Set up the retrieval-based QA chain

#         # Generalized, flexible prompt for any kind of PDF (resume, legal doc, etc.)
#         tailored_prompt = f"""
# You are an intelligent and helpful AI assistant that provides answers strictly based on the provided document contents.
# If the question cannot be answered using the documents, say: 'The document does not contain this information.'
# Otherwise, respond clearly and concisely with relevant and factual details from the PDF.

# Question: {user_question}
# """

#         generated_text = qa({"query": tailored_prompt})
#         answer = generated_text['result']

#         # Add a safeguard for hallucinated answers
#         if "not provide" in answer.lower() or "no information" in answer.lower() or len(answer.strip()) < 10:
#             return "The document does not contain this information."

#         logging.info("Answer generated successfully")
#         return answer

#     except Exception as e:
#         logging.error(f"Error during answer generation: {str(e)}")
#         return "Sorry, something went wrong while processing your question."


# # ---------------- STREAMLIT UI ---------------- #

# # Sidebar Upload 
# st.sidebar.header("📤 Upload PDF Files")
# uploaded_files = st.sidebar.file_uploader("Select one or more PDF files", type="pdf", accept_multiple_files=True)

# if uploaded_files:
#     if not os.path.exists(uploaded_files_dir):
#         os.makedirs(uploaded_files_dir)

#     for file in uploaded_files:
#         path = os.path.join(uploaded_files_dir, file.name)
#         with open(path, "wb") as f:
#             f.write(file.getbuffer())

#     st.sidebar.success(f"{len(uploaded_files)} file(s) uploaded.")

#     # Display previews
#     st.subheader("📄 Uploaded PDF Previews")
#     for file in uploaded_files:
#         with st.expander(file.name):
#             st.text(extract_outline_from_pdf(os.path.join(uploaded_files_dir, file.name)))

#     # Trigger ingestion
#     with st.spinner("🔄 Ingesting uploaded documents..."):
#         data_ingestion()

#     # Ask a question
#     st.header("❓ Ask a Question from Your Documents")
#     user_input = st.text_input("Enter your question:")
#     if user_input:
#         with st.spinner("💬 Generating response..."):
#             response = process_answer(user_input)
#         st.success(response)

# else:
#     st.sidebar.info("Upload PDFs to begin your QA journey.")


import os
import logging
import math
import streamlit as st
import fitz  # PyMuPDF
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from langchain_community.document_loaders import PDFMinerLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import SentenceTransformerEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_community.llms import HuggingFacePipeline
from langchain.chains import RetrievalQA

# Set up logging
logging.basicConfig(level=logging.INFO)

# Define global variables
device = 'cpu'
persist_directory = "db"
uploaded_files_dir = "uploaded_files"

# Streamlit app configuration
st.set_page_config(page_title="RAG-based Chatbot", layout="wide")
st.title("RAG-based Chatbot")

# Load the model
checkpoint = "MBZUAI/LaMini-T5-738M"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
base_model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)

# Helper Functions

def extract_text_from_pdf(file_path):
    """Extract full text from a PDF using PyMuPDF (fitz)."""
    try:
        doc = fitz.open(file_path)
        text = ""
        for page_num in range(doc.page_count):
            page = doc.load_page(page_num)
            text += page.get_text("text")
        return text
    except Exception as e:
        logging.error(f"Error reading PDF {file_path}: {e}")
        return None

def data_ingestion():
    """Function to load PDFs and create embeddings with improved error handling and efficiency."""
    try:
        logging.info("Starting data ingestion")

        if not os.path.exists(uploaded_files_dir):
            os.makedirs(uploaded_files_dir)

        documents = []  
        for filename in os.listdir(uploaded_files_dir):
            if filename.endswith(".pdf"):
                file_path = os.path.join(uploaded_files_dir, filename)
                logging.info(f"Processing file: {file_path}")
                
                try:
                    loader = PDFMinerLoader(file_path)
                    loaded_docs = loader.load()
                    if not loaded_docs:
                        logging.warning(f"Skipping file with missing or invalid metadata: {file_path}")
                        continue
                    
                    for doc in loaded_docs:
                        if hasattr(doc, 'page_content') and len(doc.page_content.strip()) > 0:
                            documents.append(doc)
                        else:
                            logging.warning(f"Skipping invalid document structure in {file_path}")
                except ValueError as e:
                    logging.error(f"Skipping {file_path}: {str(e)}")
                    continue

        if not documents:
            logging.error("No valid documents found to process.")
            return

        logging.info(f"Total valid documents: {len(documents)}")

        # Proceed with splitting and embedding documents
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
        texts = text_splitter.split_documents(documents)

        logging.info(f"Total text chunks created: {len(texts)}")
        
        if not texts:
            logging.error("No valid text chunks to create embeddings.")
            return

        embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
        
        # Proceed to split and embed the documents
        MAX_BATCH_SIZE = 5461  
        total_batches = math.ceil(len(texts) / MAX_BATCH_SIZE)
        
        logging.info(f"Processing {len(texts)} text chunks in {total_batches} batches...")

        db = None
        for i in range(total_batches):
            batch_start = i * MAX_BATCH_SIZE
            batch_end = min((i + 1) * MAX_BATCH_SIZE, len(texts))
            text_batch = texts[batch_start:batch_end]
            
            logging.info(f"Processing batch {i + 1}/{total_batches}, size: {len(text_batch)}")

            if db is None:
                db = Chroma.from_documents(text_batch, embeddings, persist_directory=persist_directory)
            else:
                db.add_documents(text_batch)

        db.persist()
        logging.info("Data ingestion completed successfully")
        
    except Exception as e:
        logging.error(f"Error during data ingestion: {str(e)}")
        raise

def llm_pipeline():
    """Set up the language model pipeline."""
    logging.info("Setting up LLM pipeline")
    pipe = pipeline(
        'text2text-generation',
        model=base_model,
        tokenizer=tokenizer,
        max_length=256,
        do_sample=True,
        temperature=0.3,
        top_p=0.95,
        device=device
    )
    local_llm = HuggingFacePipeline(pipeline=pipe)
    logging.info("LLM pipeline setup complete")
    return local_llm

def qa_llm():
    """Set up the question-answering chain."""
    logging.info("Setting up QA model")
    llm = llm_pipeline()
    embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
    db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
    retriever = db.as_retriever()  # Set up the retriever for the vector store
    qa = RetrievalQA.from_chain_type(
        llm=llm,
        chain_type="stuff",
        retriever=retriever,
        return_source_documents=True
    )
    logging.info("QA model setup complete")
    return qa

def process_answer(user_question, full_text):
    """Generate an answer to the user’s question or summarize the PDF content."""
    try:
        logging.info("Processing user question")
        
        # Check if the question is related to summarization
        if "summarize" in user_question.lower() or "summary" in user_question.lower():
            tailored_prompt = f"""
            Please provide a summary of the following content extracted from the PDF:
            {full_text}
            """
        else:
            # Regular Q&A with context from the uploaded PDF
            tailored_prompt = f"""
            You are an expert chatbot designed to assist with any topic, providing accurate and detailed answers based on the provided PDFs. 
            Your goal is to deliver the most relevant information and resources based on the question asked. 
            User question: {user_question}
            Content from the uploaded document: {full_text}
            """

        # Pass the tailored prompt to the question-answering chain (QA) system
        qa = qa_llm()  # Call your QA LLM setup
        generated_text = qa({"query": tailored_prompt})
        answer = generated_text['result']

        # If the answer contains certain fallback phrases, return a default message
        if "not provide" in answer or "no information" in answer:
            return "The document does not provide sufficient information to answer your question."

        logging.info("Answer generated successfully")
        return answer

    except Exception as e:
        logging.error(f"Error during answer generation: {str(e)}")
        return "Error processing the question."


# Streamlit UI Setup
st.sidebar.header("File Upload")
uploaded_files = st.sidebar.file_uploader("Upload your PDF files", type=["pdf"], accept_multiple_files=True)

if uploaded_files:
    # Save uploaded files and extract their text
    if not os.path.exists(uploaded_files_dir):
        os.makedirs(uploaded_files_dir)

    for uploaded_file in uploaded_files:
        file_path = os.path.join(uploaded_files_dir, uploaded_file.name)
        with open(file_path, "wb") as f:
            f.write(uploaded_file.getbuffer())

    st.sidebar.success(f"Uploaded {len(uploaded_files)} file(s) successfully!")

    # Show the uploaded files' names
    st.subheader("Uploaded PDF(s):")
    for uploaded_file in uploaded_files:
        st.write(uploaded_file.name)
        # Display PDF preview link if possible
        with open(file_path, "rb") as f:
            file_bytes = f.read()
            st.download_button(
                label="Download PDF",
                data=file_bytes,
                file_name=uploaded_file.name,
                mime="application/pdf",
            )

        # Extract and display the full text from the PDF
        st.subheader("Full Text from the PDF:")
        full_text = extract_text_from_pdf(file_path)
        if full_text:
            st.text_area("PDF Text", full_text, height=300)
        else:
            st.warning("Failed to extract text from this PDF.")

    # # Generate summary option
    # if st.button("Generate Summary of Document"):
    #     st.write("Summary: [Provide the generated summary here]")
    
    # Run data ingestion when files are uploaded
    data_ingestion()

    # Display UI for Q&A
    st.header("Ask a Question")
    user_question = st.text_input("Enter your question here:")

    if user_question:
        answer = process_answer(user_question)
        st.write(answer)

else:
    st.sidebar.info("Upload PDF files to get started!")