emeses commited on
Commit
a19992d
·
1 Parent(s): 5ad043c

Update space

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -19,7 +19,7 @@ def respond(message, history, system_message):
19
 
20
  messages.append({"role": "user", "content": message})
21
 
22
- full_response = ""
23
  for message in client.chat_completion(
24
  messages,
25
  max_tokens=2048,
@@ -28,9 +28,8 @@ def respond(message, history, system_message):
28
  top_p=0.9,
29
  ):
30
  if message.choices[0].delta.content is not None:
31
- full_response += message.choices[0].delta.content
32
-
33
- return full_response
34
 
35
  def extract_table(url):
36
  global data
@@ -64,8 +63,8 @@ def extract_table(url):
64
  <td>{row['Topic']}</td>
65
  <td>
66
  <button onclick='
67
- document.getElementById("prepare-topic").value = "{i}";
68
- document.getElementById("prepare-topic").dispatchEvent(new Event("input"));
69
  '>
70
  Prepare
71
  </button>
@@ -85,32 +84,21 @@ def generate_response(history, system_message):
85
  if not history:
86
  return history
87
 
88
- # Ensure the last entry is a full user message and not partially processed
89
- if history[-1][1] is not None:
90
- return history
91
-
92
- response = respond(history[-1][0], history[:-1], system_message)
93
- history[-1] = (history[-1][0], response)
94
- return history
95
 
96
- def handle_prepare(index, history, system_message):
97
  try:
98
  index = int(index)
99
  if 0 <= index < len(data):
100
  topic = data[index]["Topic"]
101
  date = data[index]["Date"]
102
- message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
103
-
104
- # Generate response for the prepare request
105
- response = respond(message, history, system_message)
106
-
107
- # Add both the request and response to history
108
- new_history = history + [(message, response)]
109
-
110
- return new_history
111
- except Exception as e:
112
- print(f"Error in handle_prepare: {e}")
113
- return history
114
 
115
  def clear_chat():
116
  return [], ""
@@ -127,7 +115,8 @@ with gr.Blocks() as demo:
127
  extract_btn = gr.Button("Extract Table")
128
 
129
  # Hidden components for prepare functionality
130
- prepare_topic = gr.Textbox(value="", visible=False, elem_id="prepare-topic")
 
131
 
132
  with gr.Column(scale=2):
133
  chatbot = gr.Chatbot()
@@ -148,10 +137,24 @@ with gr.Blocks() as demo:
148
  outputs=[table_output]
149
  )
150
 
151
- # Prepare topic handler
152
- prepare_topic.change(
153
- fn=handle_prepare,
154
- inputs=[prepare_topic, chatbot, system_message],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  outputs=[chatbot]
156
  )
157
 
@@ -159,27 +162,27 @@ with gr.Blocks() as demo:
159
  msg.submit(
160
  fn=add_text,
161
  inputs=[chatbot, msg],
162
- outputs=chatbot
163
  ).success(
164
  fn=lambda: "",
165
- outputs=msg
166
  ).then(
167
  fn=generate_response,
168
  inputs=[chatbot, system_message],
169
- outputs=chatbot
170
  )
171
 
172
  submit.click(
173
  fn=add_text,
174
  inputs=[chatbot, msg],
175
- outputs=chatbot
176
  ).success(
177
  fn=lambda: "",
178
- outputs=msg
179
  ).then(
180
  fn=generate_response,
181
  inputs=[chatbot, system_message],
182
- outputs=chatbot
183
  )
184
 
185
  # Clear button handler
 
19
 
20
  messages.append({"role": "user", "content": message})
21
 
22
+ response = ""
23
  for message in client.chat_completion(
24
  messages,
25
  max_tokens=2048,
 
28
  top_p=0.9,
29
  ):
30
  if message.choices[0].delta.content is not None:
31
+ response += message.choices[0].delta.content
32
+ yield response
 
33
 
34
  def extract_table(url):
35
  global data
 
63
  <td>{row['Topic']}</td>
64
  <td>
65
  <button onclick='
66
+ document.getElementById("prepare-index").value = "{i}";
67
+ document.getElementById("prepare-trigger").click();
68
  '>
69
  Prepare
70
  </button>
 
84
  if not history:
85
  return history
86
 
87
+ response = ""
88
+ for chunk in respond(history[-1][0], history[:-1], system_message):
89
+ response = chunk
90
+ history[-1] = (history[-1][0], response)
91
+ yield history
 
 
92
 
93
+ def prepare_topic_message(index):
94
  try:
95
  index = int(index)
96
  if 0 <= index < len(data):
97
  topic = data[index]["Topic"]
98
  date = data[index]["Date"]
99
+ return f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
100
+ except:
101
+ return ""
 
 
 
 
 
 
 
 
 
102
 
103
  def clear_chat():
104
  return [], ""
 
115
  extract_btn = gr.Button("Extract Table")
116
 
117
  # Hidden components for prepare functionality
118
+ prepare_index = gr.Textbox(value="", visible=False, elem_id="prepare-index")
119
+ prepare_trigger = gr.Button("Prepare", visible=False, elem_id="prepare-trigger")
120
 
121
  with gr.Column(scale=2):
122
  chatbot = gr.Chatbot()
 
137
  outputs=[table_output]
138
  )
139
 
140
+ # Prepare trigger handler
141
+ prepare_trigger.click(
142
+ fn=prepare_topic_message,
143
+ inputs=[prepare_index],
144
+ outputs=[msg]
145
+ )
146
+
147
+ # Submit button handler for prepare trigger
148
+ prepare_trigger.click(
149
+ fn=add_text,
150
+ inputs=[chatbot, msg],
151
+ outputs=[chatbot]
152
+ ).success(
153
+ fn=lambda: "",
154
+ outputs=[msg]
155
+ ).then(
156
+ fn=generate_response,
157
+ inputs=[chatbot, system_message],
158
  outputs=[chatbot]
159
  )
160
 
 
162
  msg.submit(
163
  fn=add_text,
164
  inputs=[chatbot, msg],
165
+ outputs=[chatbot]
166
  ).success(
167
  fn=lambda: "",
168
+ outputs=[msg]
169
  ).then(
170
  fn=generate_response,
171
  inputs=[chatbot, system_message],
172
+ outputs=[chatbot]
173
  )
174
 
175
  submit.click(
176
  fn=add_text,
177
  inputs=[chatbot, msg],
178
+ outputs=[chatbot]
179
  ).success(
180
  fn=lambda: "",
181
+ outputs=[msg]
182
  ).then(
183
  fn=generate_response,
184
  inputs=[chatbot, system_message],
185
+ outputs=[chatbot]
186
  )
187
 
188
  # Clear button handler