Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
# ================================================================
|
| 2 |
# Wine Quality Capstone - Interactive RAG Chatbot
|
| 3 |
-
# Hugging Face Spaces
|
| 4 |
-
# Compatible with Gradio 6.x
|
| 5 |
# ================================================================
|
| 6 |
|
| 7 |
import os
|
|
@@ -23,7 +22,7 @@ APP_TITLE = "π· Wine Quality Research Assistant"
|
|
| 23 |
|
| 24 |
EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
|
| 25 |
|
| 26 |
-
#
|
| 27 |
LLM_MODEL_ID = "google/flan-t5-small"
|
| 28 |
|
| 29 |
TOP_K = 3
|
|
@@ -38,7 +37,7 @@ print("π· Starting Wine Quality Research Assistant")
|
|
| 38 |
print("=" * 70)
|
| 39 |
|
| 40 |
print(f"Device: {DEVICE}")
|
| 41 |
-
print(f"Gradio
|
| 42 |
|
| 43 |
|
| 44 |
# ================================================================
|
|
@@ -47,7 +46,8 @@ print(f"Gradio Version: {gr.__version__}")
|
|
| 47 |
|
| 48 |
def find_pdf():
|
| 49 |
"""
|
| 50 |
-
Automatically
|
|
|
|
| 51 |
"""
|
| 52 |
|
| 53 |
files = os.listdir(".")
|
|
@@ -60,15 +60,15 @@ def find_pdf():
|
|
| 60 |
|
| 61 |
if not pdf_files:
|
| 62 |
|
| 63 |
-
print("\nFiles
|
| 64 |
|
| 65 |
for file in files:
|
| 66 |
print(" -", file)
|
| 67 |
|
| 68 |
raise FileNotFoundError(
|
| 69 |
-
"\nNo PDF file found.\n"
|
| 70 |
-
"Please upload
|
| 71 |
-
"of the Hugging Face Space."
|
| 72 |
)
|
| 73 |
|
| 74 |
print("\nPDF files detected:")
|
|
@@ -85,17 +85,20 @@ print(f"\nβ
Using PDF: {PDF_FILE}")
|
|
| 85 |
|
| 86 |
|
| 87 |
# ================================================================
|
| 88 |
-
# 3.
|
| 89 |
# ================================================================
|
| 90 |
|
| 91 |
def clean_text(text):
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if not text:
|
| 94 |
return ""
|
| 95 |
|
| 96 |
text = text.replace("\x00", " ")
|
| 97 |
|
| 98 |
-
#
|
| 99 |
text = re.sub(r"\s+", " ", text)
|
| 100 |
|
| 101 |
return text.strip()
|
|
@@ -106,6 +109,9 @@ def clean_text(text):
|
|
| 106 |
# ================================================================
|
| 107 |
|
| 108 |
def read_pdf(pdf_path):
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
print("\nπ Reading PDF...")
|
| 111 |
|
|
@@ -146,7 +152,7 @@ pages = read_pdf(PDF_FILE)
|
|
| 146 |
if not pages:
|
| 147 |
|
| 148 |
raise ValueError(
|
| 149 |
-
"The PDF was found but no readable text "
|
| 150 |
"could be extracted."
|
| 151 |
)
|
| 152 |
|
|
@@ -161,6 +167,9 @@ print(
|
|
| 161 |
# ================================================================
|
| 162 |
|
| 163 |
def create_chunks(pages):
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
chunks = []
|
| 166 |
|
|
@@ -195,6 +204,13 @@ def create_chunks(pages):
|
|
| 195 |
chunks = create_chunks(pages)
|
| 196 |
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
print(
|
| 199 |
f"β
Created {len(chunks)} searchable knowledge chunks."
|
| 200 |
)
|
|
@@ -206,16 +222,18 @@ print(
|
|
| 206 |
|
| 207 |
print("\nπ Loading embedding model...")
|
| 208 |
|
|
|
|
| 209 |
embedding_model = SentenceTransformer(
|
| 210 |
EMBEDDING_MODEL_ID,
|
| 211 |
device=DEVICE
|
| 212 |
)
|
| 213 |
|
|
|
|
| 214 |
print("β
Embedding model loaded.")
|
| 215 |
|
| 216 |
|
| 217 |
# ================================================================
|
| 218 |
-
# 7. CREATE EMBEDDINGS
|
| 219 |
# ================================================================
|
| 220 |
|
| 221 |
print("\nπ§ Creating document embeddings...")
|
|
@@ -274,6 +292,9 @@ print("β
Language model loaded successfully.")
|
|
| 274 |
# ================================================================
|
| 275 |
|
| 276 |
def retrieve_context(question, top_k=TOP_K):
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
question_embedding = embedding_model.encode(
|
| 279 |
[question],
|
|
@@ -313,13 +334,17 @@ def retrieve_context(question, top_k=TOP_K):
|
|
| 313 |
|
| 314 |
|
| 315 |
# ================================================================
|
| 316 |
-
# 10. BUILD CONTEXT
|
| 317 |
# ================================================================
|
| 318 |
|
| 319 |
def build_context(retrieved_chunks):
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
context_parts = []
|
| 322 |
|
|
|
|
| 323 |
for item in retrieved_chunks:
|
| 324 |
|
| 325 |
context_parts.append(
|
|
@@ -330,14 +355,18 @@ SOURCE PAGE {item['page']}:
|
|
| 330 |
"""
|
| 331 |
)
|
| 332 |
|
| 333 |
-
|
|
|
|
| 334 |
|
| 335 |
|
| 336 |
# ================================================================
|
| 337 |
-
# 11.
|
| 338 |
# ================================================================
|
| 339 |
|
| 340 |
def format_history(history):
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
if not history:
|
| 343 |
|
|
@@ -346,7 +375,8 @@ def format_history(history):
|
|
| 346 |
|
| 347 |
history_text = []
|
| 348 |
|
| 349 |
-
|
|
|
|
| 350 |
recent_messages = history[-4:]
|
| 351 |
|
| 352 |
|
|
@@ -369,11 +399,25 @@ def format_history(history):
|
|
| 369 |
)
|
| 370 |
|
| 371 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
return "\n".join(history_text)
|
| 373 |
|
| 374 |
|
| 375 |
# ================================================================
|
| 376 |
-
# 12. GENERATE
|
| 377 |
# ================================================================
|
| 378 |
|
| 379 |
def generate_answer(
|
|
@@ -388,38 +432,42 @@ You are an academic research assistant for the capstone project:
|
|
| 388 |
"Machine Learning-Based Prediction of Wine Quality Using
|
| 389 |
Physicochemical Properties."
|
| 390 |
|
| 391 |
-
The
|
| 392 |
-
|
| 393 |
-
Your task is to answer the user's question using the DOCUMENT
|
| 394 |
-
CONTEXT below.
|
| 395 |
|
| 396 |
IMPORTANT INSTRUCTIONS:
|
| 397 |
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
| 408 |
|
| 409 |
-
|
| 410 |
|
| 411 |
DOCUMENT CONTEXT:
|
| 412 |
|
| 413 |
{context}
|
| 414 |
|
|
|
|
| 415 |
RECENT CONVERSATION:
|
| 416 |
|
| 417 |
{conversation_history}
|
| 418 |
|
|
|
|
| 419 |
USER QUESTION:
|
| 420 |
|
| 421 |
{question}
|
| 422 |
|
|
|
|
| 423 |
ANSWER:
|
| 424 |
"""
|
| 425 |
|
|
@@ -442,7 +490,7 @@ ANSWER:
|
|
| 442 |
|
| 443 |
outputs = llm_model.generate(
|
| 444 |
**inputs,
|
| 445 |
-
max_new_tokens=
|
| 446 |
do_sample=False,
|
| 447 |
num_beams=2,
|
| 448 |
repetition_penalty=1.15,
|
|
@@ -465,6 +513,9 @@ ANSWER:
|
|
| 465 |
# ================================================================
|
| 466 |
|
| 467 |
def chat(message, history):
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
if not message or not message.strip():
|
| 470 |
|
|
@@ -476,11 +527,11 @@ def chat(message, history):
|
|
| 476 |
|
| 477 |
print("\n" + "=" * 70)
|
| 478 |
|
| 479 |
-
print(f"
|
| 480 |
|
| 481 |
|
| 482 |
# ------------------------------------------------------------
|
| 483 |
-
# Retrieve relevant PDF
|
| 484 |
# ------------------------------------------------------------
|
| 485 |
|
| 486 |
retrieved_chunks = retrieve_context(
|
|
@@ -500,7 +551,7 @@ def chat(message, history):
|
|
| 500 |
|
| 501 |
|
| 502 |
# ------------------------------------------------------------
|
| 503 |
-
#
|
| 504 |
# ------------------------------------------------------------
|
| 505 |
|
| 506 |
context = build_context(
|
|
@@ -514,7 +565,7 @@ def chat(message, history):
|
|
| 514 |
|
| 515 |
|
| 516 |
# ------------------------------------------------------------
|
| 517 |
-
# Generate
|
| 518 |
# ------------------------------------------------------------
|
| 519 |
|
| 520 |
answer = generate_answer(
|
|
@@ -525,7 +576,7 @@ def chat(message, history):
|
|
| 525 |
|
| 526 |
|
| 527 |
# ------------------------------------------------------------
|
| 528 |
-
#
|
| 529 |
# ------------------------------------------------------------
|
| 530 |
|
| 531 |
source_pages = sorted(
|
|
@@ -542,6 +593,10 @@ def chat(message, history):
|
|
| 542 |
)
|
| 543 |
|
| 544 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
return f"""
|
| 546 |
{answer}
|
| 547 |
|
|
@@ -554,7 +609,7 @@ def chat(message, history):
|
|
| 554 |
|
| 555 |
|
| 556 |
# ================================================================
|
| 557 |
-
# 14.
|
| 558 |
# ================================================================
|
| 559 |
|
| 560 |
css = """
|
|
@@ -567,12 +622,19 @@ css = """
|
|
| 567 |
"""
|
| 568 |
|
| 569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
with gr.Blocks(
|
| 571 |
-
title=APP_TITLE
|
| 572 |
-
css=css
|
| 573 |
) as demo:
|
| 574 |
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
gr.Markdown(
|
| 577 |
"""
|
| 578 |
# π· Wine Quality Research Assistant
|
|
@@ -581,34 +643,37 @@ with gr.Blocks(
|
|
| 581 |
|
| 582 |
Welcome to the **QM640 Data Analytics Capstone Research Assistant**.
|
| 583 |
|
| 584 |
-
This chatbot searches the uploaded capstone synopsis and
|
| 585 |
-
the most relevant sections
|
| 586 |
-
questions.
|
| 587 |
|
| 588 |
You can ask questions about:
|
| 589 |
|
| 590 |
-
|
|
|
|
| 591 |
- Problem Statement
|
| 592 |
- Purpose of the Study
|
| 593 |
- Research Objectives
|
| 594 |
- Research Questions
|
| 595 |
- Research Hypotheses
|
| 596 |
|
| 597 |
-
|
|
|
|
| 598 |
- Wine Quality Dataset
|
| 599 |
- Physicochemical Variables
|
| 600 |
- Target Variable
|
| 601 |
- Sample Size
|
| 602 |
-
-
|
|
|
|
|
|
|
| 603 |
|
| 604 |
-
**π€ Machine Learning**
|
| 605 |
- Linear Regression
|
| 606 |
-
- Decision Tree
|
| 607 |
-
- Random Forest
|
| 608 |
-
- Gradient Boosting
|
| 609 |
-
- XGBoost
|
|
|
|
|
|
|
| 610 |
|
| 611 |
-
**π Model Evaluation**
|
| 612 |
- MAE
|
| 613 |
- RMSE
|
| 614 |
- RΒ²
|
|
@@ -616,12 +681,14 @@ You can ask questions about:
|
|
| 616 |
- 5-Fold Cross Validation
|
| 617 |
- Hyperparameter Optimisation
|
| 618 |
|
| 619 |
-
|
|
|
|
| 620 |
- Feature Importance
|
| 621 |
- SHAP
|
| 622 |
- LIME
|
| 623 |
|
| 624 |
-
|
|
|
|
| 625 |
- Business Impact
|
| 626 |
- Quality Assessment
|
| 627 |
- Production Optimisation
|
|
@@ -633,12 +700,12 @@ You can ask questions about:
|
|
| 633 |
|
| 634 |
|
| 635 |
# ------------------------------------------------------------
|
| 636 |
-
#
|
| 637 |
# ------------------------------------------------------------
|
| 638 |
|
| 639 |
gr.Markdown(
|
| 640 |
f"""
|
| 641 |
-
### β
Knowledge Base
|
| 642 |
|
| 643 |
π **PDF:** `{PDF_FILE}`
|
| 644 |
|
|
@@ -648,7 +715,7 @@ You can ask questions about:
|
|
| 648 |
|
| 649 |
π **Embedding model:** `{EMBEDDING_MODEL_ID}`
|
| 650 |
|
| 651 |
-
π€ **
|
| 652 |
|
| 653 |
π» **Device:** `{DEVICE}`
|
| 654 |
"""
|
|
@@ -656,16 +723,19 @@ You can ask questions about:
|
|
| 656 |
|
| 657 |
|
| 658 |
# ------------------------------------------------------------
|
| 659 |
-
#
|
| 660 |
# ------------------------------------------------------------
|
| 661 |
|
| 662 |
chatbot_component = gr.Chatbot(
|
| 663 |
-
type="messages",
|
| 664 |
height=500,
|
| 665 |
show_label=False
|
| 666 |
)
|
| 667 |
|
| 668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
chat_interface = gr.ChatInterface(
|
| 670 |
|
| 671 |
fn=chat,
|
|
@@ -676,9 +746,9 @@ You can ask questions about:
|
|
| 676 |
|
| 677 |
"What is the main objective of this study?",
|
| 678 |
|
| 679 |
-
"What problem is
|
| 680 |
|
| 681 |
-
"What dataset is
|
| 682 |
|
| 683 |
"How many observations are in the dataset?",
|
| 684 |
|
|
@@ -688,7 +758,7 @@ You can ask questions about:
|
|
| 688 |
|
| 689 |
"Explain the four research questions.",
|
| 690 |
|
| 691 |
-
"Which machine learning models
|
| 692 |
|
| 693 |
"How will the models be validated?",
|
| 694 |
|
|
@@ -704,7 +774,7 @@ You can ask questions about:
|
|
| 704 |
|
| 705 |
"What is LIME?",
|
| 706 |
|
| 707 |
-
"What is the business value of
|
| 708 |
|
| 709 |
"What are the limitations of the study?"
|
| 710 |
|
|
@@ -713,31 +783,35 @@ You can ask questions about:
|
|
| 713 |
)
|
| 714 |
|
| 715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
gr.Markdown(
|
| 717 |
"""
|
| 718 |
---
|
| 719 |
|
| 720 |
-
##
|
| 721 |
|
| 722 |
**Your Question**
|
| 723 |
|
| 724 |
-
β
|
| 725 |
|
| 726 |
**Semantic Search**
|
| 727 |
|
| 728 |
-
β
|
| 729 |
|
| 730 |
-
**
|
| 731 |
|
| 732 |
-
β
|
| 733 |
|
| 734 |
-
**
|
| 735 |
|
| 736 |
-
β
|
| 737 |
|
| 738 |
-
**
|
| 739 |
|
| 740 |
-
This is a
|
| 741 |
implementation.
|
| 742 |
|
| 743 |
*QM640 Data Analytics Capstone*
|
|
@@ -746,7 +820,7 @@ implementation.
|
|
| 746 |
|
| 747 |
|
| 748 |
# ================================================================
|
| 749 |
-
#
|
| 750 |
# ================================================================
|
| 751 |
|
| 752 |
print("\n" + "=" * 70)
|
|
@@ -760,4 +834,6 @@ if __name__ == "__main__":
|
|
| 760 |
|
| 761 |
demo.queue()
|
| 762 |
|
| 763 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
# Wine Quality Capstone - Interactive RAG Chatbot
|
| 3 |
+
# Hugging Face Spaces + Gradio 6.x
|
|
|
|
| 4 |
# ================================================================
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 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
|
|
|
|
| 37 |
print("=" * 70)
|
| 38 |
|
| 39 |
print(f"Device: {DEVICE}")
|
| 40 |
+
print(f"Gradio version: {gr.__version__}")
|
| 41 |
|
| 42 |
|
| 43 |
# ================================================================
|
|
|
|
| 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(".")
|
|
|
|
| 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:")
|
|
|
|
| 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()
|
|
|
|
| 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 |
|
|
|
|
| 152 |
if not pages:
|
| 153 |
|
| 154 |
raise ValueError(
|
| 155 |
+
"The PDF was found, but no readable text "
|
| 156 |
"could be extracted."
|
| 157 |
)
|
| 158 |
|
|
|
|
| 167 |
# ================================================================
|
| 168 |
|
| 169 |
def create_chunks(pages):
|
| 170 |
+
"""
|
| 171 |
+
Break extracted PDF text into overlapping chunks.
|
| 172 |
+
"""
|
| 173 |
|
| 174 |
chunks = []
|
| 175 |
|
|
|
|
| 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 |
)
|
|
|
|
| 222 |
|
| 223 |
print("\nπ Loading embedding model...")
|
| 224 |
|
| 225 |
+
|
| 226 |
embedding_model = SentenceTransformer(
|
| 227 |
EMBEDDING_MODEL_ID,
|
| 228 |
device=DEVICE
|
| 229 |
)
|
| 230 |
|
| 231 |
+
|
| 232 |
print("β
Embedding model loaded.")
|
| 233 |
|
| 234 |
|
| 235 |
# ================================================================
|
| 236 |
+
# 7. CREATE DOCUMENT EMBEDDINGS
|
| 237 |
# ================================================================
|
| 238 |
|
| 239 |
print("\nπ§ Creating document embeddings...")
|
|
|
|
| 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],
|
|
|
|
| 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(
|
|
|
|
| 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 |
|
|
|
|
| 375 |
|
| 376 |
history_text = []
|
| 377 |
|
| 378 |
+
|
| 379 |
+
# Keep only recent messages to prevent prompt growth
|
| 380 |
recent_messages = history[-4:]
|
| 381 |
|
| 382 |
|
|
|
|
| 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(
|
|
|
|
| 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 |
|
|
|
|
| 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,
|
|
|
|
| 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 |
|
|
|
|
| 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(
|
|
|
|
| 551 |
|
| 552 |
|
| 553 |
# ------------------------------------------------------------
|
| 554 |
+
# Build context
|
| 555 |
# ------------------------------------------------------------
|
| 556 |
|
| 557 |
context = build_context(
|
|
|
|
| 565 |
|
| 566 |
|
| 567 |
# ------------------------------------------------------------
|
| 568 |
+
# Generate response
|
| 569 |
# ------------------------------------------------------------
|
| 570 |
|
| 571 |
answer = generate_answer(
|
|
|
|
| 576 |
|
| 577 |
|
| 578 |
# ------------------------------------------------------------
|
| 579 |
+
# Get unique page numbers
|
| 580 |
# ------------------------------------------------------------
|
| 581 |
|
| 582 |
source_pages = sorted(
|
|
|
|
| 593 |
)
|
| 594 |
|
| 595 |
|
| 596 |
+
# ------------------------------------------------------------
|
| 597 |
+
# Return answer + source pages
|
| 598 |
+
# ------------------------------------------------------------
|
| 599 |
+
|
| 600 |
return f"""
|
| 601 |
{answer}
|
| 602 |
|
|
|
|
| 609 |
|
| 610 |
|
| 611 |
# ================================================================
|
| 612 |
+
# 14. CUSTOM CSS
|
| 613 |
# ================================================================
|
| 614 |
|
| 615 |
css = """
|
|
|
|
| 622 |
"""
|
| 623 |
|
| 624 |
|
| 625 |
+
# ================================================================
|
| 626 |
+
# 15. GRADIO INTERFACE
|
| 627 |
+
# ================================================================
|
| 628 |
+
|
| 629 |
with gr.Blocks(
|
| 630 |
+
title=APP_TITLE
|
|
|
|
| 631 |
) as demo:
|
| 632 |
|
| 633 |
|
| 634 |
+
# ------------------------------------------------------------
|
| 635 |
+
# Header
|
| 636 |
+
# ------------------------------------------------------------
|
| 637 |
+
|
| 638 |
gr.Markdown(
|
| 639 |
"""
|
| 640 |
# π· Wine Quality Research Assistant
|
|
|
|
| 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Β²
|
|
|
|
| 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
|
|
|
|
| 700 |
|
| 701 |
|
| 702 |
# ------------------------------------------------------------
|
| 703 |
+
# Knowledge Base Status
|
| 704 |
# ------------------------------------------------------------
|
| 705 |
|
| 706 |
gr.Markdown(
|
| 707 |
f"""
|
| 708 |
+
### β
Knowledge Base Loaded
|
| 709 |
|
| 710 |
π **PDF:** `{PDF_FILE}`
|
| 711 |
|
|
|
|
| 715 |
|
| 716 |
π **Embedding model:** `{EMBEDDING_MODEL_ID}`
|
| 717 |
|
| 718 |
+
π€ **Language model:** `{LLM_MODEL_ID}`
|
| 719 |
|
| 720 |
π» **Device:** `{DEVICE}`
|
| 721 |
"""
|
|
|
|
| 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(
|
| 740 |
|
| 741 |
fn=chat,
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 783 |
)
|
| 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*
|
|
|
|
| 820 |
|
| 821 |
|
| 822 |
# ================================================================
|
| 823 |
+
# 16. START APPLICATION
|
| 824 |
# ================================================================
|
| 825 |
|
| 826 |
print("\n" + "=" * 70)
|
|
|
|
| 834 |
|
| 835 |
demo.queue()
|
| 836 |
|
| 837 |
+
demo.launch(
|
| 838 |
+
css=css
|
| 839 |
+
)
|