bhaskartripathi commited on
Commit
6a25499
1 Parent(s): 6c2ad46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -282,18 +282,20 @@ with gr.Blocks(css="""#chatbot { font-size: 14px; min-height: 1200; }""") as dem
282
  def respond(message, chat_history, url_value, file_value, key_value, model_value):
283
  if message.strip() == "":
284
  return "", chat_history # Return empty message if no input
285
-
286
  try:
 
 
 
 
287
  if key_value.strip() == '':
288
- chat_history = chat_history + [
289
- [message, '[ERROR]: Please enter your OpenAI API key']
290
- ]
291
  return "", chat_history
292
-
293
  if url_value.strip() == '' and file_value is None:
294
- chat_history = chat_history + [
295
- [message, '[ERROR]: Both URL and PDF are empty. Provide at least one']
296
- ]
297
  return "", chat_history
298
 
299
  # Process PDF and generate answer
@@ -307,16 +309,15 @@ with gr.Blocks(css="""#chatbot { font-size: 14px; min-height: 1200; }""") as dem
307
  load_recommender(file_name)
308
 
309
  answer = generate_answer(message, key_value, model_value)
310
-
311
- # Update chat history with the new message pair
312
- chat_history = chat_history + [[message, answer]]
313
-
314
  return "", chat_history
315
 
316
  except Exception as e:
317
- chat_history = chat_history + [
318
- [message, f'[ERROR]: {str(e)}']
319
- ]
320
  return "", chat_history
321
 
322
  submit_btn.click(
 
282
  def respond(message, chat_history, url_value, file_value, key_value, model_value):
283
  if message.strip() == "":
284
  return "", chat_history # Return empty message if no input
285
+
286
  try:
287
+ # Ensure chat_history is initialized properly
288
+ if chat_history is None:
289
+ chat_history = []
290
+
291
  if key_value.strip() == '':
292
+ chat_history.append({"role": "user", "content": message})
293
+ chat_history.append({"role": "assistant", "content": '[ERROR]: Please enter your OpenAI API key'})
 
294
  return "", chat_history
295
+
296
  if url_value.strip() == '' and file_value is None:
297
+ chat_history.append({"role": "user", "content": message})
298
+ chat_history.append({"role": "assistant", "content": '[ERROR]: Both URL and PDF are empty. Provide at least one'})
 
299
  return "", chat_history
300
 
301
  # Process PDF and generate answer
 
309
  load_recommender(file_name)
310
 
311
  answer = generate_answer(message, key_value, model_value)
312
+
313
+ chat_history.append({"role": "user", "content": message})
314
+ chat_history.append({"role": "assistant", "content": answer})
315
+
316
  return "", chat_history
317
 
318
  except Exception as e:
319
+ chat_history.append({"role": "user", "content": message})
320
+ chat_history.append({"role": "assistant", "content": f'[ERROR]: {str(e)}'})
 
321
  return "", chat_history
322
 
323
  submit_btn.click(