add report button
Browse files
app.py
CHANGED
|
@@ -68,11 +68,51 @@ class SessionChatBot:
|
|
| 68 |
# Save log after full response
|
| 69 |
self.append_to_session_log(message, response)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def create_chatbot():
|
| 73 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
demo.launch()
|
|
|
|
| 68 |
# Save log after full response
|
| 69 |
self.append_to_session_log(message, response)
|
| 70 |
|
| 71 |
+
def report_interaction(self):
|
| 72 |
+
if not os.path.exists(self.local_log_path):
|
| 73 |
+
return "No session log found."
|
| 74 |
+
|
| 75 |
+
with open(self.local_log_path, "r", encoding="utf-8") as f:
|
| 76 |
+
lines = f.readlines()
|
| 77 |
+
|
| 78 |
+
if not lines:
|
| 79 |
+
return "No conversation to report."
|
| 80 |
+
|
| 81 |
+
# Mark last interaction as reported
|
| 82 |
+
last_entry = json.loads(lines[-1])
|
| 83 |
+
last_entry["reported"] = True
|
| 84 |
+
lines[-1] = json.dumps(last_entry) + "\n"
|
| 85 |
+
|
| 86 |
+
# Overwrite file
|
| 87 |
+
with open(self.local_log_path, "w", encoding="utf-8") as f:
|
| 88 |
+
f.writelines(lines)
|
| 89 |
+
|
| 90 |
+
# Upload updated log to Hugging Face
|
| 91 |
+
api.upload_file(
|
| 92 |
+
path_or_fileobj=self.local_log_path,
|
| 93 |
+
path_in_repo=self.remote_log_path,
|
| 94 |
+
repo_id=DATASET_REPO,
|
| 95 |
+
repo_type="dataset",
|
| 96 |
+
token=HF_TOKEN
|
| 97 |
+
)
|
| 98 |
+
return "Interaction reported successfully."
|
| 99 |
+
|
| 100 |
+
# ---- Instantiate and Share Bot Session Globally ----
|
| 101 |
+
chatbot_instance = SessionChatBot()
|
| 102 |
+
|
| 103 |
def create_chatbot():
|
| 104 |
+
return chatbot_instance.respond
|
| 105 |
+
|
| 106 |
+
# ---- Build Gradio Interface ----
|
| 107 |
+
with gr.Blocks() as demo:
|
| 108 |
+
chatbot = gr.ChatInterface(fn=create_chatbot(), title="BoundrAI")
|
| 109 |
+
report_btn = gr.Button("Report Companion Interaction")
|
| 110 |
+
status_box = gr.Textbox(label="Report Status", interactive=False)
|
| 111 |
+
|
| 112 |
+
def report():
|
| 113 |
+
return chatbot_instance.report_interaction()
|
| 114 |
|
| 115 |
+
report_btn.click(fn=report, outputs=status_box)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo.launch()
|