MBG0903 commited on
Commit
e45b86d
Β·
verified Β·
1 Parent(s): 713ca83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +433 -180
app.py CHANGED
@@ -11,10 +11,7 @@ import gradio as gr
11
 
12
  from pypdf import PdfReader
13
  from sentence_transformers import SentenceTransformer
14
- from transformers import (
15
- AutoTokenizer,
16
- AutoModelForSeq2SeqLM
17
- )
18
 
19
 
20
  # ================================================================
@@ -23,76 +20,49 @@ from transformers import (
23
 
24
  APP_TITLE = "🍷 Wine Quality Research Assistant"
25
 
26
- APP_DESCRIPTION = """
27
- Ask questions about the **Wine Quality Machine Learning Capstone Project**.
28
-
29
- The assistant can explain:
30
-
31
- - Research problem and objectives
32
- - Research questions
33
- - Wine Quality Dataset
34
- - Physicochemical variables
35
- - Machine learning models
36
- - Model validation
37
- - MAE, RMSE and RΒ²
38
- - Hyperparameter optimisation
39
- - SHAP and LIME
40
- - Business impact
41
- - Project methodology
42
-
43
- Responses are generated primarily from the uploaded capstone synopsis.
44
- """
45
 
46
- # Lightweight models suitable for CPU Hugging Face Spaces
47
  EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
48
 
49
- # flan-t5-small is intentionally used for fast CPU inference.
50
- # If your Space has more RAM/GPU, change this to:
51
- # google/flan-t5-base
52
- LLM_MODEL_ID = os.getenv(
53
- "LLM_MODEL_ID",
54
- "google/flan-t5-small"
55
- )
56
 
57
  TOP_K = 4
58
- CHUNK_SIZE = 900
59
- CHUNK_OVERLAP = 150
60
 
61
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
62
 
63
 
64
  # ================================================================
65
- # 2. FIND PDF AUTOMATICALLY
66
  # ================================================================
67
 
68
- def find_pdf():
69
- """
70
- Finds a PDF file in the current Hugging Face Space.
71
- """
72
 
73
- preferred_file = os.getenv("PDF_FILE")
 
74
 
75
- if preferred_file and os.path.exists(preferred_file):
76
- return preferred_file
77
 
78
- pdf_files = [
79
- file
80
- for file in os.listdir(".")
81
- if file.lower().endswith(".pdf")
82
- ]
83
 
84
- if not pdf_files:
85
- raise FileNotFoundError(
86
- "No PDF file found. Please upload your capstone PDF "
87
- "to the Hugging Face Space."
88
- )
89
 
90
- return pdf_files[0]
 
91
 
 
 
 
 
 
92
 
93
- PDF_FILE = find_pdf()
94
 
95
- print(f"Using knowledge document: {PDF_FILE}")
96
 
97
 
98
  # ================================================================
@@ -100,22 +70,21 @@ print(f"Using knowledge document: {PDF_FILE}")
100
  # ================================================================
101
 
102
  def clean_text(text):
103
- """
104
- Cleans PDF extracted text.
105
- """
106
 
107
  text = text.replace("\x00", " ")
108
 
109
- # Join broken whitespace
110
  text = re.sub(r"\s+", " ", text)
111
 
112
  return text.strip()
113
 
114
 
115
  def read_pdf(pdf_path):
116
- """
117
- Extract text from PDF while retaining page numbers.
118
- """
119
 
120
  reader = PdfReader(pdf_path)
121
 
@@ -125,8 +94,9 @@ def read_pdf(pdf_path):
125
 
126
  text = page.extract_text()
127
 
 
 
128
  if text:
129
- text = clean_text(text)
130
 
131
  pages.append(
132
  {
@@ -140,17 +110,21 @@ def read_pdf(pdf_path):
140
 
141
  pages = read_pdf(PDF_FILE)
142
 
143
- print(f"Loaded {len(pages)} PDF pages.")
 
 
 
 
 
 
 
144
 
145
 
146
  # ================================================================
147
- # 4. CHUNK DOCUMENT
148
  # ================================================================
149
 
150
  def create_chunks(pages):
151
- """
152
- Break each PDF page into smaller overlapping chunks.
153
- """
154
 
155
  chunks = []
156
 
@@ -167,7 +141,7 @@ def create_chunks(pages):
167
 
168
  chunk_text = text[start:end].strip()
169
 
170
- if chunk_text:
171
 
172
  chunks.append(
173
  {
@@ -183,290 +157,405 @@ def create_chunks(pages):
183
 
184
  chunks = create_chunks(pages)
185
 
186
- print(f"Created {len(chunks)} knowledge chunks.")
187
 
188
 
189
  # ================================================================
190
  # 5. LOAD EMBEDDING MODEL
191
  # ================================================================
192
 
193
- print("Loading embedding model...")
194
 
195
  embedding_model = SentenceTransformer(
196
  EMBEDDING_MODEL_ID,
197
  device=DEVICE
198
  )
199
 
 
 
 
 
 
 
 
 
 
200
 
201
- # Create embeddings once when the Space starts
202
  document_texts = [
 
203
  chunk["text"]
 
204
  for chunk in chunks
 
205
  ]
206
 
 
207
  document_embeddings = embedding_model.encode(
 
208
  document_texts,
 
209
  convert_to_numpy=True,
 
210
  normalize_embeddings=True,
 
211
  show_progress_bar=False
 
212
  )
213
 
214
- print("Document embeddings created.")
 
 
 
 
215
 
216
 
217
  # ================================================================
218
- # 6. LOAD LANGUAGE MODEL
219
  # ================================================================
220
 
221
- print(f"Loading language model: {LLM_MODEL_ID}")
 
222
 
223
  tokenizer = AutoTokenizer.from_pretrained(
224
  LLM_MODEL_ID
225
  )
226
 
227
- model = AutoModelForSeq2SeqLM.from_pretrained(
 
228
  LLM_MODEL_ID
229
  )
230
 
231
- model.to(DEVICE)
232
- model.eval()
233
 
234
- print("Language model loaded.")
 
 
 
 
 
235
 
236
 
237
  # ================================================================
238
- # 7. SEMANTIC SEARCH
239
  # ================================================================
240
 
241
  def retrieve_context(question, top_k=TOP_K):
242
- """
243
- Retrieve the document chunks most relevant to the question.
244
- """
245
 
246
  question_embedding = embedding_model.encode(
 
247
  [question],
 
248
  convert_to_numpy=True,
 
249
  normalize_embeddings=True
 
250
  )[0]
251
 
252
- # Cosine similarity because embeddings are normalized
253
  similarity_scores = np.dot(
 
254
  document_embeddings,
 
255
  question_embedding
 
256
  )
257
 
 
258
  top_indices = np.argsort(
259
  similarity_scores
260
  )[-top_k:][::-1]
261
 
262
- retrieved_chunks = []
 
 
263
 
264
  for index in top_indices:
265
 
266
- retrieved_chunks.append(
267
  {
268
  "page": chunks[index]["page"],
 
269
  "text": chunks[index]["text"],
270
- "score": float(similarity_scores[index])
 
 
 
271
  }
272
  )
273
 
274
- return retrieved_chunks
 
275
 
276
 
277
  # ================================================================
278
- # 8. CREATE CONTEXT FOR THE LLM
279
  # ================================================================
280
 
281
  def build_context(retrieved_chunks):
282
 
283
  context_parts = []
284
 
 
285
  for item in retrieved_chunks:
286
 
287
  context_parts.append(
 
288
  f"""
289
- SOURCE PAGE {item['page']}:
 
290
  {item['text']}
291
  """
292
  )
293
 
294
- return "\n".join(context_parts)
 
295
 
296
 
297
  # ================================================================
298
- # 9. FORMAT CHAT HISTORY
299
  # ================================================================
300
 
301
- def format_history(history, max_turns=2):
302
- """
303
- Keep only a small amount of conversation history.
304
- This makes follow-up questions more natural without creating
305
- an excessively large prompt.
306
- """
307
 
308
  if not history:
309
- return ""
 
 
310
 
311
  conversation = []
312
 
313
- recent_history = history[-max_turns:]
 
 
 
314
 
315
  for item in recent_history:
316
 
317
- # Gradio messages format
318
  if isinstance(item, dict):
319
 
320
  role = item.get("role", "")
 
321
  content = item.get("content", "")
322
 
323
  conversation.append(
324
  f"{role}: {content}"
325
  )
326
 
327
- # Compatibility with older Gradio tuple history
328
- elif isinstance(item, (list, tuple)) and len(item) == 2:
329
-
330
- user_message, assistant_message = item
331
-
332
- conversation.append(
333
- f"User: {user_message}"
334
- )
335
-
336
- conversation.append(
337
- f"Assistant: {assistant_message}"
338
- )
339
 
340
  return "\n".join(conversation)
341
 
342
 
343
  # ================================================================
344
- # 10. GENERATE ANSWER
345
  # ================================================================
346
 
347
  def generate_answer(question, context, history_text):
348
 
349
  prompt = f"""
350
- You are an academic research assistant for a Data Analytics
351
- Capstone project titled:
 
 
 
 
 
 
 
 
352
 
353
- "Machine Learning-Based Prediction of Wine Quality Using
354
- Physicochemical Properties."
355
 
356
- Answer the user's question using ONLY the information provided
357
- in the research document context below.
 
 
 
 
 
 
358
 
359
- Instructions:
 
 
 
 
 
 
360
 
361
- 1. Give a clear and easy-to-understand answer.
362
- 2. Be conversational but academically accurate.
363
- 3. When useful, explain technical concepts in simple language.
364
- 4. Do not invent research results that are not present.
365
- 5. Distinguish between planned methodology and completed results.
366
- 6. If the information is not available in the document, say:
367
- "This information is not specified in the current capstone synopsis."
368
- 7. Keep answers concise unless the user asks for detail.
369
- 8. Do not claim that the proposed models have already produced
370
- results when the document only describes planned analysis.
371
 
372
  DOCUMENT CONTEXT:
373
 
374
  {context}
375
 
 
376
  RECENT CONVERSATION:
377
 
378
  {history_text}
379
 
 
380
  USER QUESTION:
381
 
382
  {question}
383
 
 
384
  ANSWER:
385
  """
386
 
 
387
  inputs = tokenizer(
 
388
  prompt,
 
389
  return_tensors="pt",
 
390
  truncation=True,
 
391
  max_length=2048
 
392
  ).to(DEVICE)
393
 
 
394
  with torch.no_grad():
395
 
396
- outputs = model.generate(
 
397
  **inputs,
398
- max_new_tokens=250,
399
- min_new_tokens=20,
 
400
  do_sample=False,
 
401
  num_beams=3,
 
402
  repetition_penalty=1.15,
 
403
  no_repeat_ngram_size=3,
 
404
  early_stopping=True
 
405
  )
406
 
 
407
  answer = tokenizer.decode(
 
408
  outputs[0],
 
409
  skip_special_tokens=True
 
410
  )
411
 
 
412
  return answer.strip()
413
 
414
 
415
  # ================================================================
416
- # 11. CHATBOT FUNCTION
417
  # ================================================================
418
 
419
- def chatbot(message, history):
420
 
421
  if not message or not message.strip():
 
422
  return "Please enter a question about the capstone project."
423
 
 
424
  # ------------------------------------------------------------
425
- # Retrieve relevant portions of the synopsis
426
  # ------------------------------------------------------------
427
 
428
  retrieved_chunks = retrieve_context(message)
429
 
430
- context = build_context(retrieved_chunks)
431
 
432
- history_text = format_history(history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433
 
434
  # ------------------------------------------------------------
435
- # Generate response
436
  # ------------------------------------------------------------
437
 
438
  answer = generate_answer(
 
439
  message,
 
440
  context,
 
441
  history_text
 
442
  )
443
 
 
444
  # ------------------------------------------------------------
445
- # Show source pages
446
  # ------------------------------------------------------------
447
 
448
  source_pages = sorted(
 
449
  set(
 
450
  item["page"]
 
451
  for item in retrieved_chunks
 
452
  )
 
453
  )
454
 
455
- pages_text = ", ".join(
 
 
456
  str(page)
 
457
  for page in source_pages
458
- )
459
 
460
- final_answer = (
461
- f"{answer}\n\n"
462
- f"πŸ“š **Relevant source page(s): {pages_text}**"
463
  )
464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
  return final_answer
466
 
467
 
468
  # ================================================================
469
- # 12. GRADIO USER INTERFACE
470
  # ================================================================
471
 
472
  custom_css = """
@@ -484,98 +573,239 @@ footer {
484
 
485
 
486
  with gr.Blocks(
 
487
  title=APP_TITLE,
 
488
  css=custom_css
 
489
  ) as demo:
490
 
 
 
 
 
 
491
  gr.Markdown(
492
- f"""
493
- # {APP_TITLE}
 
 
 
 
 
 
 
494
 
495
- ### Machine Learning-Based Prediction of Wine Quality
496
 
497
- {APP_DESCRIPTION}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
 
499
  ---
500
  """
501
  )
502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
  chatbot_ui = gr.Chatbot(
504
- height=520,
505
- placeholder="""
506
- 🍷 Welcome!
507
 
508
- I am your Wine Quality Capstone Research Assistant.
 
 
 
 
 
 
 
509
 
510
  Try asking:
511
 
512
- β€’ What is the objective of this research?
513
- β€’ What dataset is being used?
514
- β€’ What are the research questions?
515
- β€’ Why are five regression models compared?
516
- β€’ What is SHAP?
517
- β€’ How will model performance be evaluated?
518
- β€’ What is the business value of this project?
519
- """,
520
- show_label=False
 
 
521
  )
522
 
 
 
 
 
 
523
  with gr.Row():
524
 
525
  user_input = gr.Textbox(
526
- placeholder="Ask a question about the capstone project...",
527
- label="Your Question",
 
 
 
 
528
  scale=8
 
529
  )
530
 
 
531
  submit_button = gr.Button(
532
- "Ask",
 
 
533
  variant="primary",
 
534
  scale=1
 
535
  )
536
 
 
537
  clear_button = gr.Button(
538
- "Clear Conversation"
539
  )
540
 
 
 
 
 
 
541
  gr.Examples(
 
542
  examples=[
543
- "What is the main problem this research is trying to solve?",
544
- "What are the objectives of the study?",
545
- "Explain the four research questions.",
 
 
546
  "What dataset is being used?",
 
 
 
547
  "What are the predictor variables?",
 
 
 
 
 
548
  "Which machine learning models will be compared?",
549
- "Why is Random Forest being used?",
550
  "How will the models be validated?",
 
 
 
551
  "What are MAE, RMSE and RΒ²?",
552
- "How will SHAP be used in this project?",
 
 
 
 
553
  "What is the business value of this research?",
 
554
  "What are the limitations of the study?"
 
555
  ],
 
556
  inputs=user_input
 
557
  )
558
 
 
 
 
 
 
559
  gr.Markdown(
560
  """
561
  ---
562
- **Knowledge source:** QM640 Data Analytics Capstone Synopsis
563
- **Architecture:** PDF Retrieval + Semantic Search + Generative AI
 
 
 
 
 
 
 
564
  """
565
  )
566
 
567
- # ------------------------------------------------------------
568
- # Event handling
569
- # ------------------------------------------------------------
 
570
 
571
  def respond(message, chat_history):
572
 
573
- response = chatbot(
 
 
 
 
574
  message,
 
575
  chat_history
 
576
  )
577
 
578
- chat_history = chat_history or []
579
 
580
  chat_history.append(
581
  {
@@ -584,6 +814,7 @@ Try asking:
584
  }
585
  )
586
 
 
587
  chat_history.append(
588
  {
589
  "role": "assistant",
@@ -591,43 +822,65 @@ Try asking:
591
  }
592
  )
593
 
 
594
  return "", chat_history
595
 
596
 
 
597
  submit_button.click(
 
598
  respond,
 
599
  inputs=[
600
  user_input,
601
  chatbot_ui
602
  ],
 
603
  outputs=[
604
  user_input,
605
  chatbot_ui
606
  ]
 
607
  )
608
 
 
 
609
  user_input.submit(
 
610
  respond,
 
611
  inputs=[
612
  user_input,
613
  chatbot_ui
614
  ],
 
615
  outputs=[
616
  user_input,
617
  chatbot_ui
618
  ]
 
619
  )
620
 
 
 
621
  clear_button.click(
 
622
  lambda: [],
 
623
  outputs=chatbot_ui
 
624
  )
625
 
626
 
627
  # ================================================================
628
- # 13. START APPLICATION
629
  # ================================================================
630
 
 
 
 
 
 
631
  if __name__ == "__main__":
632
 
633
  demo.queue()
 
11
 
12
  from pypdf import PdfReader
13
  from sentence_transformers import SentenceTransformer
14
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
 
15
 
16
 
17
  # ================================================================
 
20
 
21
  APP_TITLE = "🍷 Wine Quality Research Assistant"
22
 
23
+ # IMPORTANT:
24
+ # Upload this PDF into the SAME root folder as app.py
25
+ PDF_FILE = "Mahesh_Govindaraj_QM640_Data_Analytics_Capstone_Project_Synopsis.pdf"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
 
27
  EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
28
 
29
+ # Lightweight model suitable for Hugging Face CPU Space
30
+ LLM_MODEL_ID = "google/flan-t5-small"
 
 
 
 
 
31
 
32
  TOP_K = 4
33
+ CHUNK_SIZE = 800
34
+ CHUNK_OVERLAP = 120
35
 
36
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
37
 
38
 
39
  # ================================================================
40
+ # 2. VERIFY PDF
41
  # ================================================================
42
 
43
+ print("=" * 60)
44
+ print("Starting Wine Quality Research Assistant")
45
+ print("=" * 60)
 
46
 
47
+ print(f"Running on device: {DEVICE}")
48
+ print(f"Looking for PDF: {PDF_FILE}")
49
 
 
 
50
 
51
+ if not os.path.exists(PDF_FILE):
 
 
 
 
52
 
53
+ print("\nFiles currently available in Space:")
 
 
 
 
54
 
55
+ for file in os.listdir("."):
56
+ print(" -", file)
57
 
58
+ raise FileNotFoundError(
59
+ f"\nPDF file not found: {PDF_FILE}\n"
60
+ "Please upload the PDF to the root folder of your "
61
+ "Hugging Face Space."
62
+ )
63
 
 
64
 
65
+ print(f"βœ… PDF found successfully: {PDF_FILE}")
66
 
67
 
68
  # ================================================================
 
70
  # ================================================================
71
 
72
  def clean_text(text):
73
+
74
+ if not text:
75
+ return ""
76
 
77
  text = text.replace("\x00", " ")
78
 
79
+ # Remove excessive whitespace
80
  text = re.sub(r"\s+", " ", text)
81
 
82
  return text.strip()
83
 
84
 
85
  def read_pdf(pdf_path):
86
+
87
+ print("\nReading PDF...")
 
88
 
89
  reader = PdfReader(pdf_path)
90
 
 
94
 
95
  text = page.extract_text()
96
 
97
+ text = clean_text(text)
98
+
99
  if text:
 
100
 
101
  pages.append(
102
  {
 
110
 
111
  pages = read_pdf(PDF_FILE)
112
 
113
+ print(f"βœ… Successfully extracted text from {len(pages)} pages.")
114
+
115
+
116
+ if len(pages) == 0:
117
+
118
+ raise ValueError(
119
+ "No readable text could be extracted from the PDF."
120
+ )
121
 
122
 
123
  # ================================================================
124
+ # 4. CREATE DOCUMENT CHUNKS
125
  # ================================================================
126
 
127
  def create_chunks(pages):
 
 
 
128
 
129
  chunks = []
130
 
 
141
 
142
  chunk_text = text[start:end].strip()
143
 
144
+ if len(chunk_text) > 50:
145
 
146
  chunks.append(
147
  {
 
157
 
158
  chunks = create_chunks(pages)
159
 
160
+ print(f"βœ… Created {len(chunks)} searchable knowledge chunks.")
161
 
162
 
163
  # ================================================================
164
  # 5. LOAD EMBEDDING MODEL
165
  # ================================================================
166
 
167
+ print("\nLoading embedding model...")
168
 
169
  embedding_model = SentenceTransformer(
170
  EMBEDDING_MODEL_ID,
171
  device=DEVICE
172
  )
173
 
174
+ print("βœ… Embedding model loaded.")
175
+
176
+
177
+ # ================================================================
178
+ # 6. CREATE DOCUMENT EMBEDDINGS
179
+ # ================================================================
180
+
181
+ print("\nCreating document embeddings...")
182
+
183
 
 
184
  document_texts = [
185
+
186
  chunk["text"]
187
+
188
  for chunk in chunks
189
+
190
  ]
191
 
192
+
193
  document_embeddings = embedding_model.encode(
194
+
195
  document_texts,
196
+
197
  convert_to_numpy=True,
198
+
199
  normalize_embeddings=True,
200
+
201
  show_progress_bar=False
202
+
203
  )
204
 
205
+
206
+ print(
207
+ f"βœ… Created embeddings for "
208
+ f"{len(document_embeddings)} document chunks."
209
+ )
210
 
211
 
212
  # ================================================================
213
+ # 7. LOAD FLAN-T5
214
  # ================================================================
215
 
216
+ print(f"\nLoading language model: {LLM_MODEL_ID}")
217
+
218
 
219
  tokenizer = AutoTokenizer.from_pretrained(
220
  LLM_MODEL_ID
221
  )
222
 
223
+
224
+ llm_model = AutoModelForSeq2SeqLM.from_pretrained(
225
  LLM_MODEL_ID
226
  )
227
 
 
 
228
 
229
+ llm_model.to(DEVICE)
230
+
231
+ llm_model.eval()
232
+
233
+
234
+ print("βœ… Language model loaded.")
235
 
236
 
237
  # ================================================================
238
+ # 8. SEMANTIC SEARCH
239
  # ================================================================
240
 
241
  def retrieve_context(question, top_k=TOP_K):
 
 
 
242
 
243
  question_embedding = embedding_model.encode(
244
+
245
  [question],
246
+
247
  convert_to_numpy=True,
248
+
249
  normalize_embeddings=True
250
+
251
  )[0]
252
 
253
+
254
  similarity_scores = np.dot(
255
+
256
  document_embeddings,
257
+
258
  question_embedding
259
+
260
  )
261
 
262
+
263
  top_indices = np.argsort(
264
  similarity_scores
265
  )[-top_k:][::-1]
266
 
267
+
268
+ results = []
269
+
270
 
271
  for index in top_indices:
272
 
273
+ results.append(
274
  {
275
  "page": chunks[index]["page"],
276
+
277
  "text": chunks[index]["text"],
278
+
279
+ "score": float(
280
+ similarity_scores[index]
281
+ )
282
  }
283
  )
284
 
285
+
286
+ return results
287
 
288
 
289
  # ================================================================
290
+ # 9. BUILD RETRIEVED CONTEXT
291
  # ================================================================
292
 
293
  def build_context(retrieved_chunks):
294
 
295
  context_parts = []
296
 
297
+
298
  for item in retrieved_chunks:
299
 
300
  context_parts.append(
301
+
302
  f"""
303
+ SOURCE PAGE {item['page']}
304
+
305
  {item['text']}
306
  """
307
  )
308
 
309
+
310
+ return "\n\n".join(context_parts)
311
 
312
 
313
  # ================================================================
314
+ # 10. FORMAT CHAT HISTORY
315
  # ================================================================
316
 
317
+ def format_history(history):
 
 
 
 
 
318
 
319
  if not history:
320
+
321
+ return "No previous conversation."
322
+
323
 
324
  conversation = []
325
 
326
+
327
+ # Only use recent messages
328
+ recent_history = history[-4:]
329
+
330
 
331
  for item in recent_history:
332
 
 
333
  if isinstance(item, dict):
334
 
335
  role = item.get("role", "")
336
+
337
  content = item.get("content", "")
338
 
339
  conversation.append(
340
  f"{role}: {content}"
341
  )
342
 
 
 
 
 
 
 
 
 
 
 
 
 
343
 
344
  return "\n".join(conversation)
345
 
346
 
347
  # ================================================================
348
+ # 11. GENERATE ANSWER
349
  # ================================================================
350
 
351
  def generate_answer(question, context, history_text):
352
 
353
  prompt = f"""
354
+ You are an intelligent academic research assistant for the
355
+ Data Analytics Capstone project:
356
+
357
+ Machine Learning-Based Prediction of Wine Quality Using
358
+ Physicochemical Properties.
359
+
360
+ The research document belongs to Mahesh Govindaraj.
361
+
362
+ Your job is to answer questions based ONLY on the supplied
363
+ research document.
364
 
365
+ IMPORTANT RULES:
 
366
 
367
+ 1. Use the DOCUMENT CONTEXT as the primary source.
368
+ 2. Do not invent information.
369
+ 3. Do not invent model results.
370
+ 4. Clearly distinguish planned methodology from completed work.
371
+ 5. Explain technical concepts in simple language when appropriate.
372
+ 6. Give concise but informative answers.
373
+ 7. Use bullet points when they improve readability.
374
+ 8. If the answer is not available in the document, say:
375
 
376
+ "This information is not specified in the current capstone synopsis."
377
+
378
+ 9. Do not claim that a model performed best unless the document
379
+ explicitly provides that result.
380
+ 10. When discussing the dataset, models, research questions,
381
+ methodology or business value, remain consistent with the
382
+ supplied document.
383
 
 
 
 
 
 
 
 
 
 
 
384
 
385
  DOCUMENT CONTEXT:
386
 
387
  {context}
388
 
389
+
390
  RECENT CONVERSATION:
391
 
392
  {history_text}
393
 
394
+
395
  USER QUESTION:
396
 
397
  {question}
398
 
399
+
400
  ANSWER:
401
  """
402
 
403
+
404
  inputs = tokenizer(
405
+
406
  prompt,
407
+
408
  return_tensors="pt",
409
+
410
  truncation=True,
411
+
412
  max_length=2048
413
+
414
  ).to(DEVICE)
415
 
416
+
417
  with torch.no_grad():
418
 
419
+ outputs = llm_model.generate(
420
+
421
  **inputs,
422
+
423
+ max_new_tokens=220,
424
+
425
  do_sample=False,
426
+
427
  num_beams=3,
428
+
429
  repetition_penalty=1.15,
430
+
431
  no_repeat_ngram_size=3,
432
+
433
  early_stopping=True
434
+
435
  )
436
 
437
+
438
  answer = tokenizer.decode(
439
+
440
  outputs[0],
441
+
442
  skip_special_tokens=True
443
+
444
  )
445
 
446
+
447
  return answer.strip()
448
 
449
 
450
  # ================================================================
451
+ # 12. MAIN CHATBOT FUNCTION
452
  # ================================================================
453
 
454
+ def chatbot_response(message, history):
455
 
456
  if not message or not message.strip():
457
+
458
  return "Please enter a question about the capstone project."
459
 
460
+
461
  # ------------------------------------------------------------
462
+ # Retrieve relevant document content
463
  # ------------------------------------------------------------
464
 
465
  retrieved_chunks = retrieve_context(message)
466
 
 
467
 
468
+ # Debug information in HF logs
469
+ print("\n" + "=" * 60)
470
+
471
+ print(f"QUESTION: {message}")
472
+
473
+ print("Retrieved document pages:")
474
+
475
+
476
+ for item in retrieved_chunks:
477
+
478
+ print(
479
+ f"Page {item['page']} "
480
+ f"| Similarity: {item['score']:.3f}"
481
+ )
482
+
483
+
484
+ # ------------------------------------------------------------
485
+ # Build LLM context
486
+ # ------------------------------------------------------------
487
+
488
+ context = build_context(
489
+ retrieved_chunks
490
+ )
491
+
492
+
493
+ history_text = format_history(
494
+ history
495
+ )
496
+
497
 
498
  # ------------------------------------------------------------
499
+ # Generate answer
500
  # ------------------------------------------------------------
501
 
502
  answer = generate_answer(
503
+
504
  message,
505
+
506
  context,
507
+
508
  history_text
509
+
510
  )
511
 
512
+
513
  # ------------------------------------------------------------
514
+ # Extract unique source pages
515
  # ------------------------------------------------------------
516
 
517
  source_pages = sorted(
518
+
519
  set(
520
+
521
  item["page"]
522
+
523
  for item in retrieved_chunks
524
+
525
  )
526
+
527
  )
528
 
529
+
530
+ page_list = ", ".join(
531
+
532
  str(page)
533
+
534
  for page in source_pages
 
535
 
 
 
 
536
  )
537
 
538
+
539
+ # ------------------------------------------------------------
540
+ # Add source information
541
+ # ------------------------------------------------------------
542
+
543
+ final_answer = f"""
544
+ {answer}
545
+
546
+ ---
547
+
548
+ πŸ“š **Source pages:** {page_list}
549
+
550
+ πŸ“„ **Knowledge source:** QM640 Data Analytics Capstone Synopsis
551
+ """
552
+
553
+
554
  return final_answer
555
 
556
 
557
  # ================================================================
558
+ # 13. GRADIO INTERFACE
559
  # ================================================================
560
 
561
  custom_css = """
 
573
 
574
 
575
  with gr.Blocks(
576
+
577
  title=APP_TITLE,
578
+
579
  css=custom_css
580
+
581
  ) as demo:
582
 
583
+
584
+ # ------------------------------------------------------------
585
+ # Header
586
+ # ------------------------------------------------------------
587
+
588
  gr.Markdown(
589
+ """
590
+ # 🍷 Wine Quality Research Assistant
591
+
592
+ ### Machine Learning-Based Prediction of Wine Quality Using Physicochemical Properties
593
+
594
+ Ask questions about the **QM640 Data Analytics Capstone Project**.
595
+
596
+ This AI assistant searches the uploaded research synopsis before
597
+ generating an answer.
598
 
599
+ ---
600
 
601
+ ### πŸ’‘ You can ask about
602
+
603
+ **Research**
604
+ β€’ Problem Statement
605
+ β€’ Objectives
606
+ β€’ Research Questions
607
+ β€’ Hypotheses
608
+
609
+ **Dataset**
610
+ β€’ Wine Quality Dataset
611
+ β€’ Predictor Variables
612
+ β€’ Target Variable
613
+ β€’ Sample Size
614
+
615
+ **Machine Learning**
616
+ β€’ Linear Regression
617
+ β€’ Decision Tree
618
+ β€’ Random Forest
619
+ β€’ Gradient Boosting
620
+ β€’ XGBoost
621
+
622
+ **Evaluation & Explainability**
623
+ β€’ MAE
624
+ β€’ RMSE
625
+ β€’ RΒ²
626
+ β€’ Cross Validation
627
+ β€’ Hyperparameter Optimisation
628
+ β€’ SHAP
629
+ β€’ LIME
630
+
631
+ **Business**
632
+ β€’ Business Impact
633
+ β€’ Practical Applications
634
+ β€’ Study Limitations
635
 
636
  ---
637
  """
638
  )
639
 
640
+
641
+ # ------------------------------------------------------------
642
+ # PDF status
643
+ # ------------------------------------------------------------
644
+
645
+ gr.Markdown(
646
+ f"""
647
+ ### βœ… Knowledge Base Loaded
648
+
649
+ πŸ“„ **Document:** `{PDF_FILE}`
650
+
651
+ πŸ“‘ **Pages extracted:** {len(pages)}
652
+
653
+ 🧩 **Knowledge chunks:** {len(chunks)}
654
+
655
+ πŸ€– **Language model:** `{LLM_MODEL_ID}`
656
+
657
+ πŸ”Ž **Embedding model:** `{EMBEDDING_MODEL_ID}`
658
+ """
659
+ )
660
+
661
+
662
+ # ------------------------------------------------------------
663
+ # Chat window
664
+ # ------------------------------------------------------------
665
+
666
  chatbot_ui = gr.Chatbot(
 
 
 
667
 
668
+ type="messages",
669
+
670
+ height=500,
671
+
672
+ show_label=False,
673
+
674
+ placeholder="""
675
+ 🍷 Welcome to the Wine Quality Research Assistant!
676
 
677
  Try asking:
678
 
679
+ "What is the main objective of the study?"
680
+
681
+ "What dataset is being used?"
682
+
683
+ "What are the research questions?"
684
+
685
+ "Which machine learning models will be compared?"
686
+
687
+ "How will SHAP be used?"
688
+ """
689
+
690
  )
691
 
692
+
693
+ # ------------------------------------------------------------
694
+ # Input area
695
+ # ------------------------------------------------------------
696
+
697
  with gr.Row():
698
 
699
  user_input = gr.Textbox(
700
+
701
+ placeholder=
702
+ "Ask a question about the Wine Quality Capstone...",
703
+
704
+ show_label=False,
705
+
706
  scale=8
707
+
708
  )
709
 
710
+
711
  submit_button = gr.Button(
712
+
713
+ "Ask 🍷",
714
+
715
  variant="primary",
716
+
717
  scale=1
718
+
719
  )
720
 
721
+
722
  clear_button = gr.Button(
723
+ "πŸ—‘οΈ Clear Conversation"
724
  )
725
 
726
+
727
+ # ------------------------------------------------------------
728
+ # Example questions
729
+ # ------------------------------------------------------------
730
+
731
  gr.Examples(
732
+
733
  examples=[
734
+
735
+ "What is the main objective of this research?",
736
+
737
+ "What problem is this project trying to solve?",
738
+
739
  "What dataset is being used?",
740
+
741
+ "How many observations are in the dataset?",
742
+
743
  "What are the predictor variables?",
744
+
745
+ "What is the target variable?",
746
+
747
+ "Explain the four research questions.",
748
+
749
  "Which machine learning models will be compared?",
750
+
751
  "How will the models be validated?",
752
+
753
+ "What is 5-fold cross-validation?",
754
+
755
  "What are MAE, RMSE and RΒ²?",
756
+
757
+ "How will SHAP be used?",
758
+
759
+ "What is the role of LIME?",
760
+
761
  "What is the business value of this research?",
762
+
763
  "What are the limitations of the study?"
764
+
765
  ],
766
+
767
  inputs=user_input
768
+
769
  )
770
 
771
+
772
+ # ------------------------------------------------------------
773
+ # Footer
774
+ # ------------------------------------------------------------
775
+
776
  gr.Markdown(
777
  """
778
  ---
779
+
780
+ ### πŸ”Ž How this assistant works
781
+
782
+ **Question β†’ Semantic Search β†’ Relevant PDF Content β†’ AI β†’ Answer**
783
+
784
+ The chatbot uses Retrieval-Augmented Generation (RAG) to search
785
+ the capstone synopsis before generating its response.
786
+
787
+ *QM640 Data Analytics Capstone – Wine Quality Prediction*
788
  """
789
  )
790
 
791
+
792
+ # ============================================================
793
+ # 14. CHAT EVENTS
794
+ # ============================================================
795
 
796
  def respond(message, chat_history):
797
 
798
+ chat_history = chat_history or []
799
+
800
+
801
+ response = chatbot_response(
802
+
803
  message,
804
+
805
  chat_history
806
+
807
  )
808
 
 
809
 
810
  chat_history.append(
811
  {
 
814
  }
815
  )
816
 
817
+
818
  chat_history.append(
819
  {
820
  "role": "assistant",
 
822
  }
823
  )
824
 
825
+
826
  return "", chat_history
827
 
828
 
829
+ # Button click
830
  submit_button.click(
831
+
832
  respond,
833
+
834
  inputs=[
835
  user_input,
836
  chatbot_ui
837
  ],
838
+
839
  outputs=[
840
  user_input,
841
  chatbot_ui
842
  ]
843
+
844
  )
845
 
846
+
847
+ # Enter key
848
  user_input.submit(
849
+
850
  respond,
851
+
852
  inputs=[
853
  user_input,
854
  chatbot_ui
855
  ],
856
+
857
  outputs=[
858
  user_input,
859
  chatbot_ui
860
  ]
861
+
862
  )
863
 
864
+
865
+ # Clear
866
  clear_button.click(
867
+
868
  lambda: [],
869
+
870
  outputs=chatbot_ui
871
+
872
  )
873
 
874
 
875
  # ================================================================
876
+ # 15. LAUNCH APPLICATION
877
  # ================================================================
878
 
879
+ print("\n" + "=" * 60)
880
+ print("βœ… Wine Quality Research Assistant is ready!")
881
+ print("=" * 60)
882
+
883
+
884
  if __name__ == "__main__":
885
 
886
  demo.queue()