add session id
Browse files
app.py
CHANGED
@@ -17,35 +17,16 @@ with open("system_prompt.txt", "r") as f:
|
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
-
|
21 |
-
def append_to_session_log(session_id, user_message, assistant_message):
|
22 |
-
today = datetime.now().strftime("%Y-%m-%d")
|
23 |
-
local_path = f"chatlog_{today}_{session_id}.jsonl"
|
24 |
-
remote_path = f"sessions/{today}/{session_id}.jsonl"
|
25 |
-
|
26 |
-
row = {
|
27 |
-
"timestamp": datetime.now().isoformat(),
|
28 |
-
"user": user_message,
|
29 |
-
"assistant": assistant_message,
|
30 |
-
"system_prompt": SYSTEM_PROMPT,
|
31 |
-
"session_id": session_id,
|
32 |
-
}
|
33 |
-
with open(local_path, "a", encoding="utf-8") as f:
|
34 |
-
f.write(json.dumps(row) + "\n")
|
35 |
-
|
36 |
-
api.upload_file(
|
37 |
-
path_or_fileobj=local_path,
|
38 |
-
path_in_repo=remote_path,
|
39 |
-
repo_id=DATASET_REPO,
|
40 |
-
repo_type="dataset",
|
41 |
-
token=HF_TOKEN
|
42 |
-
)
|
43 |
-
|
44 |
-
|
45 |
def respond(message, history, session_id):
|
46 |
if session_id is None:
|
47 |
-
session_id = str(uuid.uuid4()) #
|
|
|
|
|
|
|
|
|
48 |
|
|
|
49 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
50 |
for user_msg, bot_msg in history:
|
51 |
if user_msg:
|
@@ -54,6 +35,7 @@ def respond(message, history, session_id):
|
|
54 |
messages.append({"role": "assistant", "content": bot_msg})
|
55 |
messages.append({"role": "user", "content": message})
|
56 |
|
|
|
57 |
response = ""
|
58 |
for chunk in client.chat_completion(
|
59 |
messages,
|
@@ -65,15 +47,32 @@ def respond(message, history, session_id):
|
|
65 |
token = chunk.choices[0].delta.content
|
66 |
if token:
|
67 |
response += token
|
68 |
-
yield response, session_id #
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
# ---- Gradio
|
74 |
demo = gr.ChatInterface(
|
75 |
fn=respond,
|
76 |
-
additional_inputs=[gr.State(
|
77 |
title="BoundrAI",
|
78 |
)
|
79 |
|
|
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
+
# ---- Chatbot function with per-user session ID ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def respond(message, history, session_id):
|
22 |
if session_id is None:
|
23 |
+
session_id = str(uuid.uuid4()) # Generate a new unique ID
|
24 |
+
|
25 |
+
today_date = datetime.now().strftime("%Y-%m-%d")
|
26 |
+
local_log_path = f"chatlog_{today_date}_{session_id}.jsonl"
|
27 |
+
remote_log_path = f"sessions/{today_date}/{session_id}.jsonl"
|
28 |
|
29 |
+
# Construct conversation history
|
30 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
31 |
for user_msg, bot_msg in history:
|
32 |
if user_msg:
|
|
|
35 |
messages.append({"role": "assistant", "content": bot_msg})
|
36 |
messages.append({"role": "user", "content": message})
|
37 |
|
38 |
+
# Generate response
|
39 |
response = ""
|
40 |
for chunk in client.chat_completion(
|
41 |
messages,
|
|
|
47 |
token = chunk.choices[0].delta.content
|
48 |
if token:
|
49 |
response += token
|
50 |
+
yield response, session_id # Yield response and session ID state
|
51 |
|
52 |
+
# ---- Logging ----
|
53 |
+
row = {
|
54 |
+
"timestamp": datetime.now().isoformat(),
|
55 |
+
"user": message,
|
56 |
+
"assistant": response,
|
57 |
+
"system_prompt": SYSTEM_PROMPT,
|
58 |
+
"session_id": session_id
|
59 |
+
}
|
60 |
|
61 |
+
with open(local_log_path, "a", encoding="utf-8") as f:
|
62 |
+
f.write(json.dumps(row) + "\n")
|
63 |
+
|
64 |
+
api.upload_file(
|
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 |
|