-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1470 lines (1271 loc) · 49.8 KB
/
app.py
File metadata and controls
1470 lines (1271 loc) · 49.8 KB
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
# ===========================================
# IntelliQuery
# A Streamlit application integrating various document retrieval and Q&A functionalities.
# ===========================================
# ===========================================
# 1) Imports and Dependencies
# ===========================================
import os
import re
import shutil
import time
import base64
import tempfile
import ffmpeg
import cohere
import cv2
import json
import google.generativeai as genai
import pandas as pd
import requests
import speech_recognition as sr
import streamlit as st
import torch
import torch._classes
import whisper
from bs4 import BeautifulSoup
from datetime import datetime
from dotenv import load_dotenv
from fpdf import FPDF
from googletrans import Translator, LANGUAGES
from langchain.chains import RetrievalQA
from langchain.chains.question_answering import load_qa_chain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.prompts import PromptTemplate
from langchain_experimental.agents import create_csv_agent
from langchain_core.documents import Document
from langchain_community.vectorstores import FAISS
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
from PIL import Image
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
from PyPDF2 import PdfReader
from transformers import BertTokenizer, BertForSequenceClassification
from streamlit import components
import yt_dlp
# ===========================================
# 2) Global Configurations and Session State
# ===========================================
# Load environment variables from .env file
load_dotenv()
# Initialize Cohere client
cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))
# Directory to store uploads
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Initialize Google Generative AI embeddings
embeddings = GoogleGenerativeAIEmbeddings(
model="models/embedding-001",
api_key=os.getenv("GOOGLE_API_KEY"),
)
# Initialize or reset session states
if 'conversation_history' not in st.session_state:
st.session_state.conversation_history = []
if 'content' not in st.session_state:
st.session_state.content = ""
if 'processing' not in st.session_state:
st.session_state.processing = False
if 'current_question' not in st.session_state:
st.session_state.current_question = None
if 'uploaded_image' not in st.session_state:
st.session_state.uploaded_image = None
if 'uploaded_audio' not in st.session_state:
st.session_state.uploaded_audio = None
if 'uploaded_video' not in st.session_state:
st.session_state.uploaded_video = None
if 'vector_store' not in st.session_state:
st.session_state.vector_store = None
if 'documents_processed' not in st.session_state:
st.session_state.documents_processed = False
if 'audio_processed' not in st.session_state:
st.session_state.audio_processed = False
if "conversation_history" not in st.session_state:
st.session_state.conversation_history = []
if "content" not in st.session_state:
st.session_state.content = ""
# Page-wide Streamlit config
st.set_page_config(layout="wide", page_title="IntelliQuery")
# ===========================================
# 3) Utility Functions for File Handling
# ===========================================
def clear_old_index():
"""
Delete any old FAISS vector store index if it exists.
"""
index_folder = "vector_store_index"
if os.path.exists(index_folder):
shutil.rmtree(index_folder)
print("[INFO] Old FAISS index deleted successfully.")
audios_folder = "YoutubeAudios"
if os.path.exists(audios_folder):
shutil.rmtree(audios_folder)
print("[INFO] Old YouTube audios deleted successfully.")
def handle_file_upload(uploaded_file):
"""
Saves an uploaded file to the UPLOAD_FOLDER and returns the file path.
"""
if not uploaded_file:
return None
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
print(f"[INFO] File saved at: {file_path}")
return file_path
def load_excel_and_convert_to_csv(file):
"""
Read an Excel file and convert it to a CSV format.
Returns a string of CSV data.
"""
try:
df = pd.read_excel(file, engine="openpyxl")
print(f"[DEBUG] Loaded Excel with {df.shape[0]} rows and {df.shape[1]} columns.")
return df.to_csv(index=False)
except Exception as e:
print(f"[ERROR] Reading Excel file: {e}")
return f"Error: {str(e)}"
def get_ppt_content(file):
"""
Extract text content from each slide of a PPTX file.
"""
slides_content = []
prs = Presentation(file)
for slide in prs.slides:
slide_text = "".join(
[shape.text for shape in slide.shapes if hasattr(shape, "text")]
)
slides_content.append(slide_text)
print(f"[DEBUG] Extracted {len(slides_content)} slides from PPT.")
return "\n".join(slides_content)
def get_pdf_text(file):
"""
Extract all text from a PDF file.
"""
text = ""
pdf_reader = PdfReader(file)
for page in pdf_reader.pages:
text += page.extract_text()
print(f"[DEBUG] Extracted {len(text.split())} words from PDF.")
return text
# ===========================================
# 4) Audio & Video Processing Functions
# ===========================================
def extract_audio(video_path):
"""
Extract audio from a video file and save it as a WAV file.
Returns the path to the WAV file or None on failure.
"""
if not os.path.exists(video_path):
print(f"[ERROR] Video file does not exist: {video_path}")
return None
audio_output = os.path.splitext(video_path)[0] + ".wav"
try:
(
ffmpeg
.input(video_path)
.output(audio_output, format='wav', acodec='pcm_s16le', ar='16000')
.run(overwrite_output=True)
)
if os.path.exists(audio_output):
print(f"[INFO] Audio extracted and saved to: {audio_output}")
return audio_output
else:
print("[ERROR] Audio extraction failed.")
return None
except Exception as e:
print(f"[ERROR] Extracting audio: {e}")
return None
# Load a Whisper model for audio transcription
WHISPER_MODEL = whisper.load_model("base")
def transcribe_audio(audio_file_path):
"""
Transcribe speech from an audio file using Whisper.
Returns the transcription text.
"""
if not isinstance(audio_file_path, str) or not os.path.exists(audio_file_path):
print(f"[ERROR] Invalid file path: {audio_file_path}")
return None
try:
result = WHISPER_MODEL.transcribe(audio_file_path)
transcription = result["text"]
print(f"[DEBUG] Transcription preview: {transcription[:100]}...")
return transcription
except Exception as e:
print(f"[ERROR] Whisper Transcription Error: {e}")
return None
def transcribe_youtube_audio(audio_file_path):
"""
Transcribe audio using Whisper after validating the file exists.
"""
if not os.path.exists(audio_file_path):
print(f"[ERROR] File not found: {audio_file_path}")
return None
try:
result = WHISPER_MODEL.transcribe(audio_file_path)
transcription = result["text"]
print(f"[DEBUG] Transcription preview: {transcription[:100]}...")
return transcription
except Exception as e:
print(f"[ERROR] Whisper Transcription Error: {e}")
return None
def process_video(uploaded_video):
"""
Process an uploaded video file:
1. Upload and save the video locally.
2. Extract audio from the video.
3. Transcribe the audio using Whisper.
4. Update the Vector Store with the transcription.
5. Clean up temporary files.
"""
if not uploaded_video:
return False
try:
# Save the video file
video_path = handle_file_upload(uploaded_video)
if not video_path:
return False
# Extract audio
audio_path = extract_audio(video_path)
if not audio_path:
print("[ERROR] Failed to extract audio from video.")
return False
# Transcribe the audio
transcription = transcribe_audio(audio_path)
if transcription:
# Update session content
if 'content' not in st.session_state:
st.session_state.content = ""
st.session_state.content += transcription + "\n"
# Rebuild the vector store
clear_old_index()
text_chunks = get_text_chunks(st.session_state.content)
st.session_state.vector_store = get_vector_store(text_chunks, "vector_store_index")
st.session_state.documents_processed = True
# Clean up temporary files
try:
os.remove(video_path)
os.remove(audio_path)
print("[INFO] Temporary files cleaned up.")
except Exception as e:
print(f"[WARNING] Could not remove temporary files: {e}")
return True
return False
except Exception as e:
print(f"[ERROR] Processing video: {e}")
return False
def process_audio(uploaded_audio):
"""
Process an uploaded audio file:
1. Upload and save the audio locally.
2. Transcribe using Whisper.
3. Create or update the Vector Store with optimized chunks.
4. Clean up the temporary file.
"""
if not uploaded_audio:
return False
try:
# Save the audio file
audio_path = handle_file_upload(uploaded_audio)
if not audio_path:
return False
# Transcribe the audio
transcription = transcribe_audio(audio_path)
if transcription:
# Clear old index
clear_old_index()
# Break transcription into smaller chunks for better retrieval
text_chunks = RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=50,
separators=["\n\n", "\n", ".", "!", "?", ",", " "]
).split_text(transcription)
# Create the FAISS vector store
st.session_state.vector_store = FAISS.from_texts(
texts=text_chunks,
embedding=embeddings,
normalize_L2=True
)
# Save the updated vector store locally
st.session_state.vector_store.save_local("vector_store_index")
st.session_state.documents_processed = True
# Update session content
if 'content' not in st.session_state:
st.session_state.content = ""
st.session_state.content = transcription # Replace existing content
# Clean up temporary audio file
try:
os.remove(audio_path)
print(f"[INFO] Temporary file removed: {audio_path}")
except Exception as e:
print(f"[WARNING] Could not remove file: {e}")
return True
return False
except Exception as e:
print(f"[ERROR] Processing audio: {e}")
return False
def download_youtube_audio(youtube_url, output_path="YoutubeAudios"):
"""
Download audio from a YouTube URL using yt-dlp.
Returns the path to the downloaded audio file.
"""
try:
if not os.path.exists(output_path):
os.makedirs(output_path)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(output_path, f"youtube_audio_{timestamp}")
ydl_opts = {
"format": "bestaudio/best",
"outtmpl": filename,
"postprocessors": [{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192",
}],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
final_audio_path = f"{filename}.mp3"
if os.path.exists(final_audio_path):
print(f"[INFO] YouTube audio downloaded successfully: {final_audio_path}")
return final_audio_path
else:
print("[ERROR] YouTube audio download failed.")
return None
except Exception as e:
print(f"[ERROR] Downloading YouTube audio: {e}")
return None
def process_youtube_url(youtube_url):
"""
Process a YouTube URL:
1. Download the audio
2. Transcribe it
3. Update the Vector Store with the transcription
4. Clean up temporary files
"""
if not youtube_url or not youtube_url.strip():
return False
try:
# Download audio from YouTube
with st.spinner("Downloading audio from YouTube..."):
audio_path = download_youtube_audio(youtube_url)
if not audio_path:
st.error("Failed to download audio from YouTube.")
return False
# Transcribe the audio
with st.spinner("Transcribing audio..."):
transcription = transcribe_youtube_audio(audio_path)
if not transcription:
st.error("Failed to transcribe the audio.")
return False
# Update session content
if 'content' not in st.session_state:
st.session_state.content = ""
st.session_state.content += f"YouTube Transcription:\n{transcription}\n"
# Rebuild the vector store
clear_old_index()
text_chunks = get_text_chunks(st.session_state.content)
st.session_state.vector_store = get_vector_store(text_chunks, "vector_store_index")
st.session_state.documents_processed = True
# Clean up temporary files
try:
os.remove(audio_path)
print(f"[INFO] Temporary file removed: {audio_path}")
except Exception as e:
print(f"[WARNING] Could not remove file: {e}")
return True
except Exception as e:
print(f"[ERROR] Processing YouTube URL: {e}")
return False
# ===========================================
# 5) Text Processing & Vector Store Functions
# ===========================================
def get_text_chunks(text):
"""
Split the provided text into chunks for vector storage.
"""
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
return text_splitter.split_text(text)
def clean_text(text):
"""
Remove non-UTF-8 characters, emojis, and surrogate Unicode pairs to ensure
text is clean before embedding.
"""
if not text:
return ""
# Replace invalid UTF-8 chars
text = text.encode("utf-8", "replace").decode("utf-8")
# Remove emojis & special symbols
text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
return text.strip()
def get_vector_store(text_chunks, vector_store_path):
"""
Create a FAISS vector store from text chunks after cleaning.
"""
cleaned_chunks = [clean_text(chunk) for chunk in text_chunks]
vector_store = FAISS.from_texts(cleaned_chunks, embedding=embeddings)
vector_store.save_local(vector_store_path)
print(f"[INFO] FAISS index created with {len(cleaned_chunks)} chunks.")
return vector_store
def load_vector_store(vector_store_path):
"""
Load a FAISS vector store from the specified path, or return None if it doesn't exist.
"""
if not os.path.exists(vector_store_path):
print("[WARNING] No FAISS index found. Returning an empty vector store.")
return None
print(f"[INFO] FAISS index loaded from: {vector_store_path}")
return FAISS.load_local(vector_store_path, embeddings, allow_dangerous_deserialization=True)
# ===========================================
# 6) Q&A Chain and Retrieval Functions
# ===========================================
def get_conversational_chain():
"""
Return a Q&A chain configured for a ChatGoogleGenerativeAI model (Gemini 1.5).
"""
prompt_template = """
Answer the question as detailed as possible from the provided context.
If the answer is not in the context, respond with: "Answer is not available in the context."
Context:\n {context}\nQuestion:\n{question}\nAnswer:
"""
model = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest", temperature=0.3)
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
return load_qa_chain(model, chain_type="stuff", prompt=prompt)
def get_late_chunked_text(retrieved_docs, chunk_size=1000, chunk_overlap=100):
"""
Dynamically chunk retrieved documents to maintain structure in the final response.
"""
chunked_docs = []
for doc in retrieved_docs:
if isinstance(doc, Document):
text = clean_text(doc.page_content)
elif isinstance(doc, dict) and "page_content" in doc:
text = clean_text(doc["page_content"])
else:
text = clean_text(str(doc))
start = 0
while start < len(text):
chunked_docs.append(Document(page_content=text[start : start + chunk_size]))
start += chunk_size - chunk_overlap
return chunked_docs
def retrieve_documents(query):
"""
Search the available vector store using the query. If no store is available, returns an empty list.
"""
try:
if st.session_state.vector_store is not None:
vector_store = st.session_state.vector_store
else:
vector_store = load_vector_store("vector_store_index")
if vector_store:
st.session_state.vector_store = vector_store
if not vector_store:
print("[WARNING] No vector store available for retrieval.")
return []
retrieved_docs = vector_store.similarity_search(query)
# Debug info
print("\n==============================")
print(f"[DEBUG] Query: {query}")
print(f"[DEBUG] Retrieved {len(retrieved_docs)} relevant documents.")
for i, doc in enumerate(retrieved_docs[:3]):
print(f"[DEBUG] Document {i+1} preview: {doc.page_content[:200]}...")
print("==============================\n")
return retrieved_docs
except Exception as e:
print(f"[ERROR] Retrieving documents: {e}")
return []
def process_question(question, retrieved_docs):
"""
Process the user's question using retrieved documents and a Q&A chain.
"""
if not retrieved_docs:
print(f"[WARNING] No documents found for query: {question}")
return "No relevant documents found. Please upload documents first or try a different question."
chain = get_conversational_chain()
response = chain({"input_documents": retrieved_docs, "question": question}, return_only_outputs=True)
print(f"[DEBUG] Response generated preview: {response['output_text'][:200]}...")
return response["output_text"]
# ===========================================
# 7) Query Improvement and Processing
# ===========================================
def fetch_related_terms(query):
"""
Use Cohere to fetch a list of related terms for short queries, improving retrieval.
"""
try:
response = cohere_client.generate(
model="command",
prompt=(
f"Provide a list of related search terms (separated by commas) for improving retrieval. "
f"Do NOT change the meaning of the query: '{query}'"
),
max_tokens=15
)
related_terms = response.generations[0].text.strip()
# Clean and join the terms
related_terms = ", ".join([term.strip() for term in related_terms.split(",") if term.strip()])
print("\n==============================")
print(f"[DEBUG] Original Query: {query}")
print(f"[DEBUG] Related Terms: {related_terms}")
print("==============================\n")
return related_terms
except Exception as e:
print(f"[ERROR] Fetching related terms from Cohere: {e}")
return ""
def process_input(question):
"""
Decide how to process the question based on its length.
- For short queries, fetch related terms to improve retrieval.
- For longer queries, skip the related terms step.
"""
if len(question.split()) < 15:
related_terms = fetch_related_terms(question)
combined_query = f"{question} {related_terms}" if related_terms else question
retrieved_docs = retrieve_documents(combined_query)
return process_question(question, retrieved_docs)
else:
retrieved_docs = retrieve_documents(question)
return process_question(question, retrieved_docs)
def get_gemini_response(question, image):
"""
Generate a response from the Gemini model for an image-based query.
"""
model = genai.GenerativeModel('gemini-1.5-flash-latest')
try:
response = model.generate_content([question, image])
return response.text
except Exception as e:
return f"Error processing image: {str(e)}"
# ===========================================
# 8) Streamlit UI and Interaction
# ===========================================
def handle_submit():
"""
Handle the 'Send' button click and process user input.
"""
if st.session_state.user_input and st.session_state.user_input.strip():
if not (st.session_state.content or st.session_state.uploaded_image):
st.error("Please upload the documents before asking questions.")
return
st.session_state.current_question = st.session_state.user_input
st.session_state.processing = True
st.session_state.user_input = ""
# ===========================================
# 9) Generating Conversation PDF
# ===========================================
class ChatPDF(FPDF):
"""
Custom PDF class to format and store the conversation history.
"""
def __init__(self, file_names):
super().__init__()
self.file_names = file_names
self.set_auto_page_break(auto=True, margin=15)
self.add_page()
self.set_margins(20, 20, 20)
def header(self):
"""
Custom header with title, file references, and generation timestamp.
"""
self.set_font('helvetica', 'B', 16)
self.cell(0, 10, 'IntelliQuery Conversation', 0, 1, 'C')
self.ln(5)
if self.file_names:
self.set_font('helvetica', 'I', 10)
file_names_str = ", ".join(self.file_names)
self.cell(0, 10, f'Files: {file_names_str}', 0, 1, 'L')
self.set_font('helvetica', 'I', 10)
self.cell(0, 10, f'Generated on: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}', 0, 1, 'R')
self.line(10, self.get_y(), self.w - 10, self.get_y())
self.ln(10)
def footer(self):
"""
Custom footer with hyperlink and page number.
"""
self.set_y(-15)
self.set_font('helvetica', 'I', 8)
# Hyperlink (left)
self.set_x(10)
self.set_text_color(0, 102, 204)
self.cell(0, 10, 'IntelliQuery built by Pranav Vuddagiri', link="https://example.com", align='L')
# Page number (right)
self.set_x(-30)
self.set_text_color(0)
self.cell(0, 10, f'Page {self.page_no()}', align='R')
def add_message(self, role, content):
"""
Add a formatted role (User/Assistant) and content to the PDF.
"""
initial_y = self.get_y()
self.line(self.l_margin, initial_y - 2, self.w - self.r_margin, initial_y - 2)
self.ln(5)
message_start_y = self.get_y()
self.set_font('helvetica', 'B', 12)
role_label_width = 30
content_x_start = self.l_margin + role_label_width
content_width = self.w - self.r_margin - content_x_start
self.set_xy(self.l_margin, message_start_y)
self.cell(role_label_width, 10, f"{role}:", 0, 0, 'L')
self.set_font('helvetica', '', 11)
for line in content.splitlines():
self.set_xy(content_x_start, message_start_y)
self.multi_cell(content_width, 10, line, 0, 'J')
message_start_y = self.get_y()
self.ln(5)
def create_download_pdf(file_names):
"""
Generate a PDF of the conversation history and return it as bytes.
"""
try:
pdf = ChatPDF(file_names)
pdf.alias_nb_pages()
if "conversation_history" in st.session_state and st.session_state.conversation_history:
for question, answer in st.session_state.conversation_history:
pdf.add_message("User", question)
pdf.add_message("Assistant", answer)
else:
pdf.add_message("System", "No conversations available.")
return pdf.output(dest='S').encode('latin1')
except Exception as e:
st.error(f"Error generating PDF: {str(e)}")
return None
# ===========================================
# 10) QUIZ GENERATOR CLASS
# ===========================================
class QuizGenerator:
def __init__(self, vectorstore=None):
self.vectorstore = vectorstore
self.llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest", temperature=0.7)
self.system_template = """Generate {num_questions} {difficulty_instruction} questions ({type_instruction}) based EXCLUSIVELY on:
{context}
Difficulty Levels:
- Easy: Basic recall questions
- Medium: Application/understanding questions
- Hard: Analysis/evaluation questions
FORMAT AS PLAIN JSON (NO MARKDOWN):
{{
"questions": [
{{
"question": "question text",
"type": "mcq/essay",
"difficulty": "easy/medium/hard",
"options": ["option1", "option2", ...], // only for mcq
"correct": "correct answer" // only for mcq
}}
]
}}"""
def generate_quiz(self, num_questions=5, quiz_type='mix', difficulty='medium'):
try:
if not self.vectorstore:
raise ValueError("No documents processed")
# Instruction mapping
difficulty_instruction = {
'easy': "easy (basic recall)",
'medium': "medium (application/understanding)",
'hard': "hard (analysis/evaluation)"
}[difficulty]
type_instruction = {
'mcq': "all multiple choice questions",
'essay': "all essay questions",
'mix': "mix of MCQs and essay questions"
}[quiz_type]
context_docs = self.vectorstore.similarity_search("", k=7)
context = "\n\n".join([doc.page_content for doc in context_docs])
prompt = self.system_template.format(
num_questions=num_questions,
context=context,
type_instruction=type_instruction,
difficulty_instruction=difficulty_instruction
)
response = self.llm.invoke(prompt)
raw_response = response.content
# Clean and parse response
cleaned_response = raw_response.replace("```json", "").replace("```", "").strip()
try:
quiz_data = json.loads(cleaned_response)
valid_questions = []
for q in quiz_data.get("questions", []):
# Validate question structure
if q["type"] not in ["mcq", "essay"]:
continue
if q["type"] == "mcq" and not isinstance(q.get("options"), list):
continue
if q.get("difficulty") not in ["easy", "medium", "hard"]:
continue
valid_questions.append(q)
if len(valid_questions) != num_questions:
st.error(f"Generated {len(valid_questions)}/{num_questions} valid questions. Please try again.")
return []
return valid_questions
except Exception as e:
st.error(f"Invalid quiz format: {str(e)}")
return []
except Exception as e:
st.error(f"Quiz generation failed: {str(e)}")
return []
def evaluate_essay(question, answer):
evaluator = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest", temperature=0)
prompt = f"""
Evaluate this essay answer (1-10 scale):
Question: {question}
Answer: {answer}
Provide feedback in this format:
Score: [number]/10
Feedback: [detailed feedback]
"""
return evaluator.invoke(prompt).content
# ===========================================
# 11) QUIZ INTERFACE
# ===========================================
if st.session_state.get("quiz_page"):
st.markdown("""
<style>
.quiz-header {
border-bottom: 1px solid #444;
padding-bottom: 1rem;
margin-bottom: 2rem;
}
.quiz-config {
background: #1a1a1a;
padding: 1.5rem;
border-radius: 10px;
margin: 1rem 0;
}
.question-container {
background: #1a1a1a;
padding: 1.5rem;
border-radius: 8px;
margin: 1.5rem 0;
border: 1px solid #333;
}
.difficulty-badge {
font-size: 0.8rem;
padding: 0.2rem 0.5rem;
border-radius: 4px;
margin-left: 0.5rem;
}
.easy { background: #2e7d32; color: white; }
.medium { background: #f9a825; color: black; }
.hard { background: #c62828; color: white; }
.navigation-buttons {
margin-top: 2rem;
display: flex;
gap: 1rem;
justify-content: center;
}
</style>
""", unsafe_allow_html=True)
# Quiz header
st.markdown('<div class="quiz-header">', unsafe_allow_html=True)
st.title("📝 Document Quiz")
st.markdown("Test your knowledge based on the uploaded documents")
st.markdown('</div>', unsafe_allow_html=True)
# Initialize quiz state
if 'quiz_results' not in st.session_state:
st.session_state.quiz_results = {
'questions': [],
'answers': [],
'score': 0,
'completed': False
}
# Quiz configuration
if not st.session_state.quiz_results['questions']:
with st.form("quiz_config"):
st.markdown("### Quiz Settings")
with st.container():
num_questions = st.slider("Number of Questions", 3, 10, 5)
difficulty = st.selectbox("Difficulty Level",
["Easy", "Medium", "Hard"],
index=1)
quiz_type = st.radio("Question Types",
["MCQ Only", "Essay Only", "Mix of Both"],
index=2)
if st.form_submit_button("Generate Quiz", type="primary"):
generator = QuizGenerator(vectorstore=st.session_state.vector_store)
questions = generator.generate_quiz(
num_questions=num_questions,
quiz_type=quiz_type.split()[0].lower(),
difficulty=difficulty.lower()
)
if questions:
st.session_state.quiz_results = {
'questions': questions,
'answers': [None]*len(questions),
'score': 0,
'completed': False,
'current_idx': 0
}
st.rerun()
# Quiz interface
if st.session_state.quiz_results['questions'] and not st.session_state.quiz_results['completed']:
current_idx = st.session_state.quiz_results['current_idx']
question = st.session_state.quiz_results['questions'][current_idx]
with st.container():
# Question header
st.markdown(f"**Question {current_idx + 1} of {len(st.session_state.quiz_results['questions'])}**")
diff_badge = f'<span class="difficulty-badge {question["difficulty"]}">{question["difficulty"].capitalize()}</span>'
st.markdown(f'<div class="question-container">{question["question"]} {diff_badge}</div>',
unsafe_allow_html=True)
# Answer input
if question['type'] == 'mcq':
answer = st.radio("Select your answer:",
question['options'],
key=f"mcq_{current_idx}",
label_visibility="collapsed")
else:
answer = st.text_area("Your answer:",
height=150,
key=f"essay_{current_idx}",
placeholder="Type your essay here...")
# Navigation controls
cols = st.columns([1, 2, 1])
with cols[1]:
if current_idx < len(st.session_state.quiz_results['questions']) - 1:
if st.button("Next Question", type="primary", use_container_width=True):
st.session_state.quiz_results['answers'][current_idx] = answer
st.session_state.quiz_results['current_idx'] += 1
st.rerun()
else:
if st.button("Finish Quiz", type="primary", use_container_width=True):
st.session_state.quiz_results['answers'][current_idx] = answer
st.session_state.quiz_results['completed'] = True
st.rerun()
# Results display
if st.session_state.quiz_results.get('completed'):
st.markdown("## 📊 Quiz Results")
total_score = 0
for idx, (question, answer) in enumerate(zip(
st.session_state.quiz_results['questions'],
st.session_state.quiz_results['answers']
)):
with st.expander(f"Question {idx + 1}", expanded=False):
diff_badge = f'<span class="difficulty-badge {question["difficulty"]}">{question["difficulty"].capitalize()}</span>'
st.markdown(f'**{question["question"]}** {diff_badge}', unsafe_allow_html=True)
if question['type'] == 'mcq':
user_answer = answer if answer else "No answer provided"
st.markdown(f"**Your Answer:** {user_answer}")
st.markdown(f"**Correct Answer:** {question['correct']}")
if answer == question['correct']:
total_score += 1
st.success("✅ Correct")
else:
st.error("❌ Incorrect")
else:
with st.spinner("Evaluating essay..."):
evaluation = evaluate_essay(question['question'], answer)
st.markdown(f"**Your Answer:**\n{answer}")
st.markdown(f"**Evaluation:**\n{evaluation}")
try: