easier session id logic
Browse files
app.py
CHANGED
@@ -5,82 +5,83 @@ import uuid
|
|
5 |
import os
|
6 |
import json
|
7 |
|
8 |
-
# ----
|
9 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
10 |
DATASET_REPO = "frimelle/companion-chat-logs"
|
11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
|
13 |
-
# ----
|
14 |
with open("system_prompt.txt", "r") as f:
|
15 |
SYSTEM_PROMPT = f.read()
|
16 |
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
-
# ----
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"timestamp": datetime.now().isoformat(),
|
24 |
"user": user_message,
|
25 |
"assistant": assistant_message,
|
26 |
"system_prompt": SYSTEM_PROMPT,
|
27 |
-
"session_id":
|
28 |
}
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
remote_path = f"sessions/{session_id}.jsonl"
|
32 |
-
|
33 |
-
# Append to local file
|
34 |
-
with open(local_path, "a", encoding="utf-8") as f:
|
35 |
-
f.write(json.dumps(log_entry) + "\n")
|
36 |
-
|
37 |
-
# Push updated file to the dataset
|
38 |
api.upload_file(
|
39 |
-
path_or_fileobj=
|
40 |
-
path_in_repo=
|
41 |
repo_id=DATASET_REPO,
|
42 |
repo_type="dataset",
|
43 |
-
token=HF_TOKEN
|
44 |
)
|
45 |
|
46 |
-
# ---- Chatbot
|
47 |
-
def respond(message, history
|
48 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
49 |
for user_msg, bot_msg in history:
|
50 |
if user_msg:
|
51 |
messages.append({"role": "user", "content": user_msg})
|
52 |
if bot_msg:
|
53 |
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
54 |
messages.append({"role": "user", "content": message})
|
55 |
-
|
56 |
response = ""
|
|
|
57 |
for chunk in client.chat_completion(
|
58 |
messages,
|
59 |
max_tokens=512,
|
|
|
60 |
temperature=0.7,
|
61 |
top_p=0.95,
|
62 |
-
stream=True,
|
63 |
):
|
64 |
token = chunk.choices[0].delta.content
|
65 |
if token:
|
66 |
response += token
|
67 |
yield response
|
68 |
|
69 |
-
#
|
70 |
-
append_to_session_log(message, response
|
71 |
-
|
72 |
-
# ---- Gradio App ----
|
73 |
-
with gr.Blocks() as demo:
|
74 |
-
session_id = gr.State(str(uuid.uuid4())) # Unique ID per user
|
75 |
-
|
76 |
-
chatbot = gr.Chatbot()
|
77 |
-
msg = gr.Textbox(placeholder="Ask me anything...")
|
78 |
-
clear = gr.Button("Clear chat")
|
79 |
-
|
80 |
-
def user_submit(message, chat_history, session_id):
|
81 |
-
return respond(message, chat_history, session_id), session_id
|
82 |
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
85 |
|
86 |
-
|
|
|
|
5 |
import os
|
6 |
import json
|
7 |
|
8 |
+
# ---- Configuration ----
|
9 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
10 |
DATASET_REPO = "frimelle/companion-chat-logs"
|
11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
|
13 |
+
# ---- Load system prompt ----
|
14 |
with open("system_prompt.txt", "r") as f:
|
15 |
SYSTEM_PROMPT = f.read()
|
16 |
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
+
# ---- Session ID and local file setup ----
|
21 |
+
SESSION_ID = 0
|
22 |
+
LOCAL_LOG_PATH = f"chatlog_{SESSION_ID}.jsonl"
|
23 |
+
REMOTE_LOG_PATH = f"sessions/{SESSION_ID}.jsonl" # stored in a folder in the dataset
|
24 |
+
|
25 |
+
def increment_session_id():
|
26 |
+
global SESSION_ID
|
27 |
+
SESSION_ID += 1
|
28 |
+
return SESSION_ID
|
29 |
+
|
30 |
+
# ---- Logging per session ----
|
31 |
+
def append_to_session_log(user_message, assistant_message):
|
32 |
+
row = {
|
33 |
"timestamp": datetime.now().isoformat(),
|
34 |
"user": user_message,
|
35 |
"assistant": assistant_message,
|
36 |
"system_prompt": SYSTEM_PROMPT,
|
37 |
+
"session_id": SESSION_ID
|
38 |
}
|
39 |
+
with open(LOCAL_LOG_PATH, "a", encoding="utf-8") as f:
|
40 |
+
f.write(json.dumps(row) + "\n")
|
41 |
|
42 |
+
# Push to HF dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
api.upload_file(
|
44 |
+
path_or_fileobj=LOCAL_LOG_PATH,
|
45 |
+
path_in_repo=REMOTE_LOG_PATH,
|
46 |
repo_id=DATASET_REPO,
|
47 |
repo_type="dataset",
|
48 |
+
token=HF_TOKEN
|
49 |
)
|
50 |
|
51 |
+
# ---- Chatbot function ----
|
52 |
+
def respond(message, history):
|
53 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
54 |
+
|
55 |
for user_msg, bot_msg in history:
|
56 |
if user_msg:
|
57 |
messages.append({"role": "user", "content": user_msg})
|
58 |
if bot_msg:
|
59 |
messages.append({"role": "assistant", "content": bot_msg})
|
60 |
+
|
61 |
messages.append({"role": "user", "content": message})
|
|
|
62 |
response = ""
|
63 |
+
|
64 |
for chunk in client.chat_completion(
|
65 |
messages,
|
66 |
max_tokens=512,
|
67 |
+
stream=True,
|
68 |
temperature=0.7,
|
69 |
top_p=0.95,
|
|
|
70 |
):
|
71 |
token = chunk.choices[0].delta.content
|
72 |
if token:
|
73 |
response += token
|
74 |
yield response
|
75 |
|
76 |
+
# Save after each message pair
|
77 |
+
append_to_session_log(message, response)
|
78 |
+
increment_session_id()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# ---- Gradio Interface ----
|
81 |
+
demo = gr.ChatInterface(
|
82 |
+
respond,
|
83 |
+
title="BoundrAI",
|
84 |
+
)
|
85 |
|
86 |
+
if __name__ == "__main__":
|
87 |
+
demo.launch()
|