bhaskartripathi commited on
Commit
ffe0f3c
·
verified ·
1 Parent(s): 228a44a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -23
app.py CHANGED
@@ -239,25 +239,19 @@ title = 'PDF GPT Turbo'
239
  description = """ PDF GPT Turbo allows you to chat with your PDF files. It uses Google's Universal Sentence Encoder with Deep averaging network (DAN) to give hallucination free response by improving the embedding quality of OpenAI. It cites the page number in square brackets([Page No.]) and shows where the information is located, adding credibility to the responses."""
240
 
241
  with gr.Blocks(css="""#chatbot { font-size: 14px; min-height: 1200; }""") as demo:
242
-
243
  gr.Markdown(f'<center><h3>{title}</h3></center>')
244
  gr.Markdown(description)
245
 
246
  with gr.Row():
247
-
248
- with gr.Group():
249
- gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
250
- with gr.Accordion("API Key"):
251
- openAI_key = gr.Textbox(label='Enter your OpenAI API key here')
252
- url = gr.Textbox(label='Enter PDF URL here (Example: https://arxiv.org/pdf/1706.03762.pdf )')
253
  gr.Markdown("<center><h4>OR<h4></center>")
254
- file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
255
- question = gr.Textbox(label='Enter your question here')
256
- gr.Examples(
257
- [[q] for q in questions],
258
- inputs=[question],
259
- label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box, then press Enter!",
260
- )
261
  model = gr.Radio(
262
  choices=[
263
  'gpt-3.5-turbo',
@@ -271,18 +265,60 @@ with gr.Blocks(css="""#chatbot { font-size: 14px; min-height: 1200; }""") as dem
271
  label='Select Model',
272
  value='gpt-3.5-turbo'
273
  )
274
- btn = gr.Button(value='Submit')
275
 
276
- with gr.Group():
277
- chatbot = gr.Chatbot(placeholder="Chat History", label="Chat History", elem_id="chatbot")
 
 
278
 
 
 
 
 
 
 
279
 
280
- #
281
- # Bind the click event of the button to the question_answer function
282
- btn.click(
283
- question_answer,
284
- inputs=[chatbot, url, file, question, openAI_key, model],
285
- outputs=[chatbot],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  )
287
 
288
  demo.launch()
 
239
  description = """ PDF GPT Turbo allows you to chat with your PDF files. It uses Google's Universal Sentence Encoder with Deep averaging network (DAN) to give hallucination free response by improving the embedding quality of OpenAI. It cites the page number in square brackets([Page No.]) and shows where the information is located, adding credibility to the responses."""
240
 
241
  with gr.Blocks(css="""#chatbot { font-size: 14px; min-height: 1200; }""") as demo:
 
242
  gr.Markdown(f'<center><h3>{title}</h3></center>')
243
  gr.Markdown(description)
244
 
245
  with gr.Row():
246
+ with gr.Column():
247
+ # API Key and File Inputs
248
+ with gr.Accordion("API Key and PDF"):
249
+ openAI_key = gr.Textbox(label='Enter your OpenAI API key here', type='password')
250
+ url = gr.Textbox(label='Enter PDF URL here (Example: https://arxiv.org/pdf/1706.03762.pdf)')
 
251
  gr.Markdown("<center><h4>OR<h4></center>")
252
+ file = gr.File(label='Upload your PDF/Research Paper/Book here', file_types=['.pdf'])
253
+
254
+ # Model Selection
 
 
 
 
255
  model = gr.Radio(
256
  choices=[
257
  'gpt-3.5-turbo',
 
265
  label='Select Model',
266
  value='gpt-3.5-turbo'
267
  )
 
268
 
269
+ # Chat Interface
270
+ chatbot = gr.Chatbot(label="Chat History", type="messages")
271
+ msg = gr.Textbox(label="Enter your question here", lines=2)
272
+ clear = gr.ClearButton([msg, chatbot])
273
 
274
+ # Example Questions
275
+ gr.Examples(
276
+ [[q] for q in questions],
277
+ inputs=[msg],
278
+ label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box",
279
+ )
280
 
281
+ def respond(message, chat_history, url_value, file_value, key_value, model_value):
282
+ try:
283
+ if key_value.strip() == '':
284
+ return chat_history + [
285
+ {"role": "user", "content": message},
286
+ {"role": "assistant", "content": '[ERROR]: Please enter your OpenAI API key'}
287
+ ]
288
+
289
+ if url_value.strip() == '' and file_value is None:
290
+ return chat_history + [
291
+ {"role": "user", "content": message},
292
+ {"role": "assistant", "content": '[ERROR]: Both URL and PDF are empty. Provide at least one'}
293
+ ]
294
+
295
+ # Process PDF and generate answer
296
+ if url_value.strip() != '':
297
+ download_pdf(url_value, 'corpus.pdf')
298
+ load_recommender('corpus.pdf')
299
+ else:
300
+ old_file_name = file_value.name
301
+ file_name = old_file_name[:-12] + old_file_name[-4:]
302
+ os.rename(old_file_name, file_name)
303
+ load_recommender(file_name)
304
+
305
+ answer = generate_answer(message, key_value, model_value)
306
+
307
+ return chat_history + [
308
+ {"role": "user", "content": message},
309
+ {"role": "assistant", "content": answer}
310
+ ]
311
+
312
+ except Exception as e:
313
+ return chat_history + [
314
+ {"role": "user", "content": message},
315
+ {"role": "assistant", "content": f'[ERROR]: {str(e)}'}
316
+ ]
317
+
318
+ msg.submit(
319
+ respond,
320
+ [msg, chatbot, url, file, openAI_key, model],
321
+ [chatbot]
322
  )
323
 
324
  demo.launch()