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

Update space

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -9,7 +9,7 @@ client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
9
  # Global data store for the table
10
  data = []
11
 
12
- def respond(message, history, system_message, max_tokens=2048, temperature=0.7, top_p=0.9):
13
  messages = [{"role": "system", "content": system_message}]
14
  for val in history:
15
  if val[0]:
@@ -17,13 +17,14 @@ def respond(message, history, system_message, max_tokens=2048, temperature=0.7,
17
  if val[1]:
18
  messages.append({"role": "assistant", "content": val[1]})
19
  messages.append({"role": "user", "content": message})
 
20
  response = ""
21
  for message in client.chat_completion(
22
  messages,
23
- max_tokens=max_tokens,
24
  stream=True,
25
- temperature=temperature,
26
- top_p=top_p,
27
  ):
28
  token = message.choices[0].delta.content
29
  response += token
@@ -75,10 +76,21 @@ def extract_table(url):
75
  except Exception as e:
76
  return f"<p>Error: {str(e)}</p>"
77
 
78
- def user_message(message, history):
79
- history = history or []
80
- history.append((message, None))
81
- return "", history
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  def handle_prepare(index, history):
84
  try:
@@ -86,9 +98,7 @@ def handle_prepare(index, history):
86
  if 0 <= index < len(data):
87
  topic = data[index]["Topic"]
88
  message = f"Prepare a 10-minute reading for the topic: {topic}"
89
- history = history or []
90
- history.append((message, None))
91
- return history
92
  except:
93
  return history or []
94
 
@@ -131,24 +141,27 @@ with gr.Blocks() as demo:
131
 
132
  # Submit button handler
133
  submit.click(
134
- fn=user_message,
135
- inputs=[msg, chatbot],
136
- outputs=[msg, chatbot]
 
 
 
137
  ).then(
138
- fn=respond,
139
- inputs=[msg, chatbot, system_message],
140
- outputs=[chatbot]
141
  )
142
 
143
  # Prepare button handler
144
  prepare_button.click(
145
  fn=handle_prepare,
146
  inputs=[prepare_topic, chatbot],
147
- outputs=[chatbot]
148
  ).then(
149
- fn=respond,
150
- inputs=[prepare_topic, chatbot, system_message],
151
- outputs=[chatbot]
152
  )
153
 
154
  # Clear button handler
 
9
  # Global data store for the table
10
  data = []
11
 
12
+ def respond(message, history, system_message):
13
  messages = [{"role": "system", "content": system_message}]
14
  for val in history:
15
  if val[0]:
 
17
  if val[1]:
18
  messages.append({"role": "assistant", "content": val[1]})
19
  messages.append({"role": "user", "content": message})
20
+
21
  response = ""
22
  for message in client.chat_completion(
23
  messages,
24
+ max_tokens=2048,
25
  stream=True,
26
+ temperature=0.7,
27
+ top_p=0.9,
28
  ):
29
  token = message.choices[0].delta.content
30
  response += token
 
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
  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
 
 
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,
152
+ inputs=[chatbot, system_message],
153
+ outputs=chatbot,
154
  )
155
 
156
  # Prepare button handler
157
  prepare_button.click(
158
  fn=handle_prepare,
159
  inputs=[prepare_topic, chatbot],
160
+ outputs=chatbot,
161
  ).then(
162
+ fn=bot,
163
+ inputs=[chatbot, system_message],
164
+ outputs=chatbot,
165
  )
166
 
167
  # Clear button handler