emeses commited on
Commit
13d2f6c
·
1 Parent(s): f7a1b1d

Update space

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -12,10 +12,10 @@ 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
  messages.append({"role": "user", "content": message})
20
 
21
  response = ""
@@ -26,9 +26,9 @@ def respond(message, history, system_message):
26
  temperature=0.7,
27
  top_p=0.9,
28
  ):
29
- token = message.choices[0].delta.content
30
- response += token
31
- yield response
32
 
33
  def extract_table(url):
34
  global data
@@ -76,21 +76,17 @@ def extract_table(url):
76
  except Exception as e:
77
  return f"<p>Error: {str(e)}</p>"
78
 
79
- def add_text(history, text):
80
- history = history + [(text, None)]
81
  return history
82
 
83
  def bot(history, system_message):
84
- if not history:
85
- return history
86
-
87
- user_message = history[-1][0]
88
  response = ""
89
-
90
- for chunk in respond(user_message, history[:-1], system_message):
91
  response = chunk
92
- history[-1] = (user_message, response)
93
- yield history
94
 
95
  def handle_prepare(index, history):
96
  try:
@@ -98,9 +94,10 @@ def handle_prepare(index, history):
98
  if 0 <= index < len(data):
99
  topic = data[index]["Topic"]
100
  message = f"Prepare a 10-minute reading for the topic: {topic}"
101
- return add_text(history or [], message)
 
102
  except:
103
- return history or []
104
 
105
  def clear_chat():
106
  return [], ""
@@ -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"
@@ -141,11 +138,11 @@ with gr.Blocks() as demo:
141
 
142
  # Submit button handler
143
  submit.click(
144
- fn=add_text,
145
- inputs=[chatbot, msg],
146
  outputs=chatbot,
147
  ).then(
148
- fn=lambda: "",
149
  outputs=msg,
150
  ).then(
151
  fn=bot,
 
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
+
19
  messages.append({"role": "user", "content": message})
20
 
21
  response = ""
 
26
  temperature=0.7,
27
  top_p=0.9,
28
  ):
29
+ if message.choices[0].delta.content is not None:
30
+ response += message.choices[0].delta.content
31
+ yield response
32
 
33
  def extract_table(url):
34
  global data
 
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:
 
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
 
102
  def clear_chat():
103
  return [], ""
 
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"
 
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,