Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# ================================================================
|
| 2 |
# Wine Quality Capstone - Interactive RAG Chatbot
|
| 3 |
-
# Hugging Face Spaces
|
|
|
|
| 4 |
# ================================================================
|
| 5 |
|
| 6 |
import os
|
|
@@ -20,53 +21,71 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
| 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 |
-
#
|
| 30 |
LLM_MODEL_ID = "google/flan-t5-small"
|
| 31 |
|
| 32 |
-
TOP_K =
|
| 33 |
-
CHUNK_SIZE =
|
| 34 |
-
CHUNK_OVERLAP =
|
| 35 |
|
| 36 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# ================================================================
|
| 40 |
-
# 2.
|
| 41 |
# ================================================================
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
print(f"Looking for PDF: {PDF_FILE}")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
if not
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
# ================================================================
|
| 69 |
-
# 3.
|
| 70 |
# ================================================================
|
| 71 |
|
| 72 |
def clean_text(text):
|
|
@@ -76,15 +95,19 @@ def clean_text(text):
|
|
| 76 |
|
| 77 |
text = text.replace("\x00", " ")
|
| 78 |
|
| 79 |
-
#
|
| 80 |
text = re.sub(r"\s+", " ", text)
|
| 81 |
|
| 82 |
return text.strip()
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def read_pdf(pdf_path):
|
| 86 |
|
| 87 |
-
print("\
|
| 88 |
|
| 89 |
reader = PdfReader(pdf_path)
|
| 90 |
|
|
@@ -92,17 +115,26 @@ def read_pdf(pdf_path):
|
|
| 92 |
|
| 93 |
for page_number, page in enumerate(reader.pages, start=1):
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
return pages
|
|
@@ -110,18 +142,22 @@ def read_pdf(pdf_path):
|
|
| 110 |
|
| 111 |
pages = read_pdf(PDF_FILE)
|
| 112 |
|
| 113 |
-
print(f"✅ Successfully extracted text from {len(pages)} pages.")
|
| 114 |
-
|
| 115 |
|
| 116 |
-
if
|
| 117 |
|
| 118 |
raise ValueError(
|
| 119 |
-
"
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
# ================================================================
|
| 124 |
-
#
|
| 125 |
# ================================================================
|
| 126 |
|
| 127 |
def create_chunks(pages):
|
|
@@ -130,9 +166,10 @@ def create_chunks(pages):
|
|
| 130 |
|
| 131 |
for page_data in pages:
|
| 132 |
|
| 133 |
-
page_number = page_data["page"]
|
| 134 |
text = page_data["text"]
|
| 135 |
|
|
|
|
|
|
|
| 136 |
start = 0
|
| 137 |
|
| 138 |
while start < len(text):
|
|
@@ -157,14 +194,17 @@ def create_chunks(pages):
|
|
| 157 |
|
| 158 |
chunks = create_chunks(pages)
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
|
| 163 |
# ================================================================
|
| 164 |
-
#
|
| 165 |
# ================================================================
|
| 166 |
|
| 167 |
-
print("\
|
| 168 |
|
| 169 |
embedding_model = SentenceTransformer(
|
| 170 |
EMBEDDING_MODEL_ID,
|
|
@@ -175,45 +215,40 @@ print("✅ Embedding model loaded.")
|
|
| 175 |
|
| 176 |
|
| 177 |
# ================================================================
|
| 178 |
-
#
|
| 179 |
# ================================================================
|
| 180 |
|
| 181 |
-
print("\
|
| 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"✅
|
| 208 |
-
f"{len(document_embeddings)}
|
| 209 |
)
|
| 210 |
|
| 211 |
|
| 212 |
# ================================================================
|
| 213 |
-
#
|
| 214 |
# ================================================================
|
| 215 |
|
| 216 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
@@ -231,32 +266,25 @@ llm_model.to(DEVICE)
|
|
| 231 |
llm_model.eval()
|
| 232 |
|
| 233 |
|
| 234 |
-
print("✅ Language model loaded.")
|
| 235 |
|
| 236 |
|
| 237 |
# ================================================================
|
| 238 |
-
#
|
| 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 |
|
|
@@ -273,9 +301,7 @@ def retrieve_context(question, top_k=TOP_K):
|
|
| 273 |
results.append(
|
| 274 |
{
|
| 275 |
"page": chunks[index]["page"],
|
| 276 |
-
|
| 277 |
"text": chunks[index]["text"],
|
| 278 |
-
|
| 279 |
"score": float(
|
| 280 |
similarity_scores[index]
|
| 281 |
)
|
|
@@ -287,31 +313,28 @@ def retrieve_context(question, top_k=TOP_K):
|
|
| 287 |
|
| 288 |
|
| 289 |
# ================================================================
|
| 290 |
-
#
|
| 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 |
-
#
|
| 315 |
# ================================================================
|
| 316 |
|
| 317 |
def format_history(history):
|
|
@@ -321,126 +344,116 @@ def format_history(history):
|
|
| 321 |
return "No previous conversation."
|
| 322 |
|
| 323 |
|
| 324 |
-
|
| 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(
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
content = item.get(
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
-
|
| 340 |
f"{role}: {content}"
|
| 341 |
)
|
| 342 |
|
| 343 |
|
| 344 |
-
return "\n".join(
|
| 345 |
|
| 346 |
|
| 347 |
# ================================================================
|
| 348 |
-
#
|
| 349 |
# ================================================================
|
| 350 |
|
| 351 |
-
def generate_answer(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
prompt = f"""
|
| 354 |
-
You are an
|
| 355 |
-
Data Analytics Capstone project:
|
| 356 |
|
| 357 |
-
Machine Learning-Based Prediction of Wine Quality Using
|
| 358 |
-
Physicochemical Properties.
|
| 359 |
|
| 360 |
-
The
|
| 361 |
|
| 362 |
-
Your
|
| 363 |
-
|
| 364 |
|
| 365 |
-
IMPORTANT
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 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 |
-
{
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -448,41 +461,46 @@ ANSWER:
|
|
| 448 |
|
| 449 |
|
| 450 |
# ================================================================
|
| 451 |
-
#
|
| 452 |
# ================================================================
|
| 453 |
|
| 454 |
-
def
|
| 455 |
|
| 456 |
if not message or not message.strip():
|
| 457 |
|
| 458 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
|
|
|
| 460 |
|
| 461 |
-
|
| 462 |
-
# Retrieve relevant document content
|
| 463 |
-
# ------------------------------------------------------------
|
| 464 |
|
| 465 |
-
retrieved_chunks = retrieve_context(message)
|
| 466 |
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
-
|
| 469 |
-
|
|
|
|
| 470 |
|
| 471 |
-
print(f"QUESTION: {message}")
|
| 472 |
|
| 473 |
-
print("
|
| 474 |
|
| 475 |
|
| 476 |
for item in retrieved_chunks:
|
| 477 |
|
| 478 |
print(
|
| 479 |
-
f"Page {item['page']} "
|
| 480 |
-
f"
|
| 481 |
)
|
| 482 |
|
| 483 |
|
| 484 |
# ------------------------------------------------------------
|
| 485 |
-
#
|
| 486 |
# ------------------------------------------------------------
|
| 487 |
|
| 488 |
context = build_context(
|
|
@@ -490,7 +508,7 @@ def chatbot_response(message, history):
|
|
| 490 |
)
|
| 491 |
|
| 492 |
|
| 493 |
-
|
| 494 |
history
|
| 495 |
)
|
| 496 |
|
|
@@ -500,138 +518,114 @@ def chatbot_response(message, history):
|
|
| 500 |
# ------------------------------------------------------------
|
| 501 |
|
| 502 |
answer = generate_answer(
|
| 503 |
-
|
| 504 |
message,
|
| 505 |
-
|
| 506 |
context,
|
| 507 |
-
|
| 508 |
-
history_text
|
| 509 |
-
|
| 510 |
)
|
| 511 |
|
| 512 |
|
| 513 |
# ------------------------------------------------------------
|
| 514 |
-
#
|
| 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 |
-
|
| 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 |
-
📚 **
|
| 549 |
|
| 550 |
-
📄 *
|
| 551 |
"""
|
| 552 |
|
| 553 |
|
| 554 |
-
return final_answer
|
| 555 |
-
|
| 556 |
-
|
| 557 |
# ================================================================
|
| 558 |
-
#
|
| 559 |
# ================================================================
|
| 560 |
|
| 561 |
-
|
| 562 |
|
| 563 |
.gradio-container {
|
| 564 |
max-width: 1100px !important;
|
| 565 |
margin: auto !important;
|
| 566 |
}
|
| 567 |
|
| 568 |
-
footer {
|
| 569 |
-
visibility: hidden;
|
| 570 |
-
}
|
| 571 |
-
|
| 572 |
"""
|
| 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 |
-
|
| 595 |
-
|
| 596 |
-
This
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
**Dataset**
|
| 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 |
"""
|
|
@@ -639,104 +633,52 @@ generating an answer.
|
|
| 639 |
|
| 640 |
|
| 641 |
# ------------------------------------------------------------
|
| 642 |
-
#
|
| 643 |
# ------------------------------------------------------------
|
| 644 |
|
| 645 |
gr.Markdown(
|
| 646 |
f"""
|
| 647 |
-
### ✅ Knowledge Base
|
| 648 |
-
|
| 649 |
-
📄 **Document:** `{PDF_FILE}`
|
| 650 |
|
| 651 |
-
|
| 652 |
|
| 653 |
-
|
| 654 |
|
| 655 |
-
|
| 656 |
|
| 657 |
🔎 **Embedding model:** `{EMBEDDING_MODEL_ID}`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 658 |
"""
|
| 659 |
)
|
| 660 |
|
| 661 |
|
| 662 |
# ------------------------------------------------------------
|
| 663 |
-
# Chat
|
| 664 |
# ------------------------------------------------------------
|
| 665 |
|
| 666 |
-
|
| 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 |
-
|
| 732 |
|
| 733 |
examples=[
|
| 734 |
|
| 735 |
-
"What is the main objective of this
|
| 736 |
|
| 737 |
-
"What problem is
|
| 738 |
|
| 739 |
-
"What dataset is
|
| 740 |
|
| 741 |
"How many observations are in the dataset?",
|
| 742 |
|
|
@@ -746,139 +688,72 @@ Try asking:
|
|
| 746 |
|
| 747 |
"Explain the four research questions.",
|
| 748 |
|
| 749 |
-
"Which machine learning models
|
| 750 |
|
| 751 |
"How will the models be validated?",
|
| 752 |
|
| 753 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
|
| 755 |
-
"What
|
| 756 |
|
| 757 |
"How will SHAP be used?",
|
| 758 |
|
| 759 |
-
"What is
|
| 760 |
|
| 761 |
-
"What is the business value of
|
| 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
|
| 781 |
-
|
| 782 |
-
**Question → Semantic Search → Relevant PDF Content → AI → Answer**
|
| 783 |
|
| 784 |
-
|
| 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 |
-
|
| 797 |
|
| 798 |
-
|
| 799 |
|
|
|
|
| 800 |
|
| 801 |
-
|
| 802 |
|
| 803 |
-
|
| 804 |
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
)
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
chat_history.append(
|
| 811 |
-
{
|
| 812 |
-
"role": "user",
|
| 813 |
-
"content": message
|
| 814 |
-
}
|
| 815 |
-
)
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
chat_history.append(
|
| 819 |
-
{
|
| 820 |
-
"role": "assistant",
|
| 821 |
-
"content": response
|
| 822 |
-
}
|
| 823 |
-
)
|
| 824 |
|
|
|
|
|
|
|
| 825 |
|
| 826 |
-
|
| 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.
|
| 877 |
# ================================================================
|
| 878 |
|
| 879 |
-
print("\n" + "=" *
|
| 880 |
-
|
| 881 |
-
print("
|
|
|
|
|
|
|
| 882 |
|
| 883 |
|
| 884 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
# Wine Quality Capstone - Interactive RAG Chatbot
|
| 3 |
+
# Hugging Face Spaces
|
| 4 |
+
# Compatible with Gradio 6.x
|
| 5 |
# ================================================================
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 21 |
|
| 22 |
APP_TITLE = "🍷 Wine Quality Research Assistant"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
|
| 25 |
|
| 26 |
+
# Small model chosen for Hugging Face CPU Spaces
|
| 27 |
LLM_MODEL_ID = "google/flan-t5-small"
|
| 28 |
|
| 29 |
+
TOP_K = 3
|
| 30 |
+
CHUNK_SIZE = 700
|
| 31 |
+
CHUNK_OVERLAP = 100
|
| 32 |
|
| 33 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
|
| 35 |
|
| 36 |
+
print("=" * 70)
|
| 37 |
+
print("🍷 Starting Wine Quality Research Assistant")
|
| 38 |
+
print("=" * 70)
|
| 39 |
+
|
| 40 |
+
print(f"Device: {DEVICE}")
|
| 41 |
+
print(f"Gradio Version: {gr.__version__}")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
# ================================================================
|
| 45 |
+
# 2. FIND PDF AUTOMATICALLY
|
| 46 |
# ================================================================
|
| 47 |
|
| 48 |
+
def find_pdf():
|
| 49 |
+
"""
|
| 50 |
+
Automatically locate a PDF in the 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 found in repository:")
|
| 64 |
|
| 65 |
+
for file in files:
|
| 66 |
+
print(" -", file)
|
| 67 |
|
| 68 |
+
raise FileNotFoundError(
|
| 69 |
+
"\nNo PDF file found.\n"
|
| 70 |
+
"Please upload your capstone PDF into the root folder "
|
| 71 |
+
"of the Hugging Face Space."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
print("\nPDF files detected:")
|
| 75 |
|
| 76 |
+
for pdf in pdf_files:
|
| 77 |
+
print(" -", pdf)
|
| 78 |
|
| 79 |
+
return pdf_files[0]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
PDF_FILE = find_pdf()
|
| 83 |
+
|
| 84 |
+
print(f"\n✅ Using PDF: {PDF_FILE}")
|
| 85 |
|
| 86 |
|
| 87 |
# ================================================================
|
| 88 |
+
# 3. CLEAN TEXT
|
| 89 |
# ================================================================
|
| 90 |
|
| 91 |
def clean_text(text):
|
|
|
|
| 95 |
|
| 96 |
text = text.replace("\x00", " ")
|
| 97 |
|
| 98 |
+
# Fix excessive whitespace
|
| 99 |
text = re.sub(r"\s+", " ", text)
|
| 100 |
|
| 101 |
return text.strip()
|
| 102 |
|
| 103 |
|
| 104 |
+
# ================================================================
|
| 105 |
+
# 4. READ PDF
|
| 106 |
+
# ================================================================
|
| 107 |
+
|
| 108 |
def read_pdf(pdf_path):
|
| 109 |
|
| 110 |
+
print("\n📖 Reading PDF...")
|
| 111 |
|
| 112 |
reader = PdfReader(pdf_path)
|
| 113 |
|
|
|
|
| 115 |
|
| 116 |
for page_number, page in enumerate(reader.pages, start=1):
|
| 117 |
|
| 118 |
+
try:
|
| 119 |
|
| 120 |
+
text = page.extract_text()
|
| 121 |
|
| 122 |
+
text = clean_text(text)
|
| 123 |
|
| 124 |
+
if text:
|
| 125 |
+
|
| 126 |
+
pages.append(
|
| 127 |
+
{
|
| 128 |
+
"page": page_number,
|
| 129 |
+
"text": text
|
| 130 |
+
}
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
except Exception as error:
|
| 134 |
+
|
| 135 |
+
print(
|
| 136 |
+
f"Warning: Could not read page "
|
| 137 |
+
f"{page_number}: {error}"
|
| 138 |
)
|
| 139 |
|
| 140 |
return pages
|
|
|
|
| 142 |
|
| 143 |
pages = read_pdf(PDF_FILE)
|
| 144 |
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
if not pages:
|
| 147 |
|
| 148 |
raise ValueError(
|
| 149 |
+
"The PDF was found but no readable text "
|
| 150 |
+
"could be extracted."
|
| 151 |
)
|
| 152 |
|
| 153 |
|
| 154 |
+
print(
|
| 155 |
+
f"✅ Text extracted from {len(pages)} pages."
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
# ================================================================
|
| 160 |
+
# 5. CREATE KNOWLEDGE CHUNKS
|
| 161 |
# ================================================================
|
| 162 |
|
| 163 |
def create_chunks(pages):
|
|
|
|
| 166 |
|
| 167 |
for page_data in pages:
|
| 168 |
|
|
|
|
| 169 |
text = page_data["text"]
|
| 170 |
|
| 171 |
+
page_number = page_data["page"]
|
| 172 |
+
|
| 173 |
start = 0
|
| 174 |
|
| 175 |
while start < len(text):
|
|
|
|
| 194 |
|
| 195 |
chunks = create_chunks(pages)
|
| 196 |
|
| 197 |
+
|
| 198 |
+
print(
|
| 199 |
+
f"✅ Created {len(chunks)} searchable knowledge chunks."
|
| 200 |
+
)
|
| 201 |
|
| 202 |
|
| 203 |
# ================================================================
|
| 204 |
+
# 6. LOAD EMBEDDING MODEL
|
| 205 |
# ================================================================
|
| 206 |
|
| 207 |
+
print("\n🔎 Loading embedding model...")
|
| 208 |
|
| 209 |
embedding_model = SentenceTransformer(
|
| 210 |
EMBEDDING_MODEL_ID,
|
|
|
|
| 215 |
|
| 216 |
|
| 217 |
# ================================================================
|
| 218 |
+
# 7. CREATE EMBEDDINGS
|
| 219 |
# ================================================================
|
| 220 |
|
| 221 |
+
print("\n🧠 Creating document embeddings...")
|
| 222 |
|
| 223 |
|
| 224 |
document_texts = [
|
|
|
|
| 225 |
chunk["text"]
|
|
|
|
| 226 |
for chunk in chunks
|
|
|
|
| 227 |
]
|
| 228 |
|
| 229 |
|
| 230 |
document_embeddings = embedding_model.encode(
|
|
|
|
| 231 |
document_texts,
|
|
|
|
| 232 |
convert_to_numpy=True,
|
|
|
|
| 233 |
normalize_embeddings=True,
|
|
|
|
| 234 |
show_progress_bar=False
|
|
|
|
| 235 |
)
|
| 236 |
|
| 237 |
|
| 238 |
print(
|
| 239 |
+
f"✅ Embeddings created for "
|
| 240 |
+
f"{len(document_embeddings)} chunks."
|
| 241 |
)
|
| 242 |
|
| 243 |
|
| 244 |
# ================================================================
|
| 245 |
+
# 8. LOAD LANGUAGE MODEL
|
| 246 |
# ================================================================
|
| 247 |
|
| 248 |
+
print(
|
| 249 |
+
f"\n🤖 Loading language model: "
|
| 250 |
+
f"{LLM_MODEL_ID}"
|
| 251 |
+
)
|
| 252 |
|
| 253 |
|
| 254 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
| 266 |
llm_model.eval()
|
| 267 |
|
| 268 |
|
| 269 |
+
print("✅ Language model loaded successfully.")
|
| 270 |
|
| 271 |
|
| 272 |
# ================================================================
|
| 273 |
+
# 9. SEMANTIC SEARCH
|
| 274 |
# ================================================================
|
| 275 |
|
| 276 |
def retrieve_context(question, top_k=TOP_K):
|
| 277 |
|
| 278 |
question_embedding = embedding_model.encode(
|
|
|
|
| 279 |
[question],
|
|
|
|
| 280 |
convert_to_numpy=True,
|
|
|
|
| 281 |
normalize_embeddings=True
|
|
|
|
| 282 |
)[0]
|
| 283 |
|
| 284 |
|
| 285 |
similarity_scores = np.dot(
|
|
|
|
| 286 |
document_embeddings,
|
|
|
|
| 287 |
question_embedding
|
|
|
|
| 288 |
)
|
| 289 |
|
| 290 |
|
|
|
|
| 301 |
results.append(
|
| 302 |
{
|
| 303 |
"page": chunks[index]["page"],
|
|
|
|
| 304 |
"text": chunks[index]["text"],
|
|
|
|
| 305 |
"score": float(
|
| 306 |
similarity_scores[index]
|
| 307 |
)
|
|
|
|
| 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(
|
|
|
|
| 326 |
f"""
|
| 327 |
+
SOURCE PAGE {item['page']}:
|
| 328 |
|
| 329 |
{item['text']}
|
| 330 |
"""
|
| 331 |
)
|
| 332 |
|
| 333 |
+
return "\n".join(context_parts)
|
|
|
|
| 334 |
|
| 335 |
|
| 336 |
# ================================================================
|
| 337 |
+
# 11. PREPARE CONVERSATION HISTORY
|
| 338 |
# ================================================================
|
| 339 |
|
| 340 |
def format_history(history):
|
|
|
|
| 344 |
return "No previous conversation."
|
| 345 |
|
| 346 |
|
| 347 |
+
history_text = []
|
| 348 |
|
| 349 |
+
# Keep only recent messages
|
| 350 |
+
recent_messages = history[-4:]
|
| 351 |
|
|
|
|
|
|
|
| 352 |
|
| 353 |
+
for item in recent_messages:
|
|
|
|
| 354 |
|
| 355 |
if isinstance(item, dict):
|
| 356 |
|
| 357 |
+
role = item.get(
|
| 358 |
+
"role",
|
| 359 |
+
"user"
|
| 360 |
+
)
|
| 361 |
|
| 362 |
+
content = item.get(
|
| 363 |
+
"content",
|
| 364 |
+
""
|
| 365 |
+
)
|
| 366 |
|
| 367 |
+
history_text.append(
|
| 368 |
f"{role}: {content}"
|
| 369 |
)
|
| 370 |
|
| 371 |
|
| 372 |
+
return "\n".join(history_text)
|
| 373 |
|
| 374 |
|
| 375 |
# ================================================================
|
| 376 |
+
# 12. GENERATE RESPONSE
|
| 377 |
# ================================================================
|
| 378 |
|
| 379 |
+
def generate_answer(
|
| 380 |
+
question,
|
| 381 |
+
context,
|
| 382 |
+
conversation_history
|
| 383 |
+
):
|
| 384 |
|
| 385 |
prompt = f"""
|
| 386 |
+
You are an academic research assistant for the capstone project:
|
|
|
|
| 387 |
|
| 388 |
+
"Machine Learning-Based Prediction of Wine Quality Using
|
| 389 |
+
Physicochemical Properties."
|
| 390 |
|
| 391 |
+
The project is based on the supplied capstone synopsis.
|
| 392 |
|
| 393 |
+
Your task is to answer the user's question using the DOCUMENT
|
| 394 |
+
CONTEXT below.
|
| 395 |
|
| 396 |
+
IMPORTANT INSTRUCTIONS:
|
| 397 |
|
| 398 |
+
- Answer based primarily on the supplied document.
|
| 399 |
+
- Do not invent facts.
|
| 400 |
+
- Do not invent model results.
|
| 401 |
+
- Do not say that a planned analysis has already been completed.
|
| 402 |
+
- Clearly distinguish planned methodology from completed findings.
|
| 403 |
+
- Explain technical concepts in simple language.
|
| 404 |
+
- Keep answers clear and reasonably concise.
|
| 405 |
+
- Use bullet points when useful.
|
| 406 |
+
- If the document does not contain the requested information,
|
| 407 |
+
clearly say:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
|
| 409 |
+
"This information is not specified in the current capstone synopsis."
|
| 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 |
|
| 426 |
|
| 427 |
inputs = tokenizer(
|
|
|
|
| 428 |
prompt,
|
|
|
|
| 429 |
return_tensors="pt",
|
|
|
|
| 430 |
truncation=True,
|
| 431 |
+
max_length=1800
|
| 432 |
+
)
|
| 433 |
|
|
|
|
| 434 |
|
| 435 |
+
inputs = {
|
| 436 |
+
key: value.to(DEVICE)
|
| 437 |
+
for key, value in inputs.items()
|
| 438 |
+
}
|
| 439 |
|
| 440 |
|
| 441 |
with torch.no_grad():
|
| 442 |
|
| 443 |
outputs = llm_model.generate(
|
|
|
|
| 444 |
**inputs,
|
| 445 |
+
max_new_tokens=200,
|
|
|
|
|
|
|
| 446 |
do_sample=False,
|
| 447 |
+
num_beams=2,
|
|
|
|
|
|
|
| 448 |
repetition_penalty=1.15,
|
|
|
|
| 449 |
no_repeat_ngram_size=3,
|
|
|
|
| 450 |
early_stopping=True
|
|
|
|
| 451 |
)
|
| 452 |
|
| 453 |
|
| 454 |
answer = tokenizer.decode(
|
|
|
|
| 455 |
outputs[0],
|
|
|
|
| 456 |
skip_special_tokens=True
|
|
|
|
| 457 |
)
|
| 458 |
|
| 459 |
|
|
|
|
| 461 |
|
| 462 |
|
| 463 |
# ================================================================
|
| 464 |
+
# 13. MAIN CHAT FUNCTION
|
| 465 |
# ================================================================
|
| 466 |
|
| 467 |
+
def chat(message, history):
|
| 468 |
|
| 469 |
if not message or not message.strip():
|
| 470 |
|
| 471 |
+
return (
|
| 472 |
+
"Please enter a question about "
|
| 473 |
+
"the capstone project."
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
|
| 477 |
+
print("\n" + "=" * 70)
|
| 478 |
|
| 479 |
+
print(f"Question: {message}")
|
|
|
|
|
|
|
| 480 |
|
|
|
|
| 481 |
|
| 482 |
+
# ------------------------------------------------------------
|
| 483 |
+
# Retrieve relevant PDF chunks
|
| 484 |
+
# ------------------------------------------------------------
|
| 485 |
|
| 486 |
+
retrieved_chunks = retrieve_context(
|
| 487 |
+
message
|
| 488 |
+
)
|
| 489 |
|
|
|
|
| 490 |
|
| 491 |
+
print("\nRelevant pages retrieved:")
|
| 492 |
|
| 493 |
|
| 494 |
for item in retrieved_chunks:
|
| 495 |
|
| 496 |
print(
|
| 497 |
+
f"Page {item['page']} | "
|
| 498 |
+
f"Similarity: {item['score']:.3f}"
|
| 499 |
)
|
| 500 |
|
| 501 |
|
| 502 |
# ------------------------------------------------------------
|
| 503 |
+
# Create context
|
| 504 |
# ------------------------------------------------------------
|
| 505 |
|
| 506 |
context = build_context(
|
|
|
|
| 508 |
)
|
| 509 |
|
| 510 |
|
| 511 |
+
conversation_history = format_history(
|
| 512 |
history
|
| 513 |
)
|
| 514 |
|
|
|
|
| 518 |
# ------------------------------------------------------------
|
| 519 |
|
| 520 |
answer = generate_answer(
|
|
|
|
| 521 |
message,
|
|
|
|
| 522 |
context,
|
| 523 |
+
conversation_history
|
|
|
|
|
|
|
| 524 |
)
|
| 525 |
|
| 526 |
|
| 527 |
# ------------------------------------------------------------
|
| 528 |
+
# Source pages
|
| 529 |
# ------------------------------------------------------------
|
| 530 |
|
| 531 |
source_pages = sorted(
|
|
|
|
| 532 |
set(
|
|
|
|
| 533 |
item["page"]
|
|
|
|
| 534 |
for item in retrieved_chunks
|
|
|
|
| 535 |
)
|
|
|
|
| 536 |
)
|
| 537 |
|
| 538 |
|
| 539 |
+
pages_string = ", ".join(
|
|
|
|
| 540 |
str(page)
|
|
|
|
| 541 |
for page in source_pages
|
|
|
|
| 542 |
)
|
| 543 |
|
| 544 |
|
| 545 |
+
return f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
{answer}
|
| 547 |
|
| 548 |
---
|
| 549 |
|
| 550 |
+
📚 **Relevant source page(s): {pages_string}**
|
| 551 |
|
| 552 |
+
📄 *Answer generated from the uploaded QM640 Capstone Synopsis.*
|
| 553 |
"""
|
| 554 |
|
| 555 |
|
|
|
|
|
|
|
|
|
|
| 556 |
# ================================================================
|
| 557 |
+
# 14. GRADIO INTERFACE
|
| 558 |
# ================================================================
|
| 559 |
|
| 560 |
+
css = """
|
| 561 |
|
| 562 |
.gradio-container {
|
| 563 |
max-width: 1100px !important;
|
| 564 |
margin: auto !important;
|
| 565 |
}
|
| 566 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 579 |
|
| 580 |
### Machine Learning-Based Prediction of Wine Quality Using Physicochemical Properties
|
| 581 |
|
| 582 |
+
Welcome to the **QM640 Data Analytics Capstone Research Assistant**.
|
| 583 |
+
|
| 584 |
+
This chatbot searches the uploaded capstone synopsis and uses
|
| 585 |
+
the most relevant sections of the document to answer your
|
| 586 |
+
questions.
|
| 587 |
+
|
| 588 |
+
You can ask questions about:
|
| 589 |
+
|
| 590 |
+
**📊 Research**
|
| 591 |
+
- Problem Statement
|
| 592 |
+
- Purpose of the Study
|
| 593 |
+
- Research Objectives
|
| 594 |
+
- Research Questions
|
| 595 |
+
- Research Hypotheses
|
| 596 |
+
|
| 597 |
+
**🍷 Dataset**
|
| 598 |
+
- Wine Quality Dataset
|
| 599 |
+
- Physicochemical Variables
|
| 600 |
+
- Target Variable
|
| 601 |
+
- Sample Size
|
| 602 |
+
- Data Source
|
| 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²
|
| 615 |
+
- 80/20 Train-Test Split
|
| 616 |
+
- 5-Fold Cross Validation
|
| 617 |
+
- Hyperparameter Optimisation
|
| 618 |
+
|
| 619 |
+
**🔍 Explainable AI**
|
| 620 |
+
- Feature Importance
|
| 621 |
+
- SHAP
|
| 622 |
+
- LIME
|
| 623 |
+
|
| 624 |
+
**💼 Business**
|
| 625 |
+
- Business Impact
|
| 626 |
+
- Quality Assessment
|
| 627 |
+
- Production Optimisation
|
| 628 |
+
- Study Limitations
|
| 629 |
|
| 630 |
---
|
| 631 |
"""
|
|
|
|
| 633 |
|
| 634 |
|
| 635 |
# ------------------------------------------------------------
|
| 636 |
+
# Show Knowledge Base Status
|
| 637 |
# ------------------------------------------------------------
|
| 638 |
|
| 639 |
gr.Markdown(
|
| 640 |
f"""
|
| 641 |
+
### ✅ Knowledge Base Status
|
|
|
|
|
|
|
| 642 |
|
| 643 |
+
📄 **PDF:** `{PDF_FILE}`
|
| 644 |
|
| 645 |
+
📑 **Pages loaded:** {len(pages)}
|
| 646 |
|
| 647 |
+
🧩 **Searchable chunks:** {len(chunks)}
|
| 648 |
|
| 649 |
🔎 **Embedding model:** `{EMBEDDING_MODEL_ID}`
|
| 650 |
+
|
| 651 |
+
🤖 **Answer model:** `{LLM_MODEL_ID}`
|
| 652 |
+
|
| 653 |
+
💻 **Device:** `{DEVICE}`
|
| 654 |
"""
|
| 655 |
)
|
| 656 |
|
| 657 |
|
| 658 |
# ------------------------------------------------------------
|
| 659 |
+
# Chat Interface
|
| 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,
|
|
|
|
|
|
|
| 672 |
|
| 673 |
+
chatbot=chatbot_component,
|
| 674 |
|
| 675 |
examples=[
|
| 676 |
|
| 677 |
+
"What is the main objective of this study?",
|
| 678 |
|
| 679 |
+
"What problem is the research trying to solve?",
|
| 680 |
|
| 681 |
+
"What dataset is used in this project?",
|
| 682 |
|
| 683 |
"How many observations are in the dataset?",
|
| 684 |
|
|
|
|
| 688 |
|
| 689 |
"Explain the four research questions.",
|
| 690 |
|
| 691 |
+
"Which machine learning models are being compared?",
|
| 692 |
|
| 693 |
"How will the models be validated?",
|
| 694 |
|
| 695 |
+
"Explain 5-fold cross-validation in simple terms.",
|
| 696 |
+
|
| 697 |
+
"What is MAE?",
|
| 698 |
+
|
| 699 |
+
"What is RMSE?",
|
| 700 |
|
| 701 |
+
"What does R² mean?",
|
| 702 |
|
| 703 |
"How will SHAP be used?",
|
| 704 |
|
| 705 |
+
"What is LIME?",
|
| 706 |
|
| 707 |
+
"What is the business value of the project?",
|
| 708 |
|
| 709 |
"What are the limitations of the study?"
|
| 710 |
|
| 711 |
+
]
|
|
|
|
|
|
|
| 712 |
|
| 713 |
)
|
| 714 |
|
| 715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
gr.Markdown(
|
| 717 |
"""
|
| 718 |
---
|
| 719 |
|
| 720 |
+
### 🔎 How the chatbot works
|
|
|
|
|
|
|
| 721 |
|
| 722 |
+
**Your Question**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
+
↓
|
| 725 |
|
| 726 |
+
**Semantic Search**
|
|
|
|
|
|
|
| 727 |
|
| 728 |
+
↓
|
| 729 |
|
| 730 |
+
**Find Relevant Sections of the PDF**
|
| 731 |
|
| 732 |
+
↓
|
| 733 |
|
| 734 |
+
**Provide Those Sections to the Language Model**
|
| 735 |
|
| 736 |
+
↓
|
| 737 |
|
| 738 |
+
**Generate the Answer**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
|
| 740 |
+
This is a simple **Retrieval-Augmented Generation (RAG)**
|
| 741 |
+
implementation.
|
| 742 |
|
| 743 |
+
*QM640 Data Analytics Capstone*
|
| 744 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
)
|
| 746 |
|
| 747 |
|
| 748 |
# ================================================================
|
| 749 |
+
# 15. START APPLICATION
|
| 750 |
# ================================================================
|
| 751 |
|
| 752 |
+
print("\n" + "=" * 70)
|
| 753 |
+
|
| 754 |
+
print("✅ Wine Quality Research Assistant is ready.")
|
| 755 |
+
|
| 756 |
+
print("=" * 70)
|
| 757 |
|
| 758 |
|
| 759 |
if __name__ == "__main__":
|