frimelle HF Staff commited on
Commit
e4ec781
·
1 Parent(s): 96c64db
Files changed (1) hide show
  1. app.py +13 -44
app.py CHANGED
@@ -15,6 +15,7 @@ with open("system_prompt.txt", "r") as f:
15
  SYSTEM_PROMPT = f.read()
16
 
17
  client = InferenceClient(MODEL_NAME)
 
18
 
19
  # ---- Chatbot Class with per-user session ID ----
20
  class SessionChatBot:
@@ -32,23 +33,15 @@ 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,38 +65,14 @@ class SessionChatBot:
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()
 
15
  SYSTEM_PROMPT = f.read()
16
 
17
  client = InferenceClient(MODEL_NAME)
18
+ api = HfApi()
19
 
20
  # ---- Chatbot Class with per-user session ID ----
21
  class SessionChatBot:
 
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}]
 
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()