Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,122 @@
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
| 5 |
model = Llama(
|
| 6 |
model_path="qwen2.5-1.5B-q4.gguf",
|
| 7 |
n_ctx=4096,
|
| 8 |
n_gpu_layers=0,
|
| 9 |
-
chat_format="qwen",
|
| 10 |
)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def chat(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
messages = [
|
| 14 |
-
{"role": "system", "content": "You are a helpful assistant.
|
| 15 |
-
{"role": "user", "content":
|
| 16 |
]
|
| 17 |
|
| 18 |
response = model.create_chat_completion(
|
| 19 |
messages=messages,
|
| 20 |
-
max_tokens=
|
| 21 |
-
temperature=0.
|
| 22 |
)
|
| 23 |
|
| 24 |
return response["choices"][0]["message"]["content"]
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
gr.Interface(
|
| 27 |
fn=chat,
|
| 28 |
inputs="text",
|
| 29 |
outputs="text",
|
| 30 |
-
title="
|
| 31 |
).launch()
|
|
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
import gradio as gr
|
| 3 |
+
import faiss
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Load LLM
|
| 10 |
+
# -----------------------------
|
| 11 |
model = Llama(
|
| 12 |
model_path="qwen2.5-1.5B-q4.gguf",
|
| 13 |
n_ctx=4096,
|
| 14 |
n_gpu_layers=0,
|
| 15 |
+
chat_format="qwen",
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# -----------------------------
|
| 19 |
+
# Load RAG
|
| 20 |
+
# -----------------------------
|
| 21 |
+
embed_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 22 |
+
|
| 23 |
+
index = faiss.read_index("faiss_index.bin")
|
| 24 |
+
chunks = pickle.load(open("chunks.pkl", "rb"))
|
| 25 |
+
metadata = pickle.load(open("metadata.pkl", "rb"))
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Detect query intent
|
| 29 |
+
# -----------------------------
|
| 30 |
+
def detect_query(query):
|
| 31 |
+
query = query.lower()
|
| 32 |
+
|
| 33 |
+
animal = None
|
| 34 |
+
topic = None
|
| 35 |
+
|
| 36 |
+
if "goat" in query:
|
| 37 |
+
animal = "goat"
|
| 38 |
+
elif "cow" in query:
|
| 39 |
+
animal = "cow"
|
| 40 |
+
|
| 41 |
+
if any(word in query for word in ["feed", "diet", "khilana"]):
|
| 42 |
+
topic = "feeding"
|
| 43 |
+
elif any(word in query for word in ["disease", "bimari"]):
|
| 44 |
+
topic = "disease"
|
| 45 |
+
|
| 46 |
+
return animal, topic
|
| 47 |
+
|
| 48 |
+
# -----------------------------
|
| 49 |
+
# Retrieve context (RAG)
|
| 50 |
+
# -----------------------------
|
| 51 |
+
def retrieve_context(query):
|
| 52 |
+
animal, topic = detect_query(query)
|
| 53 |
+
|
| 54 |
+
filtered_indices = []
|
| 55 |
+
for i, meta in enumerate(metadata):
|
| 56 |
+
if animal and meta["animal"] != animal:
|
| 57 |
+
continue
|
| 58 |
+
if topic and meta["topic"] != topic:
|
| 59 |
+
continue
|
| 60 |
+
filtered_indices.append(i)
|
| 61 |
+
|
| 62 |
+
if not filtered_indices:
|
| 63 |
+
filtered_indices = list(range(len(chunks)))
|
| 64 |
+
|
| 65 |
+
query_embedding = embed_model.encode([query])
|
| 66 |
+
|
| 67 |
+
filtered_embeddings = [index.reconstruct(i) for i in filtered_indices]
|
| 68 |
+
filtered_embeddings = np.array(filtered_embeddings)
|
| 69 |
+
|
| 70 |
+
distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
|
| 71 |
+
top_indices = distances.argsort()[:2]
|
| 72 |
+
|
| 73 |
+
context = ""
|
| 74 |
+
for idx in top_indices:
|
| 75 |
+
real_index = filtered_indices[idx]
|
| 76 |
+
context += chunks[real_index] + "\n"
|
| 77 |
+
|
| 78 |
+
return context
|
| 79 |
+
|
| 80 |
+
# -----------------------------
|
| 81 |
+
# Chat function (UPDATED)
|
| 82 |
+
# -----------------------------
|
| 83 |
def chat(user_input):
|
| 84 |
+
context = retrieve_context(user_input)
|
| 85 |
+
|
| 86 |
+
prompt = f"""
|
| 87 |
+
You are a livestock expert assistant.
|
| 88 |
+
|
| 89 |
+
Use ONLY the information below to answer.
|
| 90 |
+
If answer is not present, say "I don't know".
|
| 91 |
+
|
| 92 |
+
Context:
|
| 93 |
+
{context}
|
| 94 |
+
|
| 95 |
+
Question:
|
| 96 |
+
{user_input}
|
| 97 |
+
|
| 98 |
+
Answer in short and clear sentences.
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
messages = [
|
| 102 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 103 |
+
{"role": "user", "content": prompt}
|
| 104 |
]
|
| 105 |
|
| 106 |
response = model.create_chat_completion(
|
| 107 |
messages=messages,
|
| 108 |
+
max_tokens=200,
|
| 109 |
+
temperature=0.5,
|
| 110 |
)
|
| 111 |
|
| 112 |
return response["choices"][0]["message"]["content"]
|
| 113 |
|
| 114 |
+
# -----------------------------
|
| 115 |
+
# Gradio UI (UNCHANGED)
|
| 116 |
+
# -----------------------------
|
| 117 |
gr.Interface(
|
| 118 |
fn=chat,
|
| 119 |
inputs="text",
|
| 120 |
outputs="text",
|
| 121 |
+
title="Livestock Chatbot"
|
| 122 |
).launch()
|