Update app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,9 @@ print("Loading sentence-transformer model for retrieval...")
|
|
| 9 |
retriever_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
print("Retriever model loaded.")
|
| 11 |
|
| 12 |
-
# --- THIS IS THE UPDATED LINE ---
|
| 13 |
print("Loading generative model for answering (google/flan-t5-base)...")
|
| 14 |
-
# Using the balanced 'base' model for better performance and reliability.
|
| 15 |
generator_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 16 |
print("Generative model loaded.")
|
| 17 |
-
# --- END OF UPDATE ---
|
| 18 |
|
| 19 |
|
| 20 |
# --- 2. Setup ChromaDB ---
|
|
@@ -71,12 +68,14 @@ except ValueError:
|
|
| 71 |
print("ChromaDB collection loaded.")
|
| 72 |
|
| 73 |
|
| 74 |
-
# --- 3. Define Chatbot Logic ---
|
| 75 |
def chatbot_response(message, history):
|
| 76 |
query_embedding = retriever_model.encode([message]).tolist()
|
|
|
|
|
|
|
| 77 |
results = collection.query(
|
| 78 |
query_embeddings=query_embedding,
|
| 79 |
-
n_results=
|
| 80 |
)
|
| 81 |
retrieved_documents = results['documents'][0]
|
| 82 |
|
|
@@ -84,8 +83,12 @@ def chatbot_response(message, history):
|
|
| 84 |
return "I'm sorry, I couldn't find any relevant information in the chat history. 🤔"
|
| 85 |
|
| 86 |
context = "\n- ".join(retrieved_documents)
|
|
|
|
|
|
|
| 87 |
prompt = f"""
|
| 88 |
-
Based on the following excerpts from a WhatsApp chat, provide a
|
|
|
|
|
|
|
| 89 |
|
| 90 |
Chat Context:
|
| 91 |
- {context}
|
|
@@ -93,10 +96,11 @@ def chatbot_response(message, history):
|
|
| 93 |
Question:
|
| 94 |
{message}
|
| 95 |
|
| 96 |
-
Answer:
|
| 97 |
"""
|
| 98 |
|
| 99 |
-
|
|
|
|
| 100 |
response = generated_text[0]['generated_text']
|
| 101 |
|
| 102 |
return response
|
|
@@ -104,10 +108,10 @@ def chatbot_response(message, history):
|
|
| 104 |
# --- 4. Create the Gradio Interface ---
|
| 105 |
iface = gr.ChatInterface(
|
| 106 |
fn=chatbot_response,
|
| 107 |
-
title="WhatsApp Chat Bot
|
| 108 |
-
description="Ask me anything about this WhatsApp chat history.
|
| 109 |
theme="soft",
|
| 110 |
-
examples=["
|
| 111 |
cache_examples=False
|
| 112 |
)
|
| 113 |
|
|
|
|
| 9 |
retriever_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
print("Retriever model loaded.")
|
| 11 |
|
|
|
|
| 12 |
print("Loading generative model for answering (google/flan-t5-base)...")
|
|
|
|
| 13 |
generator_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 14 |
print("Generative model loaded.")
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
# --- 2. Setup ChromaDB ---
|
|
|
|
| 68 |
print("ChromaDB collection loaded.")
|
| 69 |
|
| 70 |
|
| 71 |
+
# --- 3. Define Chatbot Logic (UPDATED SECTION) ---
|
| 72 |
def chatbot_response(message, history):
|
| 73 |
query_embedding = retriever_model.encode([message]).tolist()
|
| 74 |
+
|
| 75 |
+
# --- 1. INCREASED CONTEXT ---
|
| 76 |
results = collection.query(
|
| 77 |
query_embeddings=query_embedding,
|
| 78 |
+
n_results=10 # Retrieve more context
|
| 79 |
)
|
| 80 |
retrieved_documents = results['documents'][0]
|
| 81 |
|
|
|
|
| 83 |
return "I'm sorry, I couldn't find any relevant information in the chat history. 🤔"
|
| 84 |
|
| 85 |
context = "\n- ".join(retrieved_documents)
|
| 86 |
+
|
| 87 |
+
# --- 2. ENHANCED PROMPT ---
|
| 88 |
prompt = f"""
|
| 89 |
+
Based ONLY on the following excerpts from a WhatsApp chat, provide a detailed and comprehensive answer to the user's question.
|
| 90 |
+
Synthesize the information from all relevant excerpts into a single, well-structured response.
|
| 91 |
+
Do not make up information.
|
| 92 |
|
| 93 |
Chat Context:
|
| 94 |
- {context}
|
|
|
|
| 96 |
Question:
|
| 97 |
{message}
|
| 98 |
|
| 99 |
+
Detailed Answer:
|
| 100 |
"""
|
| 101 |
|
| 102 |
+
# --- 3. INCREASED MAX LENGTH ---
|
| 103 |
+
generated_text = generator_pipe(prompt, max_length=400, num_beams=5, early_stopping=True)
|
| 104 |
response = generated_text[0]['generated_text']
|
| 105 |
|
| 106 |
return response
|
|
|
|
| 108 |
# --- 4. Create the Gradio Interface ---
|
| 109 |
iface = gr.ChatInterface(
|
| 110 |
fn=chatbot_response,
|
| 111 |
+
title="WhatsApp Chat Bot ✍️",
|
| 112 |
+
description="Ask me anything about this WhatsApp chat history. I will provide a detailed response.",
|
| 113 |
theme="soft",
|
| 114 |
+
examples=["Summarize the conversation about the event in detail.", "What were all the points raised about the project deadline?"],
|
| 115 |
cache_examples=False
|
| 116 |
)
|
| 117 |
|