Update app.py
Browse files
app.py
CHANGED
|
@@ -101,15 +101,16 @@ def chat_fn(user_input, history):
|
|
| 101 |
|
| 102 |
context = retrieve_context(user_input)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
if not context:
|
| 106 |
response = "I don't know."
|
|
|
|
| 107 |
else:
|
| 108 |
-
#
|
| 109 |
if len(context.split()) < 50:
|
| 110 |
response = context.strip()
|
|
|
|
| 111 |
else:
|
| 112 |
-
# π₯ Use LLM with strict instruction
|
| 113 |
prompt = f"""
|
| 114 |
You are a livestock expert assistant for goats and cows.
|
| 115 |
|
|
@@ -125,16 +126,24 @@ Question:
|
|
| 125 |
Answer in short and clear sentences.
|
| 126 |
"""
|
| 127 |
|
| 128 |
-
output = generator(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
text = output[0]["generated_text"]
|
| 130 |
|
| 131 |
-
# Clean output
|
| 132 |
if prompt.strip() in text:
|
| 133 |
text = text.split(prompt.strip())[-1].strip()
|
| 134 |
|
| 135 |
response = text
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
return history
|
| 139 |
|
| 140 |
# -----------------------------
|
|
@@ -143,8 +152,8 @@ Answer in short and clear sentences.
|
|
| 143 |
with gr.Blocks() as demo:
|
| 144 |
gr.Markdown("## π Livestock Chatbot (RAG + Qwen)")
|
| 145 |
|
| 146 |
-
chatbot = gr.Chatbot()
|
| 147 |
-
msg = gr.Textbox(
|
| 148 |
btn = gr.Button("Send")
|
| 149 |
|
| 150 |
btn.click(chat_fn, [msg, chatbot], chatbot)
|
|
|
|
| 101 |
|
| 102 |
context = retrieve_context(user_input)
|
| 103 |
|
| 104 |
+
# No context
|
| 105 |
if not context:
|
| 106 |
response = "I don't know."
|
| 107 |
+
|
| 108 |
else:
|
| 109 |
+
# Small context β direct RAG
|
| 110 |
if len(context.split()) < 50:
|
| 111 |
response = context.strip()
|
| 112 |
+
|
| 113 |
else:
|
|
|
|
| 114 |
prompt = f"""
|
| 115 |
You are a livestock expert assistant for goats and cows.
|
| 116 |
|
|
|
|
| 126 |
Answer in short and clear sentences.
|
| 127 |
"""
|
| 128 |
|
| 129 |
+
output = generator(
|
| 130 |
+
prompt,
|
| 131 |
+
max_new_tokens=120, # β
remove max_length warning
|
| 132 |
+
do_sample=True,
|
| 133 |
+
temperature=0.6
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
text = output[0]["generated_text"]
|
| 137 |
|
|
|
|
| 138 |
if prompt.strip() in text:
|
| 139 |
text = text.split(prompt.strip())[-1].strip()
|
| 140 |
|
| 141 |
response = text
|
| 142 |
|
| 143 |
+
# β
FIXED FORMAT (IMPORTANT)
|
| 144 |
+
history.append({"role": "user", "content": user_input})
|
| 145 |
+
history.append({"role": "assistant", "content": response})
|
| 146 |
+
|
| 147 |
return history
|
| 148 |
|
| 149 |
# -----------------------------
|
|
|
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
gr.Markdown("## π Livestock Chatbot (RAG + Qwen)")
|
| 154 |
|
| 155 |
+
chatbot = gr.Chatbot(type="messages") # β
REQUIRED
|
| 156 |
+
msg = gr.Textbox()
|
| 157 |
btn = gr.Button("Send")
|
| 158 |
|
| 159 |
btn.click(chat_fn, [msg, chatbot], chatbot)
|