back to simple session ID
Browse files
app.py
CHANGED
|
@@ -45,12 +45,14 @@ class SessionChatBot:
|
|
| 45 |
|
| 46 |
def respond(self, message, history):
|
| 47 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 48 |
-
for
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
messages.append({"role": "user", "content": message})
|
| 51 |
|
| 52 |
response = ""
|
| 53 |
-
# Yield a full message dict each time (not just a string)
|
| 54 |
for chunk in client.chat_completion(
|
| 55 |
messages,
|
| 56 |
max_tokens=512,
|
|
@@ -61,9 +63,9 @@ class SessionChatBot:
|
|
| 61 |
token = chunk.choices[0].delta.content
|
| 62 |
if token:
|
| 63 |
response += token
|
| 64 |
-
yield
|
| 65 |
|
| 66 |
-
# Save full response
|
| 67 |
self.append_to_session_log(message, response)
|
| 68 |
|
| 69 |
def report_interaction(self):
|
|
@@ -76,13 +78,16 @@ class SessionChatBot:
|
|
| 76 |
if not lines:
|
| 77 |
return "No conversation to report."
|
| 78 |
|
|
|
|
| 79 |
last_entry = json.loads(lines[-1])
|
| 80 |
last_entry["reported"] = True
|
| 81 |
lines[-1] = json.dumps(last_entry) + "\n"
|
| 82 |
|
|
|
|
| 83 |
with open(self.local_log_path, "w", encoding="utf-8") as f:
|
| 84 |
f.writelines(lines)
|
| 85 |
|
|
|
|
| 86 |
api.upload_file(
|
| 87 |
path_or_fileobj=self.local_log_path,
|
| 88 |
path_in_repo=self.remote_log_path,
|
|
@@ -92,28 +97,22 @@ class SessionChatBot:
|
|
| 92 |
)
|
| 93 |
return "Interaction reported successfully."
|
| 94 |
|
| 95 |
-
# ----
|
| 96 |
-
|
| 97 |
-
chatbot_state = gr.State()
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
title="BoundrAI",
|
| 102 |
-
textbox=gr.Textbox(placeholder="Ask me anything...", container=True),
|
| 103 |
-
additional_inputs=[chatbot_state],
|
| 104 |
-
)
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
report_btn = gr.Button("Report Companion Interaction")
|
| 107 |
status_box = gr.Textbox(label="Report Status", interactive=False)
|
| 108 |
|
| 109 |
-
def
|
| 110 |
-
return
|
| 111 |
-
|
| 112 |
-
def report(chatbot):
|
| 113 |
-
return chatbot.report_interaction()
|
| 114 |
|
| 115 |
-
|
| 116 |
-
report_btn.click(fn=report, inputs=[chatbot_state], outputs=status_box)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.launch()
|
|
|
|
| 45 |
|
| 46 |
def respond(self, message, history):
|
| 47 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 48 |
+
for user_msg, bot_msg in history:
|
| 49 |
+
if user_msg:
|
| 50 |
+
messages.append({"role": "user", "content": user_msg})
|
| 51 |
+
if bot_msg:
|
| 52 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
response = ""
|
|
|
|
| 56 |
for chunk in client.chat_completion(
|
| 57 |
messages,
|
| 58 |
max_tokens=512,
|
|
|
|
| 63 |
token = chunk.choices[0].delta.content
|
| 64 |
if token:
|
| 65 |
response += token
|
| 66 |
+
yield response
|
| 67 |
|
| 68 |
+
# Save log after full response
|
| 69 |
self.append_to_session_log(message, response)
|
| 70 |
|
| 71 |
def report_interaction(self):
|
|
|
|
| 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,
|
|
|
|
| 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()
|