vedaco commited on
Commit
7591fe1
Β·
verified Β·
1 Parent(s): 5302b7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -45
app.py CHANGED
@@ -139,23 +139,18 @@ def generate_response(user_input: str, temperature: float = 0.7, max_tokens: int
139
  return f"Error: {str(e)}"
140
 
141
 
142
- def chat_fn(user_input, history, temperature, max_tokens):
143
- """Chat function for Gradio 6.x with messages format"""
144
- if not user_input or not user_input.strip():
145
- return "", history or []
146
 
147
- response = generate_response(user_input, temperature, max_tokens)
148
 
149
- if history is None:
150
- history = []
151
 
152
- # Gradio 6.x requires messages format with 'role' and 'content'
153
- history = history + [
154
- {"role": "user", "content": user_input},
155
- {"role": "assistant", "content": response}
156
- ]
157
-
158
- return "", history
159
 
160
 
161
  def feedback_good():
@@ -168,14 +163,14 @@ def feedback_good():
168
  def feedback_bad():
169
  if current_conv_id > 0:
170
  db.update_feedback(current_conv_id, -1)
171
- return "πŸ‘Ž Thanks for the feedback. I will try to improve."
172
  return "No conversation to rate yet."
173
 
174
 
175
- def clear_fn():
176
  global conversation_history
177
  conversation_history = []
178
- return [], "Conversation cleared."
179
 
180
 
181
  def retrain(epochs):
@@ -185,7 +180,7 @@ def retrain(epochs):
185
  good_convs = db.get_good_conversations()
186
 
187
  if not good_convs:
188
- return "No approved conversations yet. Rate some responses first!"
189
 
190
  extra_data = ""
191
  for conv in good_convs:
@@ -199,7 +194,7 @@ def retrain(epochs):
199
  tokenizer = trainer.tokenizer
200
 
201
  loss = history.history['loss'][-1]
202
- return f"βœ… Training done! Loss: {loss:.4f}, Used {len(good_convs)} conversations"
203
 
204
 
205
  def get_stats():
@@ -208,9 +203,9 @@ def get_stats():
208
 
209
  | Metric | Count |
210
  |--------|-------|
211
- | πŸ’¬ Conversations | {stats['total']} |
212
- | πŸ‘ Positive | {stats['positive']} |
213
- | πŸ‘Ž Negative | {stats['negative']} |
214
  """
215
 
216
 
@@ -220,23 +215,22 @@ initialize()
220
  print("Initialization complete!")
221
 
222
 
223
- # Create Gradio interface for Gradio 6.x
224
  with gr.Blocks(title="Veda Programming Assistant") as demo:
225
 
226
  gr.Markdown("""
227
  # πŸ•‰οΈ Veda Programming Assistant
228
 
229
- I can chat, write code, explain concepts, and answer programming questions!
230
  """)
231
 
232
  with gr.Tabs():
233
 
234
  with gr.TabItem("πŸ’¬ Chat"):
235
- # Chatbot with messages format for Gradio 6.x
236
  chatbot = gr.Chatbot(
237
  label="Conversation",
238
- height=400,
239
- type="messages"
240
  )
241
 
242
  with gr.Row():
@@ -273,22 +267,22 @@ with gr.Blocks(title="Veda Programming Assistant") as demo:
273
 
274
  # Event handlers
275
  send_btn.click(
276
- chat_fn,
277
  inputs=[msg, chatbot, temperature, max_tokens],
278
  outputs=[msg, chatbot]
279
  )
280
 
281
  msg.submit(
282
- chat_fn,
283
  inputs=[msg, chatbot, temperature, max_tokens],
284
  outputs=[msg, chatbot]
285
  )
286
 
287
  good_btn.click(feedback_good, outputs=feedback_msg)
288
  bad_btn.click(feedback_bad, outputs=feedback_msg)
289
- clear_btn.click(clear_fn, outputs=[chatbot, feedback_msg])
290
 
291
- gr.Markdown("### πŸ’‘ Example prompts")
292
  gr.Examples(
293
  examples=[
294
  ["Hello! What can you do?"],
@@ -302,15 +296,20 @@ with gr.Blocks(title="Veda Programming Assistant") as demo:
302
  )
303
 
304
  with gr.TabItem("πŸŽ“ Training"):
305
- gr.Markdown("### Train on approved conversations")
306
- gr.Markdown("Rate responses as 'Good' to add them to training data.")
 
 
 
 
 
307
 
308
  train_epochs = gr.Slider(
309
  minimum=5,
310
  maximum=20,
311
  value=10,
312
  step=1,
313
- label="Epochs"
314
  )
315
  train_btn = gr.Button("πŸ”„ Retrain Model", variant="primary")
316
  train_output = gr.Markdown()
@@ -318,20 +317,15 @@ with gr.Blocks(title="Veda Programming Assistant") as demo:
318
 
319
  with gr.TabItem("πŸ“Š Statistics"):
320
  stats_out = gr.Markdown()
321
- refresh_btn = gr.Button("πŸ”„ Refresh")
322
  refresh_btn.click(get_stats, outputs=stats_out)
323
-
324
- gr.Markdown("""
325
- ### How it works
326
- 1. Chat with the assistant
327
- 2. Rate responses as Good πŸ‘ or Bad πŸ‘Ž
328
- 3. Good responses are saved for training
329
- 4. Click 'Retrain Model' to improve the assistant
330
- """)
331
 
332
- gr.Markdown("---\n**Veda Programming Assistant** - Learning from conversations!")
 
 
 
333
 
334
 
335
- # Launch
336
  if __name__ == "__main__":
337
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
139
  return f"Error: {str(e)}"
140
 
141
 
142
+ def respond(message, chat_history, temperature, max_tokens):
143
+ """Chat function - returns tuple format"""
144
+ if not message or not message.strip():
145
+ return "", chat_history if chat_history else []
146
 
147
+ bot_message = generate_response(message, temperature, max_tokens)
148
 
149
+ if chat_history is None:
150
+ chat_history = []
151
 
152
+ chat_history.append((message, bot_message))
153
+ return "", chat_history
 
 
 
 
 
154
 
155
 
156
  def feedback_good():
 
163
  def feedback_bad():
164
  if current_conv_id > 0:
165
  db.update_feedback(current_conv_id, -1)
166
+ return "πŸ‘Ž Thanks! I'll try to improve."
167
  return "No conversation to rate yet."
168
 
169
 
170
+ def clear_chat():
171
  global conversation_history
172
  conversation_history = []
173
+ return [], "Chat cleared."
174
 
175
 
176
  def retrain(epochs):
 
180
  good_convs = db.get_good_conversations()
181
 
182
  if not good_convs:
183
+ return "No approved conversations yet. Rate some responses as 'Good' first!"
184
 
185
  extra_data = ""
186
  for conv in good_convs:
 
194
  tokenizer = trainer.tokenizer
195
 
196
  loss = history.history['loss'][-1]
197
+ return f"βœ… Training complete! Loss: {loss:.4f}, Used {len(good_convs)} conversations"
198
 
199
 
200
  def get_stats():
 
203
 
204
  | Metric | Count |
205
  |--------|-------|
206
+ | πŸ’¬ Total Conversations | {stats['total']} |
207
+ | πŸ‘ Positive Feedback | {stats['positive']} |
208
+ | πŸ‘Ž Negative Feedback | {stats['negative']} |
209
  """
210
 
211
 
 
215
  print("Initialization complete!")
216
 
217
 
218
+ # Create Gradio interface - NO type parameter
219
  with gr.Blocks(title="Veda Programming Assistant") as demo:
220
 
221
  gr.Markdown("""
222
  # πŸ•‰οΈ Veda Programming Assistant
223
 
224
+ I can **chat**, **write code**, **explain concepts**, and **answer questions**!
225
  """)
226
 
227
  with gr.Tabs():
228
 
229
  with gr.TabItem("πŸ’¬ Chat"):
230
+ # Simple Chatbot without type parameter
231
  chatbot = gr.Chatbot(
232
  label="Conversation",
233
+ height=400
 
234
  )
235
 
236
  with gr.Row():
 
267
 
268
  # Event handlers
269
  send_btn.click(
270
+ respond,
271
  inputs=[msg, chatbot, temperature, max_tokens],
272
  outputs=[msg, chatbot]
273
  )
274
 
275
  msg.submit(
276
+ respond,
277
  inputs=[msg, chatbot, temperature, max_tokens],
278
  outputs=[msg, chatbot]
279
  )
280
 
281
  good_btn.click(feedback_good, outputs=feedback_msg)
282
  bad_btn.click(feedback_bad, outputs=feedback_msg)
283
+ clear_btn.click(clear_chat, outputs=[chatbot, feedback_msg])
284
 
285
+ gr.Markdown("### πŸ’‘ Try these examples:")
286
  gr.Examples(
287
  examples=[
288
  ["Hello! What can you do?"],
 
296
  )
297
 
298
  with gr.TabItem("πŸŽ“ Training"):
299
+ gr.Markdown("""
300
+ ### Improve the Assistant
301
+
302
+ 1. Chat with the assistant
303
+ 2. Rate good responses with πŸ‘
304
+ 3. Click "Retrain Model" to learn from good conversations
305
+ """)
306
 
307
  train_epochs = gr.Slider(
308
  minimum=5,
309
  maximum=20,
310
  value=10,
311
  step=1,
312
+ label="Training Epochs"
313
  )
314
  train_btn = gr.Button("πŸ”„ Retrain Model", variant="primary")
315
  train_output = gr.Markdown()
 
317
 
318
  with gr.TabItem("πŸ“Š Statistics"):
319
  stats_out = gr.Markdown()
320
+ refresh_btn = gr.Button("πŸ”„ Refresh Statistics")
321
  refresh_btn.click(get_stats, outputs=stats_out)
 
 
 
 
 
 
 
 
322
 
323
+ gr.Markdown("""
324
+ ---
325
+ **Veda Programming Assistant** - Learning from every conversation!
326
+ """)
327
 
328
 
329
+ # Launch the app
330
  if __name__ == "__main__":
331
  demo.launch(server_name="0.0.0.0", server_port=7860)