bhaskartripathi commited on
Commit
105f709
1 Parent(s): 18867be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -8
app.py CHANGED
@@ -141,7 +141,7 @@ def generate_answer(question, openAI_key,model):
141
  answer = generate_text(openAI_key, prompt, model)
142
  return answer
143
 
144
- def question_answer(url, file, question, openAI_key, model):
145
  try:
146
  if openAI_key.strip()=='':
147
  return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
@@ -170,6 +170,38 @@ def question_answer(url, file, question, openAI_key, model):
170
  except openai.error.InvalidRequestError as e:
171
  return f'[ERROR]: Either you do not have access to GPT4 or you have exhausted your quota!'
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
 
175
  def generate_text_text_davinci_003(openAI_key,prompt, engine="text-davinci-003"):
@@ -235,29 +267,29 @@ with gr.Blocks() as demo:
235
 
236
  with gr.Group():
237
  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>')
238
- #openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
239
  openAI_key = gr.Textbox(label='Enter your OpenAI API key here', password=True)
240
  url = gr.Textbox(label='Enter PDF URL here')
241
  gr.Markdown("<center><h4>OR<h4></center>")
242
  file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
243
  question = gr.Textbox(label='Enter your question here')
244
- #Predefined questions
245
  gr.Examples(
246
  [[q] for q in questions],
247
  inputs=[question],
248
  label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box, then press Enter!",
249
  )
250
  model = gr.Radio(['gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613', 'text-davinci-003','gpt-4','gpt-4-32k'], label='Select Model', default='gpt-3.5-turbo')
251
- #model = gr.Dropdown(choices=['gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613', 'text-davinci-003'], label='Select Large Language Model', default='gpt-3.5-turbo')
252
  btn = gr.Button(value='Submit')
253
  btn.style(full_width=True)
254
 
255
  with gr.Group():
256
- #answer = gr.Textbox(label='The answer to your question is :')
257
- answer = gr.Textbox(label='The answer to your question is :').style(show_copy_button=True)
258
 
 
 
 
 
 
 
259
 
260
- #btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
261
- btn.click(question_answer, inputs=[url, file, question, openAI_key, model], outputs=[answer])
262
  #openai.api_key = os.getenv('Your_Key_Here')
263
  demo.launch()
 
141
  answer = generate_text(openAI_key, prompt, model)
142
  return answer
143
 
144
+ def question_answer1(url, file, question, openAI_key, model):
145
  try:
146
  if openAI_key.strip()=='':
147
  return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
 
170
  except openai.error.InvalidRequestError as e:
171
  return f'[ERROR]: Either you do not have access to GPT4 or you have exhausted your quota!'
172
 
173
+ # Modifying your code to include chat history feature
174
+
175
+ def question_answer(chat_history, url, file, question, openAI_key, model):
176
+ try:
177
+ if openAI_key.strip()=='':
178
+ return '[ERROR]: Please enter your OpenAI Key. Get your key here : https://platform.openai.com/account/api-keys'
179
+ if url.strip() == '' and file == None:
180
+ return '[ERROR]: Both URL and PDF is empty. Provide at least one.'
181
+ if url.strip() != '' and file != None:
182
+ return '[ERROR]: Both URL and PDF is provided. Please provide only one (either URL or PDF).'
183
+ if model is None or model =='':
184
+ return '[ERROR]: You have not selected any model. Please choose an LLM model.'
185
+ if url.strip() != '':
186
+ glob_url = url
187
+ download_pdf(glob_url, 'corpus.pdf')
188
+ load_recommender('corpus.pdf')
189
+ else:
190
+ old_file_name = file.name
191
+ file_name = file.name
192
+ file_name = file_name[:-12] + file_name[-4:]
193
+ os.rename(old_file_name, file_name)
194
+ load_recommender(file_name)
195
+ if question.strip() == '':
196
+ return '[ERROR]: Question field is empty'
197
+ if model == "text-davinci-003":
198
+ return generate_answer_text_davinci_003(question, openAI_key)
199
+ else:
200
+ answer = generate_answer(question, openAI_key, model)
201
+ chat_history.append([question, answer])
202
+ return chat_history
203
+ except openai.error.InvalidRequestError as e:
204
+ return f'[ERROR]: Either you do not have access to GPT4 or you have exhausted your quota!'
205
 
206
 
207
  def generate_text_text_davinci_003(openAI_key,prompt, engine="text-davinci-003"):
 
267
 
268
  with gr.Group():
269
  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>')
 
270
  openAI_key = gr.Textbox(label='Enter your OpenAI API key here', password=True)
271
  url = gr.Textbox(label='Enter PDF URL here')
272
  gr.Markdown("<center><h4>OR<h4></center>")
273
  file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
274
  question = gr.Textbox(label='Enter your question here')
 
275
  gr.Examples(
276
  [[q] for q in questions],
277
  inputs=[question],
278
  label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box, then press Enter!",
279
  )
280
  model = gr.Radio(['gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613', 'text-davinci-003','gpt-4','gpt-4-32k'], label='Select Model', default='gpt-3.5-turbo')
 
281
  btn = gr.Button(value='Submit')
282
  btn.style(full_width=True)
283
 
284
  with gr.Group():
285
+ chatbot = gr.Chatbot(placeholder="Chat History", label="Chat History", lines=20)
 
286
 
287
+ question.submit(
288
+ question_answer,
289
+ [chatbot, url, file, question, openAI_key, model],
290
+ outputs=[chatbot],
291
+ show_progress=False,
292
+ )
293
 
 
 
294
  #openai.api_key = os.getenv('Your_Key_Here')
295
  demo.launch()