frimelle HF Staff commited on
Commit
ee29e60
·
1 Parent(s): 9bf5ae3

adjust session ID

Browse files
Files changed (1) hide show
  1. app.py +20 -27
app.py CHANGED
@@ -17,51 +17,44 @@ with open("system_prompt.txt", "r") as f:
17
  client = InferenceClient(MODEL_NAME)
18
  api = HfApi()
19
 
20
- # ---- Session ID and local file setup ----
21
- SESSION_ID = 0
22
- TODAY_DATE = datetime.now().strftime("%Y-%m-%d")
23
- LOCAL_LOG_PATH = f"chatlog_{TODAY_DATE}_{SESSION_ID}.jsonl"
24
- REMOTE_LOG_PATH = f"sessions/{TODAY_DATE}/{SESSION_ID}.jsonl" # stored in a folder in the dataset
25
 
26
- def increment_session_id():
27
- global SESSION_ID
28
- SESSION_ID += 1
29
- return SESSION_ID
30
 
31
- # ---- Logging per session ----
32
- def append_to_session_log(user_message, assistant_message):
33
  row = {
34
  "timestamp": datetime.now().isoformat(),
35
  "user": user_message,
36
  "assistant": assistant_message,
37
  "system_prompt": SYSTEM_PROMPT,
38
- "session_id": SESSION_ID
39
  }
40
- with open(LOCAL_LOG_PATH, "a", encoding="utf-8") as f:
41
  f.write(json.dumps(row) + "\n")
42
 
43
- # Push to HF dataset
44
  api.upload_file(
45
- path_or_fileobj=LOCAL_LOG_PATH,
46
- path_in_repo=REMOTE_LOG_PATH,
47
  repo_id=DATASET_REPO,
48
  repo_type="dataset",
49
  token=HF_TOKEN
50
  )
51
 
52
- # ---- Chatbot function ----
53
- def respond(message, history):
 
 
 
54
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
55
-
56
  for user_msg, bot_msg in history:
57
  if user_msg:
58
  messages.append({"role": "user", "content": user_msg})
59
  if bot_msg:
60
  messages.append({"role": "assistant", "content": bot_msg})
61
-
62
  messages.append({"role": "user", "content": message})
63
- response = ""
64
 
 
65
  for chunk in client.chat_completion(
66
  messages,
67
  max_tokens=512,
@@ -72,15 +65,15 @@ def respond(message, history):
72
  token = chunk.choices[0].delta.content
73
  if token:
74
  response += token
75
- yield response
 
 
76
 
77
- # Save after each message pair
78
- append_to_session_log(message, response)
79
- increment_session_id()
80
 
81
- # ---- Gradio Interface ----
82
  demo = gr.ChatInterface(
83
- respond,
 
84
  title="BoundrAI",
85
  )
86
 
 
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()) # unique per user session
48
+
49
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
50
  for user_msg, bot_msg in history:
51
  if user_msg:
52
  messages.append({"role": "user", "content": user_msg})
53
  if bot_msg:
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,
60
  max_tokens=512,
 
65
  token = chunk.choices[0].delta.content
66
  if token:
67
  response += token
68
+ yield response, session_id # return state too
69
+
70
+ append_to_session_log(session_id, message, response)
71
 
 
 
 
72
 
73
+ # ---- Gradio UI ----
74
  demo = gr.ChatInterface(
75
+ fn=respond,
76
+ additional_inputs=[gr.State(value=None)],
77
  title="BoundrAI",
78
  )
79