Update app.py
Browse files
app.py
CHANGED
|
@@ -187,38 +187,34 @@ def interact_with_lucas(prompt, messages):
|
|
| 187 |
for response in generate(prompt, chat_history, system_prompt):
|
| 188 |
messages[-1] = ChatMessage(role="assistant", content=response)
|
| 189 |
yield messages
|
| 190 |
-
|
| 191 |
-
def vote(
|
| 192 |
-
|
|
|
|
| 193 |
"timestamp": datetime.now().isoformat(),
|
| 194 |
-
"user_input":
|
| 195 |
-
"bot_response":
|
| 196 |
-
"liked":
|
| 197 |
}
|
| 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 |
-
|
| 204 |
try:
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
current_feedback = []
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
updated_content = json.dumps(current_feedback, ensure_ascii=False, indent=2)
|
| 216 |
-
|
| 217 |
-
temp_file_path = "/tmp/feedback.json"
|
| 218 |
-
with open(temp_file_path, "w", encoding="utf-8") as temp_file:
|
| 219 |
-
temp_file.write(updated_content)
|
| 220 |
-
|
| 221 |
-
try:
|
| 222 |
api.upload_file(
|
| 223 |
path_or_fileobj=temp_file_path,
|
| 224 |
path_in_repo=file_name,
|
|
@@ -228,8 +224,7 @@ def vote(feedback: str, message: str, response: str):
|
|
| 228 |
print(f"Feedback enregistré dans {repo_id}/{file_name}")
|
| 229 |
except Exception as e:
|
| 230 |
print(f"Erreur lors de l'enregistrement du feedback : {str(e)}")
|
| 231 |
-
|
| 232 |
-
return "Merci pour votre feedback !"
|
| 233 |
|
| 234 |
def load_feedback_data():
|
| 235 |
try:
|
|
@@ -285,21 +280,8 @@ with gr.Blocks() as demo:
|
|
| 285 |
text_input = gr.Textbox(lines=1, label="Votre message")
|
| 286 |
text_input.submit(interact_with_lucas, [text_input, chatbot], [chatbot])
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
dislike_btn = gr.Button("👎")
|
| 291 |
-
|
| 292 |
-
feedback_text = gr.Textbox(label="Feedback")
|
| 293 |
-
|
| 294 |
-
def vote_callback(feedback, history):
|
| 295 |
-
if history:
|
| 296 |
-
last_user_message = history[-1][0]
|
| 297 |
-
last_bot_response = history[-1][1]
|
| 298 |
-
return vote(feedback, last_user_message, last_bot_response)
|
| 299 |
-
return "Aucun message à évaluer."
|
| 300 |
-
|
| 301 |
-
like_btn.click(lambda: vote_callback("👍", chatbot.value), outputs=feedback_text)
|
| 302 |
-
dislike_btn.click(lambda: vote_callback("👎", chatbot.value), outputs=feedback_text)
|
| 303 |
|
| 304 |
with gr.Tab("Statistiques"):
|
| 305 |
gr.Markdown("# Statistiques d'utilisation 📊")
|
|
|
|
| 187 |
for response in generate(prompt, chat_history, system_prompt):
|
| 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)
|
| 206 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
| 207 |
+
current_feedback = json.load(file)
|
| 208 |
+
if not isinstance(current_feedback, list):
|
| 209 |
+
current_feedback = []
|
| 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,
|
|
|
|
| 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():
|
| 230 |
try:
|
|
|
|
| 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 📊")
|