Pijush2023 commited on
Commit
39bf3f3
·
verified ·
1 Parent(s): f58d69a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -215,28 +215,32 @@ def insert_prompt(current_text, prompt):
215
 
216
  # Function to add a user's message to the chat history and clear the input box
217
  def add_message(history, message):
218
- history.append((message, None)) # Add the user's message to the chat history
 
219
  return history, gr.Textbox(value="", interactive=True, show_label=False) # Clear the input box
220
 
221
 
 
222
  # Define the function to handle the conversation and get the response in a streaming way
223
  def chat_with_bot(history, user_message):
224
- # Add user message to the chat history
225
- history.append((user_message, ""))
 
 
 
226
 
227
- # Generate the response in a streaming manner
 
228
  response = get_response(user_message)
229
-
230
- # Simulate streaming by yielding each character of the response
231
  for character in response:
232
  history[-1] = (user_message, history[-1][1] + character)
233
  yield history # Stream each character
234
- time.sleep(0.05) # Adjust delay as needed for real-time effect
235
 
236
  yield history # Final yield to ensure full response is displayed
237
 
238
 
239
-
240
  # Create the Gradio Blocks interface
241
  with gr.Blocks() as demo:
242
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
@@ -263,8 +267,16 @@ with gr.Blocks() as demo:
263
  gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input)
264
 
265
  # Define interactions
266
- get_response_btn.click(fn=add_message, inputs=[chatbot, question_input], outputs=[chatbot, question_input])\
267
- .then(fn=chat_with_bot, inputs=[chatbot, question_input], outputs=chatbot)
 
 
 
 
 
 
 
 
268
  generate_audio_btn.click(fn=generate_audio_from_last_response, inputs=chatbot, outputs=audio_output)
269
  clean_btn.click(fn=clear_fields, inputs=[], outputs=[chatbot, question_input, audio_output])
270
 
 
215
 
216
  # Function to add a user's message to the chat history and clear the input box
217
  def add_message(history, message):
218
+ if message.strip():
219
+ history.append((message, None)) # Add the user's message to the chat history only if it's not empty
220
  return history, gr.Textbox(value="", interactive=True, show_label=False) # Clear the input box
221
 
222
 
223
+
224
  # Define the function to handle the conversation and get the response in a streaming way
225
  def chat_with_bot(history, user_message):
226
+ if not user_message.strip():
227
+ # If the user message is empty or whitespace, add an appropriate error message.
228
+ history.append(("It seems like your question is missing. Could you please provide more details or ask your question again?", None))
229
+ yield history
230
+ return
231
 
232
+ # Proceed with adding the user message and generating the bot response
233
+ history.append((user_message, ""))
234
  response = get_response(user_message)
235
+
 
236
  for character in response:
237
  history[-1] = (user_message, history[-1][1] + character)
238
  yield history # Stream each character
239
+ time.sleep(0.05) # Adjust delay as needed
240
 
241
  yield history # Final yield to ensure full response is displayed
242
 
243
 
 
244
  # Create the Gradio Blocks interface
245
  with gr.Blocks() as demo:
246
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
 
267
  gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input)
268
 
269
  # Define interactions
270
+ get_response_btn.click(
271
+ fn=add_message,
272
+ inputs=[chatbot, question_input],
273
+ outputs=[chatbot, question_input]
274
+ ).then(
275
+ fn=chat_with_bot,
276
+ inputs=[chatbot, question_input],
277
+ outputs=chatbot
278
+ )
279
+
280
  generate_audio_btn.click(fn=generate_audio_from_last_response, inputs=chatbot, outputs=audio_output)
281
  clean_btn.click(fn=clear_fields, inputs=[], outputs=[chatbot, question_input, audio_output])
282