Simple_ChatBot / app.py
MBG0903's picture
Update app.py
5d7cdd2 verified
Raw History Blame Contribute Delete
12.7 kB
# ================================================================
# Wine Quality Research Assistant
# Simple RAG Chatbot for Hugging Face Spaces
# Gradio 6.x Compatible
# ================================================================
import os
import re
import numpy as np
import torch
import gradio as gr
from pypdf import PdfReader
from sentence_transformers import SentenceTransformer
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# ================================================================
# 1. CONFIGURATION
# ================================================================
APP_TITLE = "🍷 Wine Quality Research Assistant"
EMBEDDING_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
LLM_MODEL_ID = "google/flan-t5-small"
TOP_K = 3
CHUNK_SIZE = 700
CHUNK_OVERLAP = 100
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print("=" * 70)
print("Starting Wine Quality Research Assistant")
print("=" * 70)
print(f"Device: {DEVICE}")
print(f"Gradio: {gr.__version__}")
# ================================================================
# 2. FIND PDF
# ================================================================
def find_pdf():
pdf_files = [
file
for file in os.listdir(".")
if file.lower().endswith(".pdf")
]
if not pdf_files:
raise FileNotFoundError(
"No PDF found. Upload the capstone PDF "
"to the root folder of the Space."
)
print("\nPDF files detected:")
for pdf in pdf_files:
print(" -", pdf)
return pdf_files[0]
PDF_FILE = find_pdf()
print(f"\nUsing PDF: {PDF_FILE}")
# ================================================================
# 3. CLEAN PDF TEXT
# ================================================================
def clean_text(text):
if not text:
return ""
text = text.replace("\x00", " ")
text = re.sub(
r"\s+",
" ",
text
)
return text.strip()
# ================================================================
# 4. READ PDF
# ================================================================
def read_pdf(pdf_path):
reader = PdfReader(pdf_path)
pages = []
for page_number, page in enumerate(
reader.pages,
start=1
):
try:
text = clean_text(
page.extract_text()
)
if text:
pages.append(
{
"page": page_number,
"text": text
}
)
except Exception as error:
print(
f"Could not read page "
f"{page_number}: {error}"
)
return pages
pages = read_pdf(PDF_FILE)
if not pages:
raise ValueError(
"PDF found, but no readable text "
"could be extracted."
)
print(
f"Loaded {len(pages)} PDF pages."
)
# ================================================================
# 5. CREATE CHUNKS
# ================================================================
def create_chunks(pages):
chunks = []
for page_data in pages:
text = page_data["text"]
page_number = page_data["page"]
start = 0
while start < len(text):
end = start + CHUNK_SIZE
chunk_text = text[
start:end
].strip()
if len(chunk_text) > 50:
chunks.append(
{
"page": page_number,
"text": chunk_text
}
)
start += (
CHUNK_SIZE -
CHUNK_OVERLAP
)
return chunks
chunks = create_chunks(pages)
print(
f"Created {len(chunks)} "
f"knowledge chunks."
)
# ================================================================
# 6. LOAD EMBEDDING MODEL
# ================================================================
print("Loading embedding model...")
embedding_model = SentenceTransformer(
EMBEDDING_MODEL_ID,
device=DEVICE
)
document_texts = [
chunk["text"]
for chunk in chunks
]
document_embeddings = embedding_model.encode(
document_texts,
convert_to_numpy=True,
normalize_embeddings=True,
show_progress_bar=False
)
print("Embedding model ready.")
# ================================================================
# 7. LOAD LANGUAGE MODEL
# ================================================================
print(
f"Loading language model: "
f"{LLM_MODEL_ID}"
)
tokenizer = AutoTokenizer.from_pretrained(
LLM_MODEL_ID
)
llm_model = AutoModelForSeq2SeqLM.from_pretrained(
LLM_MODEL_ID
)
llm_model.to(DEVICE)
llm_model.eval()
print("Language model ready.")
# ================================================================
# 8. RETRIEVE RELEVANT PDF CONTENT
# ================================================================
def retrieve_context(question):
question_embedding = embedding_model.encode(
[question],
convert_to_numpy=True,
normalize_embeddings=True
)[0]
similarity_scores = np.dot(
document_embeddings,
question_embedding
)
top_indices = np.argsort(
similarity_scores
)[-TOP_K:][::-1]
results = []
for index in top_indices:
results.append(
{
"page":
chunks[index]["page"],
"text":
chunks[index]["text"],
"score":
float(
similarity_scores[
index
]
)
}
)
return results
# ================================================================
# 9. BUILD CONTEXT
# ================================================================
def build_context(results):
sections = []
for item in results:
sections.append(
f"""
PAGE {item['page']}:
{item['text']}
"""
)
return "\n".join(sections)
# ================================================================
# 10. FORMAT CHAT HISTORY
# ================================================================
def format_history(history):
if not history:
return ""
conversation = []
# Only retain recent conversation
for item in history[-4:]:
if isinstance(item, dict):
role = item.get(
"role",
""
)
content = item.get(
"content",
""
)
conversation.append(
f"{role}: {content}"
)
return "\n".join(conversation)
# ================================================================
# 11. GENERATE ANSWER
# ================================================================
def generate_answer(
question,
context,
history
):
prompt = f"""
You are a helpful academic research assistant.
You answer questions about the research project:
"Machine Learning-Based Prediction of Wine Quality
Using Physicochemical Properties."
Answer the user's question using ONLY the information
provided in the DOCUMENT CONTEXT.
RULES:
1. Answer the question directly.
2. Use clear and simple language.
3. Be academically accurate.
4. Do not invent information.
5. Do not invent research results.
6. Clearly distinguish proposed methodology from
completed findings.
7. Use bullet points when helpful.
8. Keep the answer focused on the user's question.
9. If the document does not contain the answer, say:
"This information is not specified in the current
capstone synopsis."
DOCUMENT CONTEXT:
{context}
RECENT CONVERSATION:
{history}
QUESTION:
{question}
ANSWER:
"""
inputs = tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=1800
)
inputs = {
key: value.to(DEVICE)
for key, value
in inputs.items()
}
with torch.no_grad():
output = llm_model.generate(
**inputs,
max_new_tokens=220,
num_beams=2,
do_sample=False,
repetition_penalty=1.15,
no_repeat_ngram_size=3
)
answer = tokenizer.decode(
output[0],
skip_special_tokens=True
)
return answer.strip()
# ================================================================
# 12. CHAT FUNCTION
# ================================================================
def chat(message, history):
if not message.strip():
return (
"Please enter a question "
"about the document."
)
print("\n" + "=" * 60)
print(f"QUESTION: {message}")
# Retrieve relevant content
results = retrieve_context(
message
)
print("Retrieved:")
for item in results:
print(
f"Page {item['page']} | "
f"Score: {item['score']:.3f}"
)
context = build_context(
results
)
history_text = format_history(
history
)
answer = generate_answer(
message,
context,
history_text
)
# Unique page numbers
source_pages = sorted(
set(
item["page"]
for item in results
)
)
page_text = ", ".join(
str(page)
for page in source_pages
)
return f"""
{answer}
---
πŸ“š **Source page(s): {page_text}**
"""
# ================================================================
# 13. CSS
# ================================================================
css = """
.gradio-container {
max-width: 1000px !important;
margin: auto !important;
}
"""
# ================================================================
# 14. USER INTERFACE
# ================================================================
with gr.Blocks(
title=APP_TITLE
) as demo:
# ------------------------------------------------------------
# SIMPLE HEADER
# ------------------------------------------------------------
gr.Markdown(
"""
# 🍷 Wine Quality Research Assistant
### Ask questions about the QM640 Wine Quality Capstone
This chatbot searches the uploaded capstone document and
answers your questions using the relevant information from it.
**Type your question below to begin.**
"""
)
# ------------------------------------------------------------
# SMALL STATUS LINE
# ------------------------------------------------------------
gr.Markdown(
f"""
πŸ“„ **Document loaded:** `{PDF_FILE}`
πŸ“‘ **Pages:** {len(pages)} &nbsp;&nbsp; | &nbsp;&nbsp;
🧩 **Knowledge chunks:** {len(chunks)}
"""
)
# ------------------------------------------------------------
# CHATBOT
# ------------------------------------------------------------
chatbot_component = gr.Chatbot(
height=480,
show_label=False,
placeholder=(
"🍷 Ask me anything about "
"the Wine Quality Capstone."
)
)
# ------------------------------------------------------------
# CHAT INTERFACE
# ------------------------------------------------------------
chat_interface = gr.ChatInterface(
fn=chat,
chatbot=chatbot_component,
textbox=gr.Textbox(
placeholder=(
"Ask a question about "
"the document..."
),
show_label=False
),
submit_btn="Ask",
stop_btn=False,
examples=[
"What is the main objective of the study?",
"What dataset is used in this project?",
"What are the research questions?",
"Which machine learning models will be compared?",
"How will model performance be evaluated?",
"What is the business value of this research?"
]
)
# ------------------------------------------------------------
# FOOTER
# ------------------------------------------------------------
gr.Markdown(
"""
---
πŸ”Ž **RAG-powered:** Your question β†’ Relevant PDF sections β†’
AI-generated answer
*QM640 Data Analytics Capstone*
"""
)
# ================================================================
# 15. LAUNCH
# ================================================================
print("\n" + "=" * 70)
print("Wine Quality Research Assistant is ready.")
print("=" * 70)
if __name__ == "__main__":
demo.queue()
demo.launch(
css=css
)