ar12c commited on
Commit
2eecbee
·
1 Parent(s): 2c60fde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -134,13 +134,15 @@ SYSTEM_PROMPT = (
134
  STOP_STRINGS = ["\nUser:", "\nAssistant:"]
135
 
136
  def build_prompt(message, history):
137
- """Build prompt from message history (list of lists/tuples)."""
138
  prompt = SYSTEM_PROMPT
139
- for user_text, assistant_text in history:
140
- if user_text:
141
- prompt += f"User: {user_text}\n"
142
- if assistant_text:
143
- prompt += f"Assistant: {assistant_text}\n"
 
 
144
  prompt += f"User: {message}\nAssistant:"
145
  return prompt
146
 
@@ -178,30 +180,35 @@ def generate_stream(prompt, max_tokens=256):
178
  with gr.Blocks() as demo:
179
  gr.Markdown("# 🌠 OLM North Star")
180
 
181
- # Removed type="messages" to use the legacy/standard format
 
182
  chat = gr.Chatbot(height=550)
183
  box = gr.Textbox(placeholder="Say hi…", label="Input")
184
 
185
  def user_step(msg, hist):
186
  if hist is None:
187
  hist = []
188
- # Return empty input box and updated history with user message
189
- return "", hist + [[msg, None]]
 
190
 
191
  def bot_step(hist):
192
- if not hist or hist[-1][0] is None:
193
  yield hist
194
  return
195
 
196
- user_msg = hist[-1][0]
197
- context = hist[:-1] # Previous turns
198
 
199
  prompt = build_prompt(user_msg, context)
200
 
 
 
 
201
  bot_text = ""
202
  for chunk in generate_stream(prompt):
203
  bot_text = chunk
204
- hist[-1][1] = bot_text
205
  yield hist
206
 
207
  box.submit(user_step, [box, chat], [box, chat], queue=False).then(
 
134
  STOP_STRINGS = ["\nUser:", "\nAssistant:"]
135
 
136
  def build_prompt(message, history):
137
+ """Build prompt from message history (list of dictionaries)."""
138
  prompt = SYSTEM_PROMPT
139
+ for msg in history:
140
+ role = msg.get("role")
141
+ content = msg.get("content")
142
+ if role == "user":
143
+ prompt += f"User: {content}\n"
144
+ elif role == "assistant" and content:
145
+ prompt += f"Assistant: {content}\n"
146
  prompt += f"User: {message}\nAssistant:"
147
  return prompt
148
 
 
180
  with gr.Blocks() as demo:
181
  gr.Markdown("# 🌠 OLM North Star")
182
 
183
+ # We do not pass type="messages" to avoid the TypeError
184
+ # But we provide dictionary-based data to satisfy the internal postprocess check
185
  chat = gr.Chatbot(height=550)
186
  box = gr.Textbox(placeholder="Say hi…", label="Input")
187
 
188
  def user_step(msg, hist):
189
  if hist is None:
190
  hist = []
191
+ # Modern Gradio requires dictionaries for history even if 'type' isn't set
192
+ hist.append({"role": "user", "content": msg})
193
+ return "", hist
194
 
195
  def bot_step(hist):
196
+ if not hist or hist[-1]["role"] != "user":
197
  yield hist
198
  return
199
 
200
+ user_msg = hist[-1]["content"]
201
+ context = hist[:-1]
202
 
203
  prompt = build_prompt(user_msg, context)
204
 
205
+ # Append assistant response dictionary
206
+ hist.append({"role": "assistant", "content": ""})
207
+
208
  bot_text = ""
209
  for chunk in generate_stream(prompt):
210
  bot_text = chunk
211
+ hist[-1]["content"] = bot_text
212
  yield hist
213
 
214
  box.submit(user_step, [box, chat], [box, chat], queue=False).then(