debug
Browse files
app.py
CHANGED
@@ -15,6 +15,7 @@ with open("system_prompt.txt", "r") as f:
|
|
15 |
SYSTEM_PROMPT = f.read()
|
16 |
|
17 |
client = InferenceClient(MODEL_NAME)
|
|
|
18 |
|
19 |
# ---- Chatbot Class with per-user session ID ----
|
20 |
class SessionChatBot:
|
@@ -32,23 +33,15 @@ class SessionChatBot:
|
|
32 |
"system_prompt": SYSTEM_PROMPT,
|
33 |
"session_id": self.session_id
|
34 |
}
|
35 |
-
|
36 |
-
# Write to local log
|
37 |
with open(self.local_log_path, "a", encoding="utf-8") as f:
|
38 |
f.write(json.dumps(row) + "\n")
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
repo_id=DATASET_REPO,
|
47 |
-
repo_type="dataset",
|
48 |
-
token=HF_TOKEN
|
49 |
-
)
|
50 |
-
except Exception as e:
|
51 |
-
print(f"[Upload error] {e}")
|
52 |
|
53 |
def respond(self, message, history):
|
54 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
@@ -72,38 +65,14 @@ class SessionChatBot:
|
|
72 |
response += token
|
73 |
yield response
|
74 |
|
75 |
-
# Save after
|
76 |
self.append_to_session_log(message, response)
|
77 |
|
78 |
-
# ----
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
chatbot_ui = gr.Chatbot()
|
83 |
-
msg_input = gr.Textbox(label="Message", placeholder="Say something...")
|
84 |
-
send_btn = gr.Button("Send")
|
85 |
-
|
86 |
-
# Initialize a new bot per user
|
87 |
-
def init_bot():
|
88 |
-
return SessionChatBot()
|
89 |
-
|
90 |
-
demo.load(init_bot, outputs=[chatbot_state])
|
91 |
-
|
92 |
-
def handle_message(message, history, bot: SessionChatBot):
|
93 |
-
return bot.respond(message, history)
|
94 |
-
|
95 |
-
send_btn.click(
|
96 |
-
handle_message,
|
97 |
-
inputs=[msg_input, chatbot_ui, chatbot_state],
|
98 |
-
outputs=[chatbot_ui]
|
99 |
-
)
|
100 |
|
101 |
-
|
102 |
-
handle_message,
|
103 |
-
inputs=[msg_input, chatbot_ui, chatbot_state],
|
104 |
-
outputs=[chatbot_ui]
|
105 |
-
)
|
106 |
|
107 |
-
# Launch app
|
108 |
if __name__ == "__main__":
|
109 |
demo.launch()
|
|
|
15 |
SYSTEM_PROMPT = f.read()
|
16 |
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
+
api = HfApi()
|
19 |
|
20 |
# ---- Chatbot Class with per-user session ID ----
|
21 |
class SessionChatBot:
|
|
|
33 |
"system_prompt": SYSTEM_PROMPT,
|
34 |
"session_id": self.session_id
|
35 |
}
|
|
|
|
|
36 |
with open(self.local_log_path, "a", encoding="utf-8") as f:
|
37 |
f.write(json.dumps(row) + "\n")
|
38 |
+
api.upload_file(
|
39 |
+
path_or_fileobj=self.local_log_path,
|
40 |
+
path_in_repo=self.remote_log_path,
|
41 |
+
repo_id=DATASET_REPO,
|
42 |
+
repo_type="dataset",
|
43 |
+
token=HF_TOKEN
|
44 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def respond(self, message, history):
|
47 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
65 |
response += token
|
66 |
yield response
|
67 |
|
68 |
+
# Save log after full response
|
69 |
self.append_to_session_log(message, response)
|
70 |
|
71 |
+
# ---- Instantiate a new bot for each user session ----
|
72 |
+
def create_chatbot():
|
73 |
+
return SessionChatBot().respond
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
demo = gr.ChatInterface(fn=create_chatbot(), title="BoundrAI")
|
|
|
|
|
|
|
|
|
76 |
|
|
|
77 |
if __name__ == "__main__":
|
78 |
demo.launch()
|