Update app.py
Browse files
app.py
CHANGED
|
@@ -39,29 +39,30 @@ def detect_query(query):
|
|
| 39 |
# -----------------------------
|
| 40 |
# Retrieve context (RAG)
|
| 41 |
# -----------------------------
|
| 42 |
-
def retrieve_context(query):
|
| 43 |
animal, topic = detect_query(query)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
filtered_indices.append(i)
|
| 52 |
|
|
|
|
| 53 |
if not filtered_indices:
|
| 54 |
filtered_indices = list(range(len(chunks)))
|
| 55 |
|
|
|
|
| 56 |
query_embedding = embed_model.encode([query])
|
| 57 |
filtered_embeddings = np.array([index.reconstruct(i) for i in filtered_indices])
|
|
|
|
|
|
|
| 58 |
distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
|
| 59 |
-
top_indices = distances.argsort()[:
|
| 60 |
|
| 61 |
-
|
| 62 |
-
for idx in top_indices
|
| 63 |
-
real_index = filtered_indices[idx]
|
| 64 |
-
context += chunks[real_index] + "\n"
|
| 65 |
|
| 66 |
return context.strip()
|
| 67 |
|
|
@@ -72,15 +73,18 @@ def chat(user_input):
|
|
| 72 |
context = retrieve_context(user_input)
|
| 73 |
if not context:
|
| 74 |
return "I don't know."
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# -----------------------------
|
| 78 |
# Gradio UI
|
| 79 |
# -----------------------------
|
| 80 |
gr.Interface(
|
| 81 |
fn=chat,
|
| 82 |
-
inputs="
|
| 83 |
-
outputs=
|
| 84 |
title="Livestock Chatbot (RAG only)",
|
| 85 |
-
description="This chatbot answers livestock questions using only
|
|
|
|
| 86 |
).launch()
|
|
|
|
| 39 |
# -----------------------------
|
| 40 |
# Retrieve context (RAG)
|
| 41 |
# -----------------------------
|
| 42 |
+
def retrieve_context(query, top_k=2):
|
| 43 |
animal, topic = detect_query(query)
|
| 44 |
|
| 45 |
+
# Filter relevant chunks based on metadata
|
| 46 |
+
filtered_indices = [
|
| 47 |
+
i for i, meta in enumerate(metadata)
|
| 48 |
+
if (not animal or meta["animal"] == animal) and
|
| 49 |
+
(not topic or meta["topic"] == topic)
|
| 50 |
+
]
|
|
|
|
| 51 |
|
| 52 |
+
# If no specific filter matches, consider all chunks
|
| 53 |
if not filtered_indices:
|
| 54 |
filtered_indices = list(range(len(chunks)))
|
| 55 |
|
| 56 |
+
# Embed query
|
| 57 |
query_embedding = embed_model.encode([query])
|
| 58 |
filtered_embeddings = np.array([index.reconstruct(i) for i in filtered_indices])
|
| 59 |
+
|
| 60 |
+
# Compute distances and get top-k closest chunks
|
| 61 |
distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
|
| 62 |
+
top_indices = distances.argsort()[:top_k]
|
| 63 |
|
| 64 |
+
# Combine top chunks into context
|
| 65 |
+
context = "\n".join(chunks[filtered_indices[idx]] for idx in top_indices)
|
|
|
|
|
|
|
| 66 |
|
| 67 |
return context.strip()
|
| 68 |
|
|
|
|
| 73 |
context = retrieve_context(user_input)
|
| 74 |
if not context:
|
| 75 |
return "I don't know."
|
| 76 |
+
|
| 77 |
+
# Return context with clear formatting
|
| 78 |
+
return f"Answer from retrieved data:\n\n{context}"
|
| 79 |
|
| 80 |
# -----------------------------
|
| 81 |
# Gradio UI
|
| 82 |
# -----------------------------
|
| 83 |
gr.Interface(
|
| 84 |
fn=chat,
|
| 85 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask a question about livestock..."),
|
| 86 |
+
outputs=gr.Textbox(),
|
| 87 |
title="Livestock Chatbot (RAG only)",
|
| 88 |
+
description="This chatbot answers livestock questions using only retrieved data. No AI model is used.",
|
| 89 |
+
allow_flagging="never"
|
| 90 |
).launch()
|