Woziii commited on
Commit
7582c43
·
verified ·
1 Parent(s): 3420b54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -188,18 +188,25 @@ def interact_with_lucas(prompt, messages):
188
  messages[-1] = ChatMessage(role="assistant", content=response)
189
  yield messages
190
 
191
- def vote(data: gr.LikeData, history):
192
- user_input = history[-1][0] if history else ""
193
- feedback = {
 
 
 
 
 
194
  "timestamp": datetime.now().isoformat(),
195
- "user_input": user_input,
196
- "bot_response": data.value,
197
- "liked": data.liked
198
  }
 
199
  api = HfApi()
200
  token = os.environ.get("HF_TOKEN")
201
  repo_id = "Woziii/llama-3-8b-chat-me"
202
  file_name = "feedback.json"
 
203
  try:
204
  try:
205
  file_path = hf_hub_download(repo_id=repo_id, filename=file_name, token=token)
@@ -210,11 +217,14 @@ def vote(data: gr.LikeData, history):
210
  except Exception as e:
211
  print(f"Erreur lors du téléchargement du fichier : {str(e)}")
212
  current_feedback = []
213
- current_feedback.append(feedback)
 
214
  updated_content = json.dumps(current_feedback, ensure_ascii=False, indent=2)
 
215
  temp_file_path = "/tmp/feedback.json"
216
  with open(temp_file_path, "w", encoding="utf-8") as temp_file:
217
  temp_file.write(updated_content)
 
218
  api.upload_file(
219
  path_or_fileobj=temp_file_path,
220
  path_in_repo=file_name,
@@ -222,8 +232,11 @@ def vote(data: gr.LikeData, history):
222
  token=token
223
  )
224
  print(f"Feedback enregistré dans {repo_id}/{file_name}")
 
225
  except Exception as e:
226
  print(f"Erreur lors de l'enregistrement du feedback : {str(e)}")
 
 
227
 
228
 
229
  def load_feedback_data():
@@ -280,8 +293,14 @@ with gr.Blocks() as demo:
280
  text_input = gr.Textbox(lines=1, label="Votre message")
281
  text_input.submit(interact_with_lucas, [text_input, chatbot], [chatbot])
282
 
283
- # Ajout de l'événement de vote
284
- chatbot.like(vote, inputs=[chatbot], outputs=[])
 
 
 
 
 
 
285
 
286
  with gr.Tab("Statistiques"):
287
  gr.Markdown("# Statistiques d'utilisation 📊")
@@ -299,4 +318,3 @@ demo.queue(max_size=20, default_concurrency_limit=2).launch(max_threads=10)
299
 
300
 
301
 
302
-
 
188
  messages[-1] = ChatMessage(role="assistant", content=response)
189
  yield messages
190
 
191
+ def vote(feedback: str, history):
192
+ if not history:
193
+ return "Aucun message à évaluer."
194
+
195
+ last_user_message = history[-1][0]
196
+ last_bot_response = history[-1][1]
197
+
198
+ feedback_data = {
199
  "timestamp": datetime.now().isoformat(),
200
+ "user_input": last_user_message,
201
+ "bot_response": last_bot_response,
202
+ "liked": feedback == "👍"
203
  }
204
+
205
  api = HfApi()
206
  token = os.environ.get("HF_TOKEN")
207
  repo_id = "Woziii/llama-3-8b-chat-me"
208
  file_name = "feedback.json"
209
+
210
  try:
211
  try:
212
  file_path = hf_hub_download(repo_id=repo_id, filename=file_name, token=token)
 
217
  except Exception as e:
218
  print(f"Erreur lors du téléchargement du fichier : {str(e)}")
219
  current_feedback = []
220
+
221
+ current_feedback.append(feedback_data)
222
  updated_content = json.dumps(current_feedback, ensure_ascii=False, indent=2)
223
+
224
  temp_file_path = "/tmp/feedback.json"
225
  with open(temp_file_path, "w", encoding="utf-8") as temp_file:
226
  temp_file.write(updated_content)
227
+
228
  api.upload_file(
229
  path_or_fileobj=temp_file_path,
230
  path_in_repo=file_name,
 
232
  token=token
233
  )
234
  print(f"Feedback enregistré dans {repo_id}/{file_name}")
235
+ return "Merci pour votre feedback !"
236
  except Exception as e:
237
  print(f"Erreur lors de l'enregistrement du feedback : {str(e)}")
238
+ return "Erreur lors de l'enregistrement du feedback."
239
+
240
 
241
 
242
  def load_feedback_data():
 
293
  text_input = gr.Textbox(lines=1, label="Votre message")
294
  text_input.submit(interact_with_lucas, [text_input, chatbot], [chatbot])
295
 
296
+ with gr.Row():
297
+ like_btn = gr.Button("👍")
298
+ dislike_btn = gr.Button("👎")
299
+
300
+ feedback_text = gr.Textbox(label="Feedback")
301
+
302
+ like_btn.click(lambda: vote("👍", chatbot.value), outputs=feedback_text)
303
+ dislike_btn.click(lambda: vote("👎", chatbot.value), outputs=feedback_text)
304
 
305
  with gr.Tab("Statistiques"):
306
  gr.Markdown("# Statistiques d'utilisation 📊")
 
318
 
319
 
320