MBG0903 commited on
Commit
5d7cdd2
Β·
verified Β·
1 Parent(s): ec5a291

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -333
app.py CHANGED
@@ -1,6 +1,7 @@
1
  # ================================================================
2
- # Wine Quality Capstone - Interactive RAG Chatbot
3
- # Hugging Face Spaces + Gradio 6.x
 
4
  # ================================================================
5
 
6
  import os
@@ -21,8 +22,6 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
21
  APP_TITLE = "🍷 Wine Quality Research Assistant"
22
 
23
  EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
24
-
25
- # Lightweight model for Hugging Face CPU Space
26
  LLM_MODEL_ID = "google/flan-t5-small"
27
 
28
  TOP_K = 3
@@ -33,42 +32,30 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
33
 
34
 
35
  print("=" * 70)
36
- print("🍷 Starting Wine Quality Research Assistant")
37
  print("=" * 70)
38
 
39
  print(f"Device: {DEVICE}")
40
- print(f"Gradio version: {gr.__version__}")
41
 
42
 
43
  # ================================================================
44
- # 2. FIND PDF AUTOMATICALLY
45
  # ================================================================
46
 
47
  def find_pdf():
48
- """
49
- Automatically find the first PDF file in the
50
- Hugging Face Space repository.
51
- """
52
-
53
- files = os.listdir(".")
54
 
55
  pdf_files = [
56
  file
57
- for file in files
58
  if file.lower().endswith(".pdf")
59
  ]
60
 
61
  if not pdf_files:
62
 
63
- print("\nFiles currently available in the Space:")
64
-
65
- for file in files:
66
- print(" -", file)
67
-
68
  raise FileNotFoundError(
69
- "\nNo PDF file was found.\n"
70
- "Please upload the capstone PDF into the root "
71
- "folder of the Hugging Face Space."
72
  )
73
 
74
  print("\nPDF files detected:")
@@ -81,25 +68,25 @@ def find_pdf():
81
 
82
  PDF_FILE = find_pdf()
83
 
84
- print(f"\nβœ… Using PDF: {PDF_FILE}")
85
 
86
 
87
  # ================================================================
88
- # 3. TEXT CLEANING
89
  # ================================================================
90
 
91
  def clean_text(text):
92
- """
93
- Clean text extracted from the PDF.
94
- """
95
 
96
  if not text:
97
  return ""
98
 
99
  text = text.replace("\x00", " ")
100
 
101
- # Remove excessive whitespace
102
- text = re.sub(r"\s+", " ", text)
 
 
 
103
 
104
  return text.strip()
105
 
@@ -109,23 +96,21 @@ def clean_text(text):
109
  # ================================================================
110
 
111
  def read_pdf(pdf_path):
112
- """
113
- Extract text from each PDF page while preserving page numbers.
114
- """
115
-
116
- print("\nπŸ“– Reading PDF...")
117
 
118
  reader = PdfReader(pdf_path)
119
 
120
  pages = []
121
 
122
- for page_number, page in enumerate(reader.pages, start=1):
 
 
 
123
 
124
  try:
125
 
126
- text = page.extract_text()
127
-
128
- text = clean_text(text)
129
 
130
  if text:
131
 
@@ -139,7 +124,7 @@ def read_pdf(pdf_path):
139
  except Exception as error:
140
 
141
  print(
142
- f"Warning: Could not read page "
143
  f"{page_number}: {error}"
144
  )
145
 
@@ -152,31 +137,27 @@ pages = read_pdf(PDF_FILE)
152
  if not pages:
153
 
154
  raise ValueError(
155
- "The PDF was found, but no readable text "
156
  "could be extracted."
157
  )
158
 
159
 
160
  print(
161
- f"βœ… Text extracted from {len(pages)} pages."
162
  )
163
 
164
 
165
  # ================================================================
166
- # 5. CREATE KNOWLEDGE CHUNKS
167
  # ================================================================
168
 
169
  def create_chunks(pages):
170
- """
171
- Break extracted PDF text into overlapping chunks.
172
- """
173
 
174
  chunks = []
175
 
176
  for page_data in pages:
177
 
178
  text = page_data["text"]
179
-
180
  page_number = page_data["page"]
181
 
182
  start = 0
@@ -185,7 +166,9 @@ def create_chunks(pages):
185
 
186
  end = start + CHUNK_SIZE
187
 
188
- chunk_text = text[start:end].strip()
 
 
189
 
190
  if len(chunk_text) > 50:
191
 
@@ -196,23 +179,19 @@ def create_chunks(pages):
196
  }
197
  )
198
 
199
- start += CHUNK_SIZE - CHUNK_OVERLAP
 
 
 
200
 
201
  return chunks
202
 
203
 
204
  chunks = create_chunks(pages)
205
 
206
-
207
- if not chunks:
208
-
209
- raise ValueError(
210
- "No searchable chunks could be created from the PDF."
211
- )
212
-
213
-
214
  print(
215
- f"βœ… Created {len(chunks)} searchable knowledge chunks."
 
216
  )
217
 
218
 
@@ -220,7 +199,7 @@ print(
220
  # 6. LOAD EMBEDDING MODEL
221
  # ================================================================
222
 
223
- print("\nπŸ”Ž Loading embedding model...")
224
 
225
 
226
  embedding_model = SentenceTransformer(
@@ -229,16 +208,6 @@ embedding_model = SentenceTransformer(
229
  )
230
 
231
 
232
- print("βœ… Embedding model loaded.")
233
-
234
-
235
- # ================================================================
236
- # 7. CREATE DOCUMENT EMBEDDINGS
237
- # ================================================================
238
-
239
- print("\n🧠 Creating document embeddings...")
240
-
241
-
242
  document_texts = [
243
  chunk["text"]
244
  for chunk in chunks
@@ -253,18 +222,15 @@ document_embeddings = embedding_model.encode(
253
  )
254
 
255
 
256
- print(
257
- f"βœ… Embeddings created for "
258
- f"{len(document_embeddings)} chunks."
259
- )
260
 
261
 
262
  # ================================================================
263
- # 8. LOAD LANGUAGE MODEL
264
  # ================================================================
265
 
266
  print(
267
- f"\nπŸ€– Loading language model: "
268
  f"{LLM_MODEL_ID}"
269
  )
270
 
@@ -280,21 +246,17 @@ llm_model = AutoModelForSeq2SeqLM.from_pretrained(
280
 
281
 
282
  llm_model.to(DEVICE)
283
-
284
  llm_model.eval()
285
 
286
 
287
- print("βœ… Language model loaded successfully.")
288
 
289
 
290
  # ================================================================
291
- # 9. SEMANTIC SEARCH
292
  # ================================================================
293
 
294
- def retrieve_context(question, top_k=TOP_K):
295
- """
296
- Retrieve the most relevant chunks from the PDF.
297
- """
298
 
299
  question_embedding = embedding_model.encode(
300
  [question],
@@ -311,7 +273,7 @@ def retrieve_context(question, top_k=TOP_K):
311
 
312
  top_indices = np.argsort(
313
  similarity_scores
314
- )[-top_k:][::-1]
315
 
316
 
317
  results = []
@@ -321,11 +283,18 @@ def retrieve_context(question, top_k=TOP_K):
321
 
322
  results.append(
323
  {
324
- "page": chunks[index]["page"],
325
- "text": chunks[index]["text"],
326
- "score": float(
327
- similarity_scores[index]
328
- )
 
 
 
 
 
 
 
329
  }
330
  )
331
 
@@ -334,59 +303,45 @@ def retrieve_context(question, top_k=TOP_K):
334
 
335
 
336
  # ================================================================
337
- # 10. BUILD RETRIEVED CONTEXT
338
  # ================================================================
339
 
340
- def build_context(retrieved_chunks):
341
- """
342
- Combine retrieved PDF chunks into a single context.
343
- """
344
-
345
- context_parts = []
346
 
 
347
 
348
- for item in retrieved_chunks:
349
 
350
- context_parts.append(
351
  f"""
352
- SOURCE PAGE {item['page']}:
353
 
354
  {item['text']}
355
  """
356
  )
357
 
358
-
359
- return "\n\n".join(context_parts)
360
 
361
 
362
  # ================================================================
363
- # 11. FORMAT CHAT HISTORY
364
  # ================================================================
365
 
366
  def format_history(history):
367
- """
368
- Convert recent chat messages into plain text for the LLM.
369
- """
370
 
371
  if not history:
 
372
 
373
- return "No previous conversation."
374
-
375
-
376
- history_text = []
377
-
378
-
379
- # Keep only recent messages to prevent prompt growth
380
- recent_messages = history[-4:]
381
-
382
 
383
- for item in recent_messages:
 
384
 
385
  if isinstance(item, dict):
386
 
387
  role = item.get(
388
  "role",
389
- "user"
390
  )
391
 
392
  content = item.get(
@@ -394,80 +349,62 @@ def format_history(history):
394
  ""
395
  )
396
 
397
- history_text.append(
398
  f"{role}: {content}"
399
  )
400
 
401
-
402
- elif isinstance(item, (list, tuple)) and len(item) == 2:
403
-
404
- user_message = item[0]
405
- assistant_message = item[1]
406
-
407
- history_text.append(
408
- f"user: {user_message}"
409
- )
410
-
411
- history_text.append(
412
- f"assistant: {assistant_message}"
413
- )
414
-
415
-
416
- return "\n".join(history_text)
417
 
418
 
419
  # ================================================================
420
- # 12. GENERATE ANSWER
421
  # ================================================================
422
 
423
  def generate_answer(
424
  question,
425
  context,
426
- conversation_history
427
  ):
428
 
429
  prompt = f"""
430
- You are an academic research assistant for the capstone project:
431
 
432
- "Machine Learning-Based Prediction of Wine Quality Using
433
- Physicochemical Properties."
434
 
435
- The chatbot must answer questions using the supplied research
436
- document.
437
 
438
- IMPORTANT INSTRUCTIONS:
 
439
 
440
- 1. Use the DOCUMENT CONTEXT as the main source.
441
- 2. Do not invent facts.
442
- 3. Do not invent research findings.
443
- 4. Clearly distinguish planned methodology from completed work.
444
- 5. Do not claim that a model is the best-performing model unless
445
- the supplied document explicitly provides that result.
446
- 6. Explain technical concepts in simple language when appropriate.
447
- 7. Keep answers clear and reasonably concise.
448
- 8. Use bullet points when they improve readability.
449
- 9. If the requested information is not available in the document,
450
- say:
451
 
452
- "This information is not specified in the current capstone synopsis."
 
 
 
 
 
 
 
 
 
453
 
454
- 10. Stay consistent with the terminology used in the document.
 
455
 
456
  DOCUMENT CONTEXT:
457
 
458
  {context}
459
 
460
-
461
  RECENT CONVERSATION:
462
 
463
- {conversation_history}
464
-
465
 
466
- USER QUESTION:
467
 
468
  {question}
469
 
470
-
471
  ANSWER:
472
  """
473
 
@@ -482,25 +419,25 @@ ANSWER:
482
 
483
  inputs = {
484
  key: value.to(DEVICE)
485
- for key, value in inputs.items()
 
486
  }
487
 
488
 
489
  with torch.no_grad():
490
 
491
- outputs = llm_model.generate(
492
  **inputs,
493
  max_new_tokens=220,
494
- do_sample=False,
495
  num_beams=2,
 
496
  repetition_penalty=1.15,
497
- no_repeat_ngram_size=3,
498
- early_stopping=True
499
  )
500
 
501
 
502
  answer = tokenizer.decode(
503
- outputs[0],
504
  skip_special_tokens=True
505
  )
506
 
@@ -509,113 +446,88 @@ ANSWER:
509
 
510
 
511
  # ================================================================
512
- # 13. MAIN CHAT FUNCTION
513
  # ================================================================
514
 
515
  def chat(message, history):
516
- """
517
- Main function called by Gradio ChatInterface.
518
- """
519
 
520
- if not message or not message.strip():
521
 
522
  return (
523
- "Please enter a question about "
524
- "the capstone project."
525
  )
526
 
527
 
528
- print("\n" + "=" * 70)
529
-
530
  print(f"QUESTION: {message}")
531
 
532
 
533
- # ------------------------------------------------------------
534
- # Retrieve relevant PDF sections
535
- # ------------------------------------------------------------
536
-
537
- retrieved_chunks = retrieve_context(
538
  message
539
  )
540
 
541
 
542
- print("\nRelevant pages retrieved:")
543
 
544
-
545
- for item in retrieved_chunks:
546
 
547
  print(
548
  f"Page {item['page']} | "
549
- f"Similarity: {item['score']:.3f}"
550
  )
551
 
552
 
553
- # ------------------------------------------------------------
554
- # Build context
555
- # ------------------------------------------------------------
556
-
557
  context = build_context(
558
- retrieved_chunks
559
  )
560
 
561
 
562
- conversation_history = format_history(
563
  history
564
  )
565
 
566
 
567
- # ------------------------------------------------------------
568
- # Generate response
569
- # ------------------------------------------------------------
570
-
571
  answer = generate_answer(
572
  message,
573
  context,
574
- conversation_history
575
  )
576
 
577
 
578
- # ------------------------------------------------------------
579
- # Get unique page numbers
580
- # ------------------------------------------------------------
581
-
582
  source_pages = sorted(
583
  set(
584
  item["page"]
585
- for item in retrieved_chunks
586
  )
587
  )
588
 
589
 
590
- pages_string = ", ".join(
591
  str(page)
592
  for page in source_pages
593
  )
594
 
595
 
596
- # ------------------------------------------------------------
597
- # Return answer + source pages
598
- # ------------------------------------------------------------
599
-
600
  return f"""
601
  {answer}
602
 
603
  ---
604
 
605
- πŸ“š **Relevant source page(s): {pages_string}**
606
-
607
- πŸ“„ *Answer generated from the uploaded QM640 Capstone Synopsis.*
608
  """
609
 
610
 
611
  # ================================================================
612
- # 14. CUSTOM CSS
613
  # ================================================================
614
 
615
  css = """
616
 
617
  .gradio-container {
618
- max-width: 1100px !important;
619
  margin: auto !important;
620
  }
621
 
@@ -623,7 +535,7 @@ css = """
623
 
624
 
625
  # ================================================================
626
- # 15. GRADIO INTERFACE
627
  # ================================================================
628
 
629
  with gr.Blocks(
@@ -632,108 +544,52 @@ with gr.Blocks(
632
 
633
 
634
  # ------------------------------------------------------------
635
- # Header
636
  # ------------------------------------------------------------
637
 
638
  gr.Markdown(
639
  """
640
  # 🍷 Wine Quality Research Assistant
641
 
642
- ### Machine Learning-Based Prediction of Wine Quality Using Physicochemical Properties
643
-
644
- Welcome to the **QM640 Data Analytics Capstone Research Assistant**.
645
-
646
- This chatbot searches the uploaded capstone synopsis and retrieves
647
- the most relevant sections before generating an answer.
648
-
649
- You can ask questions about:
650
 
651
- ### πŸ“Š Research
 
652
 
653
- - Problem Statement
654
- - Purpose of the Study
655
- - Research Objectives
656
- - Research Questions
657
- - Research Hypotheses
658
-
659
- ### 🍷 Dataset
660
-
661
- - Wine Quality Dataset
662
- - Physicochemical Variables
663
- - Target Variable
664
- - Sample Size
665
- - Dataset Source
666
-
667
- ### πŸ€– Machine Learning
668
-
669
- - Linear Regression
670
- - Decision Tree Regression
671
- - Random Forest Regression
672
- - Gradient Boosting Regression
673
- - XGBoost Regression
674
-
675
- ### πŸ“ˆ Model Evaluation
676
-
677
- - MAE
678
- - RMSE
679
- - RΒ²
680
- - 80/20 Train-Test Split
681
- - 5-Fold Cross Validation
682
- - Hyperparameter Optimisation
683
-
684
- ### πŸ” Explainable AI
685
-
686
- - Feature Importance
687
- - SHAP
688
- - LIME
689
-
690
- ### πŸ’Ό Business
691
-
692
- - Business Impact
693
- - Quality Assessment
694
- - Production Optimisation
695
- - Study Limitations
696
-
697
- ---
698
  """
699
  )
700
 
701
 
702
  # ------------------------------------------------------------
703
- # Knowledge Base Status
704
  # ------------------------------------------------------------
705
 
706
  gr.Markdown(
707
  f"""
708
- ### βœ… Knowledge Base Loaded
709
-
710
- πŸ“„ **PDF:** `{PDF_FILE}`
711
-
712
- πŸ“‘ **Pages loaded:** {len(pages)}
713
-
714
- 🧩 **Searchable chunks:** {len(chunks)}
715
-
716
- πŸ”Ž **Embedding model:** `{EMBEDDING_MODEL_ID}`
717
-
718
- πŸ€– **Language model:** `{LLM_MODEL_ID}`
719
-
720
- πŸ’» **Device:** `{DEVICE}`
721
  """
722
  )
723
 
724
 
725
  # ------------------------------------------------------------
726
- # Chatbot
727
  # ------------------------------------------------------------
728
 
729
  chatbot_component = gr.Chatbot(
730
- height=500,
731
- show_label=False
 
 
 
 
732
  )
733
 
734
 
735
  # ------------------------------------------------------------
736
- # Chat Interface
737
  # ------------------------------------------------------------
738
 
739
  chat_interface = gr.ChatInterface(
@@ -742,41 +598,31 @@ You can ask questions about:
742
 
743
  chatbot=chatbot_component,
744
 
745
- examples=[
746
-
747
- "What is the main objective of this study?",
 
 
 
 
748
 
749
- "What problem is this research trying to solve?",
750
 
751
- "What dataset is being used?",
752
 
753
- "How many observations are in the dataset?",
754
 
755
- "What are the predictor variables?",
756
 
757
- "What is the target variable?",
758
 
759
- "Explain the four research questions.",
760
 
761
  "Which machine learning models will be compared?",
762
 
763
- "How will the models be validated?",
764
-
765
- "Explain 5-fold cross-validation in simple terms.",
766
-
767
- "What is MAE?",
768
-
769
- "What is RMSE?",
770
-
771
- "What does RΒ² mean?",
772
-
773
- "How will SHAP be used?",
774
-
775
- "What is LIME?",
776
 
777
- "What is the business value of this research?",
778
-
779
- "What are the limitations of the study?"
780
 
781
  ]
782
 
@@ -784,35 +630,15 @@ You can ask questions about:
784
 
785
 
786
  # ------------------------------------------------------------
787
- # Explanation
788
  # ------------------------------------------------------------
789
 
790
  gr.Markdown(
791
  """
792
  ---
793
 
794
- ## πŸ”Ž How this chatbot works
795
-
796
- **Your Question**
797
-
798
- ↓
799
-
800
- **Semantic Search**
801
-
802
- ↓
803
-
804
- **Relevant Sections Retrieved from the PDF**
805
-
806
- ↓
807
-
808
- **Relevant Content Sent to FLAN-T5**
809
-
810
- ↓
811
-
812
- **AI Generated Answer**
813
-
814
- This is a lightweight **Retrieval-Augmented Generation (RAG)**
815
- implementation.
816
 
817
  *QM640 Data Analytics Capstone*
818
  """
@@ -820,13 +646,11 @@ implementation.
820
 
821
 
822
  # ================================================================
823
- # 16. START APPLICATION
824
  # ================================================================
825
 
826
  print("\n" + "=" * 70)
827
-
828
- print("βœ… Wine Quality Research Assistant is ready.")
829
-
830
  print("=" * 70)
831
 
832
 
 
1
  # ================================================================
2
+ # Wine Quality Research Assistant
3
+ # Simple RAG Chatbot for Hugging Face Spaces
4
+ # Gradio 6.x Compatible
5
  # ================================================================
6
 
7
  import os
 
22
  APP_TITLE = "🍷 Wine Quality Research Assistant"
23
 
24
  EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
 
 
25
  LLM_MODEL_ID = "google/flan-t5-small"
26
 
27
  TOP_K = 3
 
32
 
33
 
34
  print("=" * 70)
35
+ print("Starting Wine Quality Research Assistant")
36
  print("=" * 70)
37
 
38
  print(f"Device: {DEVICE}")
39
+ print(f"Gradio: {gr.__version__}")
40
 
41
 
42
  # ================================================================
43
+ # 2. FIND PDF
44
  # ================================================================
45
 
46
  def find_pdf():
 
 
 
 
 
 
47
 
48
  pdf_files = [
49
  file
50
+ for file in os.listdir(".")
51
  if file.lower().endswith(".pdf")
52
  ]
53
 
54
  if not pdf_files:
55
 
 
 
 
 
 
56
  raise FileNotFoundError(
57
+ "No PDF found. Upload the capstone PDF "
58
+ "to the root folder of the Space."
 
59
  )
60
 
61
  print("\nPDF files detected:")
 
68
 
69
  PDF_FILE = find_pdf()
70
 
71
+ print(f"\nUsing PDF: {PDF_FILE}")
72
 
73
 
74
  # ================================================================
75
+ # 3. CLEAN PDF TEXT
76
  # ================================================================
77
 
78
  def clean_text(text):
 
 
 
79
 
80
  if not text:
81
  return ""
82
 
83
  text = text.replace("\x00", " ")
84
 
85
+ text = re.sub(
86
+ r"\s+",
87
+ " ",
88
+ text
89
+ )
90
 
91
  return text.strip()
92
 
 
96
  # ================================================================
97
 
98
  def read_pdf(pdf_path):
 
 
 
 
 
99
 
100
  reader = PdfReader(pdf_path)
101
 
102
  pages = []
103
 
104
+ for page_number, page in enumerate(
105
+ reader.pages,
106
+ start=1
107
+ ):
108
 
109
  try:
110
 
111
+ text = clean_text(
112
+ page.extract_text()
113
+ )
114
 
115
  if text:
116
 
 
124
  except Exception as error:
125
 
126
  print(
127
+ f"Could not read page "
128
  f"{page_number}: {error}"
129
  )
130
 
 
137
  if not pages:
138
 
139
  raise ValueError(
140
+ "PDF found, but no readable text "
141
  "could be extracted."
142
  )
143
 
144
 
145
  print(
146
+ f"Loaded {len(pages)} PDF pages."
147
  )
148
 
149
 
150
  # ================================================================
151
+ # 5. CREATE CHUNKS
152
  # ================================================================
153
 
154
  def create_chunks(pages):
 
 
 
155
 
156
  chunks = []
157
 
158
  for page_data in pages:
159
 
160
  text = page_data["text"]
 
161
  page_number = page_data["page"]
162
 
163
  start = 0
 
166
 
167
  end = start + CHUNK_SIZE
168
 
169
+ chunk_text = text[
170
+ start:end
171
+ ].strip()
172
 
173
  if len(chunk_text) > 50:
174
 
 
179
  }
180
  )
181
 
182
+ start += (
183
+ CHUNK_SIZE -
184
+ CHUNK_OVERLAP
185
+ )
186
 
187
  return chunks
188
 
189
 
190
  chunks = create_chunks(pages)
191
 
 
 
 
 
 
 
 
 
192
  print(
193
+ f"Created {len(chunks)} "
194
+ f"knowledge chunks."
195
  )
196
 
197
 
 
199
  # 6. LOAD EMBEDDING MODEL
200
  # ================================================================
201
 
202
+ print("Loading embedding model...")
203
 
204
 
205
  embedding_model = SentenceTransformer(
 
208
  )
209
 
210
 
 
 
 
 
 
 
 
 
 
 
211
  document_texts = [
212
  chunk["text"]
213
  for chunk in chunks
 
222
  )
223
 
224
 
225
+ print("Embedding model ready.")
 
 
 
226
 
227
 
228
  # ================================================================
229
+ # 7. LOAD LANGUAGE MODEL
230
  # ================================================================
231
 
232
  print(
233
+ f"Loading language model: "
234
  f"{LLM_MODEL_ID}"
235
  )
236
 
 
246
 
247
 
248
  llm_model.to(DEVICE)
 
249
  llm_model.eval()
250
 
251
 
252
+ print("Language model ready.")
253
 
254
 
255
  # ================================================================
256
+ # 8. RETRIEVE RELEVANT PDF CONTENT
257
  # ================================================================
258
 
259
+ def retrieve_context(question):
 
 
 
260
 
261
  question_embedding = embedding_model.encode(
262
  [question],
 
273
 
274
  top_indices = np.argsort(
275
  similarity_scores
276
+ )[-TOP_K:][::-1]
277
 
278
 
279
  results = []
 
283
 
284
  results.append(
285
  {
286
+ "page":
287
+ chunks[index]["page"],
288
+
289
+ "text":
290
+ chunks[index]["text"],
291
+
292
+ "score":
293
+ float(
294
+ similarity_scores[
295
+ index
296
+ ]
297
+ )
298
  }
299
  )
300
 
 
303
 
304
 
305
  # ================================================================
306
+ # 9. BUILD CONTEXT
307
  # ================================================================
308
 
309
+ def build_context(results):
 
 
 
 
 
310
 
311
+ sections = []
312
 
313
+ for item in results:
314
 
315
+ sections.append(
316
  f"""
317
+ PAGE {item['page']}:
318
 
319
  {item['text']}
320
  """
321
  )
322
 
323
+ return "\n".join(sections)
 
324
 
325
 
326
  # ================================================================
327
+ # 10. FORMAT CHAT HISTORY
328
  # ================================================================
329
 
330
  def format_history(history):
 
 
 
331
 
332
  if not history:
333
+ return ""
334
 
335
+ conversation = []
 
 
 
 
 
 
 
 
336
 
337
+ # Only retain recent conversation
338
+ for item in history[-4:]:
339
 
340
  if isinstance(item, dict):
341
 
342
  role = item.get(
343
  "role",
344
+ ""
345
  )
346
 
347
  content = item.get(
 
349
  ""
350
  )
351
 
352
+ conversation.append(
353
  f"{role}: {content}"
354
  )
355
 
356
+ return "\n".join(conversation)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
 
358
 
359
  # ================================================================
360
+ # 11. GENERATE ANSWER
361
  # ================================================================
362
 
363
  def generate_answer(
364
  question,
365
  context,
366
+ history
367
  ):
368
 
369
  prompt = f"""
370
+ You are a helpful academic research assistant.
371
 
372
+ You answer questions about the research project:
 
373
 
374
+ "Machine Learning-Based Prediction of Wine Quality
375
+ Using Physicochemical Properties."
376
 
377
+ Answer the user's question using ONLY the information
378
+ provided in the DOCUMENT CONTEXT.
379
 
380
+ RULES:
 
 
 
 
 
 
 
 
 
 
381
 
382
+ 1. Answer the question directly.
383
+ 2. Use clear and simple language.
384
+ 3. Be academically accurate.
385
+ 4. Do not invent information.
386
+ 5. Do not invent research results.
387
+ 6. Clearly distinguish proposed methodology from
388
+ completed findings.
389
+ 7. Use bullet points when helpful.
390
+ 8. Keep the answer focused on the user's question.
391
+ 9. If the document does not contain the answer, say:
392
 
393
+ "This information is not specified in the current
394
+ capstone synopsis."
395
 
396
  DOCUMENT CONTEXT:
397
 
398
  {context}
399
 
 
400
  RECENT CONVERSATION:
401
 
402
+ {history}
 
403
 
404
+ QUESTION:
405
 
406
  {question}
407
 
 
408
  ANSWER:
409
  """
410
 
 
419
 
420
  inputs = {
421
  key: value.to(DEVICE)
422
+ for key, value
423
+ in inputs.items()
424
  }
425
 
426
 
427
  with torch.no_grad():
428
 
429
+ output = llm_model.generate(
430
  **inputs,
431
  max_new_tokens=220,
 
432
  num_beams=2,
433
+ do_sample=False,
434
  repetition_penalty=1.15,
435
+ no_repeat_ngram_size=3
 
436
  )
437
 
438
 
439
  answer = tokenizer.decode(
440
+ output[0],
441
  skip_special_tokens=True
442
  )
443
 
 
446
 
447
 
448
  # ================================================================
449
+ # 12. CHAT FUNCTION
450
  # ================================================================
451
 
452
  def chat(message, history):
 
 
 
453
 
454
+ if not message.strip():
455
 
456
  return (
457
+ "Please enter a question "
458
+ "about the document."
459
  )
460
 
461
 
462
+ print("\n" + "=" * 60)
 
463
  print(f"QUESTION: {message}")
464
 
465
 
466
+ # Retrieve relevant content
467
+ results = retrieve_context(
 
 
 
468
  message
469
  )
470
 
471
 
472
+ print("Retrieved:")
473
 
474
+ for item in results:
 
475
 
476
  print(
477
  f"Page {item['page']} | "
478
+ f"Score: {item['score']:.3f}"
479
  )
480
 
481
 
 
 
 
 
482
  context = build_context(
483
+ results
484
  )
485
 
486
 
487
+ history_text = format_history(
488
  history
489
  )
490
 
491
 
 
 
 
 
492
  answer = generate_answer(
493
  message,
494
  context,
495
+ history_text
496
  )
497
 
498
 
499
+ # Unique page numbers
 
 
 
500
  source_pages = sorted(
501
  set(
502
  item["page"]
503
+ for item in results
504
  )
505
  )
506
 
507
 
508
+ page_text = ", ".join(
509
  str(page)
510
  for page in source_pages
511
  )
512
 
513
 
 
 
 
 
514
  return f"""
515
  {answer}
516
 
517
  ---
518
 
519
+ πŸ“š **Source page(s): {page_text}**
 
 
520
  """
521
 
522
 
523
  # ================================================================
524
+ # 13. CSS
525
  # ================================================================
526
 
527
  css = """
528
 
529
  .gradio-container {
530
+ max-width: 1000px !important;
531
  margin: auto !important;
532
  }
533
 
 
535
 
536
 
537
  # ================================================================
538
+ # 14. USER INTERFACE
539
  # ================================================================
540
 
541
  with gr.Blocks(
 
544
 
545
 
546
  # ------------------------------------------------------------
547
+ # SIMPLE HEADER
548
  # ------------------------------------------------------------
549
 
550
  gr.Markdown(
551
  """
552
  # 🍷 Wine Quality Research Assistant
553
 
554
+ ### Ask questions about the QM640 Wine Quality Capstone
 
 
 
 
 
 
 
555
 
556
+ This chatbot searches the uploaded capstone document and
557
+ answers your questions using the relevant information from it.
558
 
559
+ **Type your question below to begin.**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  """
561
  )
562
 
563
 
564
  # ------------------------------------------------------------
565
+ # SMALL STATUS LINE
566
  # ------------------------------------------------------------
567
 
568
  gr.Markdown(
569
  f"""
570
+ πŸ“„ **Document loaded:** `{PDF_FILE}`
571
+ πŸ“‘ **Pages:** {len(pages)}    |   
572
+ 🧩 **Knowledge chunks:** {len(chunks)}
 
 
 
 
 
 
 
 
 
 
573
  """
574
  )
575
 
576
 
577
  # ------------------------------------------------------------
578
+ # CHATBOT
579
  # ------------------------------------------------------------
580
 
581
  chatbot_component = gr.Chatbot(
582
+ height=480,
583
+ show_label=False,
584
+ placeholder=(
585
+ "🍷 Ask me anything about "
586
+ "the Wine Quality Capstone."
587
+ )
588
  )
589
 
590
 
591
  # ------------------------------------------------------------
592
+ # CHAT INTERFACE
593
  # ------------------------------------------------------------
594
 
595
  chat_interface = gr.ChatInterface(
 
598
 
599
  chatbot=chatbot_component,
600
 
601
+ textbox=gr.Textbox(
602
+ placeholder=(
603
+ "Ask a question about "
604
+ "the document..."
605
+ ),
606
+ show_label=False
607
+ ),
608
 
609
+ submit_btn="Ask",
610
 
611
+ stop_btn=False,
612
 
613
+ examples=[
614
 
615
+ "What is the main objective of the study?",
616
 
617
+ "What dataset is used in this project?",
618
 
619
+ "What are the research questions?",
620
 
621
  "Which machine learning models will be compared?",
622
 
623
+ "How will model performance be evaluated?",
 
 
 
 
 
 
 
 
 
 
 
 
624
 
625
+ "What is the business value of this research?"
 
 
626
 
627
  ]
628
 
 
630
 
631
 
632
  # ------------------------------------------------------------
633
+ # FOOTER
634
  # ------------------------------------------------------------
635
 
636
  gr.Markdown(
637
  """
638
  ---
639
 
640
+ πŸ”Ž **RAG-powered:** Your question β†’ Relevant PDF sections β†’
641
+ AI-generated answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
642
 
643
  *QM640 Data Analytics Capstone*
644
  """
 
646
 
647
 
648
  # ================================================================
649
+ # 15. LAUNCH
650
  # ================================================================
651
 
652
  print("\n" + "=" * 70)
653
+ print("Wine Quality Research Assistant is ready.")
 
 
654
  print("=" * 70)
655
 
656