Shreyas094 commited on
Commit
fb4793f
·
verified ·
1 Parent(s): f6f35dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -188,15 +188,31 @@ class CitingSources(BaseModel):
188
  ...,
189
  description="List of sources to cite. Should be an URL of the source."
190
  )
191
- def chatbot_interface(message, history, use_web_search, model, temperature, num_calls):
192
- if not message.strip():
193
  return "", history
194
 
195
- history = history + [(message, "")]
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
  try:
198
  for response in respond(message, history, model, temperature, num_calls, use_web_search):
199
- history[-1] = (message, response)
 
 
 
200
  yield history
201
  except gr.CancelledError:
202
  yield history
@@ -425,13 +441,15 @@ css = """
425
  # Define the checkbox outside the demo block
426
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
427
 
 
428
  demo = gr.ChatInterface(
429
- respond,
430
  additional_inputs=[
 
431
  gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0]),
432
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
433
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
434
- use_web_search # Add this line to include the checkbox
435
  ],
436
  title="AI-powered Web Search and PDF Chat Assistant",
437
  description="Chat with your PDFs or use web search to answer questions.",
@@ -464,16 +482,22 @@ demo = gr.ChatInterface(
464
  analytics_enabled=False,
465
  )
466
 
467
- # In the Gradio interface
468
  with demo:
469
  continue_button = gr.Button("Continue Generation")
470
- continue_button.click(continue_generation,
471
- inputs=[gr.State(lambda: history[-1][0] if history else ""),
472
- gr.State(lambda: history[-1][1] if history else ""),
473
- gr.Dropdown(choices=MODELS, label="Select Model"),
474
- gr.Slider(label="Temperature"),
475
- gr.Slider(label="Number of API Calls")],
476
- outputs=gr.Chatbot())
 
 
 
 
 
 
477
 
478
  # Add file upload functionality
479
  with demo:
 
188
  ...,
189
  description="List of sources to cite. Should be an URL of the source."
190
  )
191
+ def chatbot_interface(message, history, use_web_search, model, temperature, num_calls, continue_generation=False):
192
+ if not message.strip() and not continue_generation:
193
  return "", history
194
 
195
+ if continue_generation and history:
196
+ last_message, last_response = history[-1]
197
+ continuation_prompt = f"""
198
+ Original query: {last_message}
199
+
200
+ Previously generated response:
201
+ {last_response}
202
+
203
+ Please continue the response from where it left off, maintaining coherence and relevance to the original query.
204
+ """
205
+ history = history[:-1] # Remove the last response to replace it with the continued one
206
+ message = continuation_prompt
207
+ else:
208
+ history = history + [(message, "")]
209
 
210
  try:
211
  for response in respond(message, history, model, temperature, num_calls, use_web_search):
212
+ if continue_generation and history:
213
+ history.append((history[-1][0], history[-1][1] + response))
214
+ else:
215
+ history[-1] = (message, response)
216
  yield history
217
  except gr.CancelledError:
218
  yield history
 
441
  # Define the checkbox outside the demo block
442
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
443
 
444
+ # Modify your main chat interface to use the same function
445
  demo = gr.ChatInterface(
446
+ fn=chatbot_interface,
447
  additional_inputs=[
448
+ use_web_search,
449
  gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0]),
450
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
451
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
452
+ gr.State(False) # Flag for continue_generation, default False for normal chat
453
  ],
454
  title="AI-powered Web Search and PDF Chat Assistant",
455
  description="Chat with your PDFs or use web search to answer questions.",
 
482
  analytics_enabled=False,
483
  )
484
 
485
+ # In your Gradio interface setup
486
  with demo:
487
  continue_button = gr.Button("Continue Generation")
488
+ continue_button.click(
489
+ chatbot_interface,
490
+ inputs=[
491
+ gr.Textbox(value="", visible=False), # Hidden textbox for the message
492
+ gr.State([]), # Chat history
493
+ use_web_search,
494
+ gr.Dropdown(choices=MODELS, label="Select Model"),
495
+ gr.Slider(label="Temperature"),
496
+ gr.Slider(label="Number of API Calls"),
497
+ gr.State(True) # Flag for continue_generation
498
+ ],
499
+ outputs=gr.Chatbot()
500
+ )
501
 
502
  # Add file upload functionality
503
  with demo: