emeses commited on
Commit
d6ea91d
·
1 Parent(s): 64f77f5

Update space

Browse files
Files changed (1) hide show
  1. app.py +22 -38
app.py CHANGED
@@ -12,8 +12,7 @@ data = []
12
  def respond(message, history, system_message):
13
  messages = [{"role": "system", "content": system_message}]
14
  for val in history:
15
- if val[0]:
16
- messages.append({"role": "user", "content": val[0]})
17
  if val[1]:
18
  messages.append({"role": "assistant", "content": val[1]})
19
 
@@ -77,28 +76,26 @@ def extract_table(url):
77
  except Exception as e:
78
  return f"<p>Error: {str(e)}</p>"
79
 
80
- def add_text(history, text):
81
- history = history + [(text, None)]
82
  return history
83
 
84
- def generate_response(history, system_message):
85
- if not history:
86
- return history
87
-
88
  response = ""
89
  for chunk in respond(history[-1][0], history[:-1], system_message):
90
  response = chunk
91
- history[-1] = (history[-1][0], response)
92
- yield history
93
 
94
  def handle_prepare(index, history):
95
  try:
96
  index = int(index)
97
  if 0 <= index < len(data):
98
  topic = data[index]["Topic"]
99
- date = data[index]["Date"]
100
- message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
101
- return history + [(message, None)]
102
  except:
103
  return history
104
 
@@ -122,7 +119,7 @@ with gr.Blocks() as demo:
122
 
123
  with gr.Column(scale=2):
124
  chatbot = gr.Chatbot()
125
- msg = gr.Textbox(label="Message")
126
  system_message = gr.Textbox(
127
  value="Student class preparation companion.",
128
  label="System message"
@@ -140,41 +137,28 @@ with gr.Blocks() as demo:
140
  )
141
 
142
  # Submit button handler
143
- msg.submit(
144
- fn=add_text,
145
- inputs=[chatbot, msg],
146
- outputs=chatbot
147
- ).success(
148
- fn=lambda: "",
149
- outputs=msg
150
- ).then(
151
- fn=generate_response,
152
- inputs=[chatbot, system_message],
153
- outputs=chatbot
154
- )
155
-
156
  submit.click(
157
- fn=add_text,
158
- inputs=[chatbot, msg],
159
- outputs=chatbot
160
- ).success(
161
- fn=lambda: "",
162
- outputs=msg
163
  ).then(
164
- fn=generate_response,
165
  inputs=[chatbot, system_message],
166
- outputs=chatbot
167
  )
168
 
169
  # Prepare button handler
170
  prepare_button.click(
171
  fn=handle_prepare,
172
  inputs=[prepare_topic, chatbot],
173
- outputs=chatbot
174
  ).then(
175
- fn=generate_response,
176
  inputs=[chatbot, system_message],
177
- outputs=chatbot
178
  )
179
 
180
  # Clear button handler
 
12
  def respond(message, history, system_message):
13
  messages = [{"role": "system", "content": system_message}]
14
  for val in history:
15
+ messages.append({"role": "user", "content": val[0]})
 
16
  if val[1]:
17
  messages.append({"role": "assistant", "content": val[1]})
18
 
 
76
  except Exception as e:
77
  return f"<p>Error: {str(e)}</p>"
78
 
79
+ def user_message(message, history):
80
+ history = history + [(message, None)]
81
  return history
82
 
83
+ def bot(history, system_message):
84
+ history_with_response = history.copy()
 
 
85
  response = ""
86
  for chunk in respond(history[-1][0], history[:-1], system_message):
87
  response = chunk
88
+ history_with_response[-1] = (history[-1][0], response)
89
+ yield history_with_response
90
 
91
  def handle_prepare(index, history):
92
  try:
93
  index = int(index)
94
  if 0 <= index < len(data):
95
  topic = data[index]["Topic"]
96
+ message = f"Prepare a 10-minute reading for the topic: {topic}"
97
+ history = history + [(message, None)]
98
+ return history
99
  except:
100
  return history
101
 
 
119
 
120
  with gr.Column(scale=2):
121
  chatbot = gr.Chatbot()
122
+ msg = gr.Textbox(label="Message", interactive=True)
123
  system_message = gr.Textbox(
124
  value="Student class preparation companion.",
125
  label="System message"
 
137
  )
138
 
139
  # Submit button handler
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  submit.click(
141
+ fn=user_message,
142
+ inputs=[msg, chatbot],
143
+ outputs=chatbot,
144
+ ).then(
145
+ fn=lambda: "", # Clear input textbox
146
+ outputs=msg,
147
  ).then(
148
+ fn=bot,
149
  inputs=[chatbot, system_message],
150
+ outputs=chatbot,
151
  )
152
 
153
  # Prepare button handler
154
  prepare_button.click(
155
  fn=handle_prepare,
156
  inputs=[prepare_topic, chatbot],
157
+ outputs=chatbot,
158
  ).then(
159
+ fn=bot,
160
  inputs=[chatbot, system_message],
161
+ outputs=chatbot,
162
  )
163
 
164
  # Clear button handler