add session id
Browse files
app.py
CHANGED
@@ -17,64 +17,62 @@ with open("system_prompt.txt", "r") as f:
|
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
-
# ---- Chatbot
|
21 |
-
|
22 |
-
|
23 |
-
session_id = str(uuid.uuid4())
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
yield response, session_id # Yield response and session ID state
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
"timestamp": datetime.now().isoformat(),
|
55 |
-
"user": message,
|
56 |
-
"assistant": response,
|
57 |
-
"system_prompt": SYSTEM_PROMPT,
|
58 |
-
"session_id": session_id
|
59 |
-
}
|
60 |
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
-
path_or_fileobj=local_log_path,
|
66 |
-
path_in_repo=remote_log_path,
|
67 |
-
repo_id=DATASET_REPO,
|
68 |
-
repo_type="dataset",
|
69 |
-
token=HF_TOKEN
|
70 |
-
)
|
71 |
-
|
72 |
-
# ---- Gradio Interface with session state ----
|
73 |
-
demo = gr.ChatInterface(
|
74 |
-
fn=respond,
|
75 |
-
additional_inputs=[gr.State(None)],
|
76 |
-
title="BoundrAI",
|
77 |
-
)
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
demo.launch()
|
|
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
+
# ---- Chatbot Class with per-user session ID ----
|
21 |
+
class SessionChatBot:
|
22 |
+
def __init__(self):
|
23 |
+
self.session_id = str(uuid.uuid4())
|
24 |
+
self.today_date = datetime.now().strftime("%Y-%m-%d")
|
25 |
+
self.local_log_path = f"chatlog_{self.today_date}_{self.session_id}.jsonl"
|
26 |
+
self.remote_log_path = f"sessions/{self.today_date}/{self.session_id}.jsonl"
|
27 |
|
28 |
+
def append_to_session_log(self, user_message, assistant_message):
|
29 |
+
row = {
|
30 |
+
"timestamp": datetime.now().isoformat(),
|
31 |
+
"user": user_message,
|
32 |
+
"assistant": assistant_message,
|
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}]
|
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,
|
59 |
+
stream=True,
|
60 |
+
temperature=0.7,
|
61 |
+
top_p=0.95,
|
62 |
+
):
|
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 |
+
# ---- 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()
|