Update app.py
Browse files
app.py
CHANGED
|
@@ -13,33 +13,11 @@ print("Files in current directory:", os.listdir())
|
|
| 13 |
# Load RAG components
|
| 14 |
# -----------------------------
|
| 15 |
embed_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
|
|
|
| 16 |
index = faiss.read_index("faiss_index.bin")
|
| 17 |
chunks = pickle.load(open("chunks.pkl", "rb"))
|
| 18 |
metadata = pickle.load(open("metadata.pkl", "rb"))
|
| 19 |
|
| 20 |
-
# -----------------------------
|
| 21 |
-
# Load Qwen 2.5B Instruct model
|
| 22 |
-
# -----------------------------
|
| 23 |
-
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 24 |
-
|
| 25 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 26 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
-
model_name,
|
| 28 |
-
device_map="auto",
|
| 29 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
generator = pipeline(
|
| 33 |
-
"text-generation",
|
| 34 |
-
model=model,
|
| 35 |
-
tokenizer=tokenizer,
|
| 36 |
-
max_new_tokens=200,
|
| 37 |
-
do_sample=True,
|
| 38 |
-
temperature=0.6
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
print("Qwen model loaded successfully!")
|
| 42 |
-
|
| 43 |
# -----------------------------
|
| 44 |
# Intent detection
|
| 45 |
# -----------------------------
|
|
@@ -63,40 +41,69 @@ def detect_query(query):
|
|
| 63 |
# -----------------------------
|
| 64 |
# Retrieve context (RAG)
|
| 65 |
# -----------------------------
|
| 66 |
-
def retrieve_context(query
|
| 67 |
animal, topic = detect_query(query)
|
| 68 |
|
| 69 |
-
filtered_indices = [
|
| 70 |
-
|
| 71 |
-
if
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
if not filtered_indices:
|
| 76 |
filtered_indices = list(range(len(chunks)))
|
| 77 |
|
| 78 |
query_embedding = embed_model.encode([query])
|
| 79 |
filtered_embeddings = np.array([index.reconstruct(i) for i in filtered_indices])
|
| 80 |
-
|
| 81 |
distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
|
| 82 |
-
top_indices = distances.argsort()[:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
context = "\n".join(chunks[filtered_indices[idx]] for idx in top_indices)
|
| 85 |
return context.strip()
|
| 86 |
|
| 87 |
# -----------------------------
|
| 88 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# -----------------------------
|
| 90 |
def chat(user_input):
|
| 91 |
context = retrieve_context(user_input)
|
|
|
|
| 92 |
if not context:
|
| 93 |
return "I don't know."
|
| 94 |
|
| 95 |
prompt = f"""
|
| 96 |
You are a livestock expert assistant.
|
| 97 |
|
| 98 |
-
Use ONLY the information below to answer
|
| 99 |
-
If
|
| 100 |
|
| 101 |
Context:
|
| 102 |
{context}
|
|
@@ -104,13 +111,12 @@ Context:
|
|
| 104 |
Question:
|
| 105 |
{user_input}
|
| 106 |
|
| 107 |
-
Answer in
|
| 108 |
"""
|
| 109 |
-
|
| 110 |
-
response = generator(prompt, max_new_tokens=200, do_sample=True, temperature=0.6)
|
| 111 |
text = response[0]["generated_text"]
|
| 112 |
|
| 113 |
-
# Remove prompt
|
| 114 |
if prompt.strip() in text:
|
| 115 |
text = text.split(prompt.strip())[-1].strip()
|
| 116 |
|
|
@@ -121,8 +127,8 @@ Answer in full, clear sentences.
|
|
| 121 |
# -----------------------------
|
| 122 |
gr.Interface(
|
| 123 |
fn=chat,
|
| 124 |
-
inputs=
|
| 125 |
-
outputs=
|
| 126 |
title="Livestock Chatbot (RAG + Qwen)",
|
| 127 |
-
description="This chatbot answers livestock questions using
|
| 128 |
).launch()
|
|
|
|
| 13 |
# Load RAG components
|
| 14 |
# -----------------------------
|
| 15 |
embed_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 16 |
+
|
| 17 |
index = faiss.read_index("faiss_index.bin")
|
| 18 |
chunks = pickle.load(open("chunks.pkl", "rb"))
|
| 19 |
metadata = pickle.load(open("metadata.pkl", "rb"))
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# -----------------------------
|
| 22 |
# Intent detection
|
| 23 |
# -----------------------------
|
|
|
|
| 41 |
# -----------------------------
|
| 42 |
# Retrieve context (RAG)
|
| 43 |
# -----------------------------
|
| 44 |
+
def retrieve_context(query):
|
| 45 |
animal, topic = detect_query(query)
|
| 46 |
|
| 47 |
+
filtered_indices = []
|
| 48 |
+
for i, meta in enumerate(metadata):
|
| 49 |
+
if animal and meta["animal"] != animal:
|
| 50 |
+
continue
|
| 51 |
+
if topic and meta["topic"] != topic:
|
| 52 |
+
continue
|
| 53 |
+
filtered_indices.append(i)
|
| 54 |
|
| 55 |
if not filtered_indices:
|
| 56 |
filtered_indices = list(range(len(chunks)))
|
| 57 |
|
| 58 |
query_embedding = embed_model.encode([query])
|
| 59 |
filtered_embeddings = np.array([index.reconstruct(i) for i in filtered_indices])
|
|
|
|
| 60 |
distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
|
| 61 |
+
top_indices = distances.argsort()[:2]
|
| 62 |
+
|
| 63 |
+
context = ""
|
| 64 |
+
for idx in top_indices:
|
| 65 |
+
real_index = filtered_indices[idx]
|
| 66 |
+
context += chunks[real_index] + "\n"
|
| 67 |
|
|
|
|
| 68 |
return context.strip()
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
+
# Load Qwen model (CPU only, no accelerate)
|
| 72 |
+
# -----------------------------
|
| 73 |
+
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 74 |
+
|
| 75 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 76 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 77 |
+
model_name,
|
| 78 |
+
torch_dtype=torch.float32 # CPU only
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
generator = pipeline(
|
| 82 |
+
"text-generation",
|
| 83 |
+
model=model,
|
| 84 |
+
tokenizer=tokenizer,
|
| 85 |
+
max_new_tokens=150,
|
| 86 |
+
do_sample=True,
|
| 87 |
+
temperature=0.6,
|
| 88 |
+
device=-1 # ensures CPU is used
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
print("LLM loaded successfully!")
|
| 92 |
+
|
| 93 |
+
# -----------------------------
|
| 94 |
+
# Chat function
|
| 95 |
# -----------------------------
|
| 96 |
def chat(user_input):
|
| 97 |
context = retrieve_context(user_input)
|
| 98 |
+
|
| 99 |
if not context:
|
| 100 |
return "I don't know."
|
| 101 |
|
| 102 |
prompt = f"""
|
| 103 |
You are a livestock expert assistant.
|
| 104 |
|
| 105 |
+
Use ONLY the information below to answer.
|
| 106 |
+
If answer is not present, say "I don't know".
|
| 107 |
|
| 108 |
Context:
|
| 109 |
{context}
|
|
|
|
| 111 |
Question:
|
| 112 |
{user_input}
|
| 113 |
|
| 114 |
+
Answer in short and clear sentences.
|
| 115 |
"""
|
| 116 |
+
response = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.6)
|
|
|
|
| 117 |
text = response[0]["generated_text"]
|
| 118 |
|
| 119 |
+
# Remove prompt if repeated
|
| 120 |
if prompt.strip() in text:
|
| 121 |
text = text.split(prompt.strip())[-1].strip()
|
| 122 |
|
|
|
|
| 127 |
# -----------------------------
|
| 128 |
gr.Interface(
|
| 129 |
fn=chat,
|
| 130 |
+
inputs="text",
|
| 131 |
+
outputs="text",
|
| 132 |
title="Livestock Chatbot (RAG + Qwen)",
|
| 133 |
+
description="This chatbot answers livestock questions using RAG retrieval and Qwen model generation."
|
| 134 |
).launch()
|