Update app.py
Browse files
app.py
CHANGED
|
@@ -68,14 +68,16 @@ def retrieve_context(query):
|
|
| 68 |
return context.strip()
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
-
# Load
|
| 72 |
# -----------------------------
|
| 73 |
-
model_name = "
|
|
|
|
| 74 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 75 |
model = AutoModelForCausalLM.from_pretrained(
|
| 76 |
model_name,
|
| 77 |
torch_dtype=torch.float32 # CPU-friendly
|
| 78 |
)
|
|
|
|
| 79 |
generator = pipeline(
|
| 80 |
"text-generation",
|
| 81 |
model=model,
|
|
@@ -83,7 +85,7 @@ generator = pipeline(
|
|
| 83 |
max_new_tokens=150,
|
| 84 |
do_sample=True,
|
| 85 |
temperature=0.6,
|
| 86 |
-
device=-1 # CPU
|
| 87 |
)
|
| 88 |
|
| 89 |
print("LLM loaded successfully!")
|
|
@@ -93,15 +95,14 @@ print("LLM loaded successfully!")
|
|
| 93 |
# -----------------------------
|
| 94 |
def chat(user_input):
|
| 95 |
context = retrieve_context(user_input)
|
| 96 |
-
|
| 97 |
if not context:
|
| 98 |
return "I don't know."
|
| 99 |
|
| 100 |
prompt = f"""
|
| 101 |
-
You are a livestock expert assistant for
|
| 102 |
|
| 103 |
Use ONLY the information below to answer.
|
| 104 |
-
If answer is not present, say "I don't know".
|
| 105 |
|
| 106 |
Context:
|
| 107 |
{context}
|
|
@@ -111,7 +112,7 @@ Question:
|
|
| 111 |
|
| 112 |
Answer in short and clear sentences.
|
| 113 |
"""
|
| 114 |
-
response = generator(prompt
|
| 115 |
text = response[0]["generated_text"]
|
| 116 |
|
| 117 |
# Remove prompt if repeated
|
|
@@ -127,6 +128,6 @@ gr.Interface(
|
|
| 127 |
fn=chat,
|
| 128 |
inputs="text",
|
| 129 |
outputs="text",
|
| 130 |
-
title="Livestock Chatbot (RAG +
|
| 131 |
-
description="This chatbot answers livestock questions using RAG retrieval and
|
| 132 |
).launch()
|
|
|
|
| 68 |
return context.strip()
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
+
# Load Qwen3.5-0.8B (CPU optimized)
|
| 72 |
# -----------------------------
|
| 73 |
+
model_name = "Qwen/Qwen3.5-0.8B-Instruct"
|
| 74 |
+
|
| 75 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 76 |
model = AutoModelForCausalLM.from_pretrained(
|
| 77 |
model_name,
|
| 78 |
torch_dtype=torch.float32 # CPU-friendly
|
| 79 |
)
|
| 80 |
+
|
| 81 |
generator = pipeline(
|
| 82 |
"text-generation",
|
| 83 |
model=model,
|
|
|
|
| 85 |
max_new_tokens=150,
|
| 86 |
do_sample=True,
|
| 87 |
temperature=0.6,
|
| 88 |
+
device=-1 # CPU only
|
| 89 |
)
|
| 90 |
|
| 91 |
print("LLM loaded successfully!")
|
|
|
|
| 95 |
# -----------------------------
|
| 96 |
def chat(user_input):
|
| 97 |
context = retrieve_context(user_input)
|
|
|
|
| 98 |
if not context:
|
| 99 |
return "I don't know."
|
| 100 |
|
| 101 |
prompt = f"""
|
| 102 |
+
You are a livestock expert assistant for goats and cows.
|
| 103 |
|
| 104 |
Use ONLY the information below to answer.
|
| 105 |
+
If the answer is not present, say "I don't know".
|
| 106 |
|
| 107 |
Context:
|
| 108 |
{context}
|
|
|
|
| 112 |
|
| 113 |
Answer in short and clear sentences.
|
| 114 |
"""
|
| 115 |
+
response = generator(prompt)
|
| 116 |
text = response[0]["generated_text"]
|
| 117 |
|
| 118 |
# Remove prompt if repeated
|
|
|
|
| 128 |
fn=chat,
|
| 129 |
inputs="text",
|
| 130 |
outputs="text",
|
| 131 |
+
title="Livestock Chatbot (RAG + Qwen3.5-0.8B)",
|
| 132 |
+
description="This chatbot answers livestock questions using RAG retrieval and Qwen3.5-0.8B model generation (CPU optimized)."
|
| 133 |
).launch()
|