Avinashstat commited on
Commit
89c72eb
·
verified ·
1 Parent(s): 91b02cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -39
app.py CHANGED
@@ -263,12 +263,16 @@ with gr.Blocks() as demo:
263
  label='Select Model',
264
  value='gpt-4o-mini'
265
  )
266
-
267
  # Chat Interface
268
- chatbot = gr.Chatbot(label="Chat History", type="messages")
269
  msg = gr.Textbox(label="Enter your question here", lines=2)
270
  submit_btn = gr.Button("Submit")
271
  clear = gr.ClearButton([msg, chatbot])
 
 
 
 
272
 
273
  # Example Questions
274
  gr.Examples(
@@ -277,52 +281,50 @@ with gr.Blocks() as demo:
277
  label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box",
278
  )
279
 
280
- def respond(message, chat_history, url_value, file_value, key_value, model_value):
281
- if message.strip() == "":
282
- return "", chat_history # Return empty message if no input
283
 
284
- try:
285
- # Ensure chat_history is initialized properly
286
- if chat_history is None:
287
- chat_history = []
288
 
289
- if key_value.strip() == '':
290
- chat_history.append({"role": "user", "content": message})
291
- chat_history.append({"role": "assistant", "content": '[ERROR]: Please enter your OpenAI API key'})
292
- return "", chat_history
293
-
294
- if url_value.strip() == '' and file_value is None:
295
- chat_history.append({"role": "user", "content": message})
296
- chat_history.append({"role": "assistant", "content": '[ERROR]: Both URL and PDF are empty. Provide at least one'})
297
- return "", chat_history
298
 
299
- # Process PDF and generate answer
300
- if url_value.strip() != '':
301
- download_pdf(url_value, 'corpus.pdf')
302
- load_recommender('corpus.pdf')
303
- else:
304
- old_file_name = file_value.name
305
- file_name = old_file_name[:-12] + old_file_name[-4:]
306
- os.rename(old_file_name, file_name)
307
- load_recommender(file_name)
308
 
309
- answer = generate_answer(message, key_value, model_value)
 
 
 
 
 
 
 
 
310
 
311
- chat_history.append({"role": "user", "content": message})
312
- chat_history.append({"role": "assistant", "content": answer})
313
 
314
- return "", chat_history
 
 
315
 
316
- except Exception as e:
317
- chat_history.append({"role": "user", "content": message})
318
- chat_history.append({"role": "assistant", "content": f'[ERROR]: {str(e)}'})
319
- return "", chat_history
320
 
321
  submit_btn.click(
322
- respond,
323
- [msg, chatbot, url, file, openAI_key, model],
324
- [msg, chatbot]
325
- )
326
 
327
  msg.submit(
328
  respond,
 
263
  label='Select Model',
264
  value='gpt-4o-mini'
265
  )
266
+
267
  # Chat Interface
268
+ chatbot = gr.Chatbot(label="Chat History") # default: list of [user, bot] pairs
269
  msg = gr.Textbox(label="Enter your question here", lines=2)
270
  submit_btn = gr.Button("Submit")
271
  clear = gr.ClearButton([msg, chatbot])
272
+ #chatbot = gr.Chatbot(label="Chat History", type="messages")
273
+ #msg = gr.Textbox(label="Enter your question here", lines=2)
274
+ #submit_btn = gr.Button("Submit")
275
+ #clear = gr.ClearButton([msg, chatbot])
276
 
277
  # Example Questions
278
  gr.Examples(
 
281
  label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box",
282
  )
283
 
284
+ def respond(message, chat_history, url_value, file_value, key_value, model_value):
285
+ if message.strip() == "":
286
+ return "", chat_history # nothing to do
287
 
288
+ # Initialize chat_history as a list of [user, assistant] pairs
289
+ if chat_history is None:
290
+ chat_history = []
 
291
 
292
+ try:
293
+ # Basic validations
294
+ if key_value.strip() == '':
295
+ chat_history.append([message, '[ERROR]: Please enter your OpenAI API key'])
296
+ return "", chat_history
 
 
 
 
297
 
298
+ if url_value.strip() == '' and file_value is None:
299
+ chat_history.append([message, '[ERROR]: Both URL and PDF are empty. Provide at least one'])
300
+ return "", chat_history
 
 
 
 
 
 
301
 
302
+ # Prepare corpus (URL or uploaded file)
303
+ if url_value.strip() != '':
304
+ download_pdf(url_value, 'corpus.pdf')
305
+ load_recommender('corpus.pdf')
306
+ else:
307
+ old_file_name = file_value.name
308
+ file_name = old_file_name[:-12] + old_file_name[-4:]
309
+ os.rename(old_file_name, file_name)
310
+ load_recommender(file_name)
311
 
312
+ # Generate answer
313
+ answer = generate_answer(message, key_value, model_value)
314
 
315
+ # Append pair [user, assistant]
316
+ chat_history.append([message, answer])
317
+ return "", chat_history
318
 
319
+ except Exception as e:
320
+ chat_history.append([message, f'[ERROR]: {str(e)}'])
321
+ return "", chat_history
 
322
 
323
  submit_btn.click(
324
+ respond,
325
+ [msg, chatbot, url, file, openAI_key, model],
326
+ [msg, chatbot]
327
+ )
328
 
329
  msg.submit(
330
  respond,