frimelle HF Staff commited on
Commit
87f27fd
·
1 Parent(s): ac4f05a

add session id

Browse files
Files changed (1) hide show
  1. app.py +50 -52
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 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:
33
- messages.append({"role": "user", "content": user_msg})
34
- if bot_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,
42
- max_tokens=512,
43
- stream=True,
44
- temperature=0.7,
45
- top_p=0.95,
46
- ):
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
 
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()