frimelle HF Staff commited on
Commit
96c64db
·
1 Parent(s): c51afbd
Files changed (1) hide show
  1. app.py +30 -23
app.py CHANGED
@@ -4,8 +4,6 @@ from datetime import datetime
4
  import uuid
5
  import os
6
  import json
7
- from huggingface_hub import HfApi
8
-
9
 
10
  # ---- Configuration ----
11
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
@@ -17,7 +15,6 @@ with open("system_prompt.txt", "r") as f:
17
  SYSTEM_PROMPT = f.read()
18
 
19
  client = InferenceClient(MODEL_NAME)
20
- api = HfApi()
21
 
22
  # ---- Chatbot Class with per-user session ID ----
23
  class SessionChatBot:
@@ -35,15 +32,23 @@ class SessionChatBot:
35
  "system_prompt": SYSTEM_PROMPT,
36
  "session_id": self.session_id
37
  }
 
 
38
  with open(self.local_log_path, "a", encoding="utf-8") as f:
39
  f.write(json.dumps(row) + "\n")
40
- api.upload_file(
41
- path_or_fileobj=self.local_log_path,
42
- path_in_repo=self.remote_log_path,
43
- repo_id=DATASET_REPO,
44
- repo_type="dataset",
45
- token=HF_TOKEN
46
- )
 
 
 
 
 
 
47
 
48
  def respond(self, message, history):
49
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
@@ -67,36 +72,38 @@ class SessionChatBot:
67
  response += token
68
  yield response
69
 
70
- # Save log after full response
71
  self.append_to_session_log(message, response)
72
 
73
- # ---- Gradio Interface with per-user session support ----
74
  with gr.Blocks() as demo:
75
- chatbot_state = gr.State() # each user gets a different SessionChatBot
76
 
77
- chat = gr.Chatbot()
78
- msg = gr.Textbox(label="Message", placeholder="Say something...")
79
  send_btn = gr.Button("Send")
80
 
81
- # Create a chatbot instance per user when interface loads
82
  def init_bot():
83
  return SessionChatBot()
84
 
85
- demo.load(init_bot, outputs=chatbot_state)
86
 
87
  def handle_message(message, history, bot: SessionChatBot):
88
  return bot.respond(message, history)
89
 
90
  send_btn.click(
91
  handle_message,
92
- inputs=[msg, chat, chatbot_state],
93
- outputs=chat
94
  )
95
 
96
- msg.submit(
97
  handle_message,
98
- inputs=[msg, chat, chatbot_state],
99
- outputs=chat
100
  )
101
 
102
- demo.launch()
 
 
 
4
  import uuid
5
  import os
6
  import json
 
 
7
 
8
  # ---- Configuration ----
9
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
 
15
  SYSTEM_PROMPT = f.read()
16
 
17
  client = InferenceClient(MODEL_NAME)
 
18
 
19
  # ---- Chatbot Class with per-user session ID ----
20
  class SessionChatBot:
 
32
  "system_prompt": SYSTEM_PROMPT,
33
  "session_id": self.session_id
34
  }
35
+
36
+ # Write to local log
37
  with open(self.local_log_path, "a", encoding="utf-8") as f:
38
  f.write(json.dumps(row) + "\n")
39
+
40
+ # Upload to Hugging Face dataset
41
+ try:
42
+ api = HfApi() # Reinitialize inside callback context (important for Spaces)
43
+ api.upload_file(
44
+ path_or_fileobj=self.local_log_path,
45
+ path_in_repo=self.remote_log_path,
46
+ repo_id=DATASET_REPO,
47
+ repo_type="dataset",
48
+ token=HF_TOKEN
49
+ )
50
+ except Exception as e:
51
+ print(f"[Upload error] {e}")
52
 
53
  def respond(self, message, history):
54
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
72
  response += token
73
  yield response
74
 
75
+ # Save after generation completes
76
  self.append_to_session_log(message, response)
77
 
78
+ # ---- Gradio Interface ----
79
  with gr.Blocks() as demo:
80
+ chatbot_state = gr.State()
81
 
82
+ chatbot_ui = gr.Chatbot()
83
+ msg_input = gr.Textbox(label="Message", placeholder="Say something...")
84
  send_btn = gr.Button("Send")
85
 
86
+ # Initialize a new bot per user
87
  def init_bot():
88
  return SessionChatBot()
89
 
90
+ demo.load(init_bot, outputs=[chatbot_state])
91
 
92
  def handle_message(message, history, bot: SessionChatBot):
93
  return bot.respond(message, history)
94
 
95
  send_btn.click(
96
  handle_message,
97
+ inputs=[msg_input, chatbot_ui, chatbot_state],
98
+ outputs=[chatbot_ui]
99
  )
100
 
101
+ msg_input.submit(
102
  handle_message,
103
+ inputs=[msg_input, chatbot_ui, chatbot_state],
104
+ outputs=[chatbot_ui]
105
  )
106
 
107
+ # Launch app
108
+ if __name__ == "__main__":
109
+ demo.launch()