Goated121 commited on
Commit
81026e1
Β·
verified Β·
1 Parent(s): b30d6cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -101,15 +101,16 @@ def chat_fn(user_input, history):
101
 
102
  context = retrieve_context(user_input)
103
 
104
- # πŸ”₯ If no context β†’ strict fallback
105
  if not context:
106
  response = "I don't know."
 
107
  else:
108
- # πŸ”₯ If context is small β†’ return directly (FAST RAG)
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(prompt)
 
 
 
 
 
 
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
- history.append((user_input, response))
 
 
 
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(label="Ask about goats or cows")
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)