llaa33219 commited on
Commit
360a4ff
·
verified ·
1 Parent(s): 91aa250

Upload 3 files

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -237,12 +237,12 @@ with gr.Blocks(title="Context Window Extender - Chat") as demo:
237
  ):
238
  """Handle chat response with streaming."""
239
  if not message.strip():
240
- yield [(user_msg, "Please enter a message.") for user_msg, _ in history] + [(message, "Please enter a message.")]
241
  return
242
 
243
  # Add user message to history
244
- history.append((message, ""))
245
- yield history
246
 
247
  # Generate response
248
  try:
@@ -251,8 +251,10 @@ with gr.Blocks(title="Context Window Extender - Chat") as demo:
251
 
252
  # Build prompt from history
253
  prompt = message
254
- for user_msg, assistant_msg in history[:-1]:
255
- prompt = f"User: {user_msg}\nAssistant: {assistant_msg}\n" + prompt
 
 
256
 
257
  prompt = prompt + "\nAssistant:"
258
 
@@ -292,20 +294,18 @@ with gr.Blocks(title="Context Window Extender - Chat") as demo:
292
  for text in streamer:
293
  full_response += text
294
  # Update the last message (assistant response)
295
- history[-1] = (message, full_response)
296
- yield history
297
 
298
  thread.join()
299
 
300
  if not full_response.strip():
301
  full_response = "Model generated same text as input. Try adjusting parameters."
302
- history[-1] = (message, full_response)
303
- yield history
304
 
305
  except Exception as e:
306
  full_response = f"Error: {str(e)}"
307
- history[-1] = (message, full_response)
308
- yield history
309
 
310
  # ChatInterface
311
  chat_interface = gr.ChatInterface(
 
237
  ):
238
  """Handle chat response with streaming."""
239
  if not message.strip():
240
+ yield [{"role": "user", "content": msg} for msg, _ in history] + [{"role": "user", "content": message, "content": "Please enter a message."}]
241
  return
242
 
243
  # Add user message to history
244
+ history.append({"role": "user", "content": message})
245
+ yield history + [{"role": "assistant", "content": "..."}]
246
 
247
  # Generate response
248
  try:
 
251
 
252
  # Build prompt from history
253
  prompt = message
254
+ for item in history[:-1]:
255
+ role = item.get("role", "user")
256
+ content = item.get("content", "")
257
+ prompt = f"User: {content}\nAssistant: " + prompt
258
 
259
  prompt = prompt + "\nAssistant:"
260
 
 
294
  for text in streamer:
295
  full_response += text
296
  # Update the last message (assistant response)
297
+ current_history = history + [{"role": "assistant", "content": full_response}]
298
+ yield current_history
299
 
300
  thread.join()
301
 
302
  if not full_response.strip():
303
  full_response = "Model generated same text as input. Try adjusting parameters."
304
+ yield history + [{"role": "assistant", "content": full_response}]
 
305
 
306
  except Exception as e:
307
  full_response = f"Error: {str(e)}"
308
+ yield history + [{"role": "assistant", "content": full_response}]
 
309
 
310
  # ChatInterface
311
  chat_interface = gr.ChatInterface(