bhaskartripathi commited on
Commit
1eda479
·
verified ·
1 Parent(s): 8d06df6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -45
app.py CHANGED
@@ -1,5 +1,5 @@
1
- import urllib.request
2
- import fitz
3
  import re
4
  import numpy as np
5
  import tensorflow_hub as hub
@@ -98,8 +98,90 @@ def load_recommender(path, start_page=1):
98
  recommender.fit(chunks)
99
  return 'Corpus Loaded.'
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- def generate_text(openAI_key,prompt, engine="text-davinci-003"):
 
103
  openai.api_key = openAI_key
104
  completions = openai.Completion.create(
105
  engine=engine,
@@ -113,7 +195,7 @@ def generate_text(openAI_key,prompt, engine="text-davinci-003"):
113
  return message
114
 
115
 
116
- def generate_answer(question,openAI_key):
117
  topn_chunks = recommender(question)
118
  prompt = ""
119
  prompt += 'search results:\n\n'
@@ -127,66 +209,79 @@ def generate_answer(question,openAI_key):
127
  "don't add any additional information. Make sure the answer is correct and don't output false content. "\
128
  "If the text does not relate to the query, simply state 'Found Nothing'. Ignore outlier "\
129
  "search results which has nothing to do with the question. Only answer what is asked. The "\
130
- "answer should be short and concise.\n\nQuery: {question}\nAnswer: "
131
 
132
  prompt += f"Query: {question}\nAnswer:"
133
- answer = generate_text(openAI_key, prompt,"text-davinci-003")
134
  return answer
135
 
136
-
137
- def question_answer(url, file, question,openAI_key):
138
- if openAI_key.strip()=='':
139
- return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
140
- if url.strip() == '' and file == None:
141
- return '[ERROR]: Both URL and PDF is empty. Provide atleast one.'
142
-
143
- if url.strip() != '' and file != None:
144
- return '[ERROR]: Both URL and PDF is provided. Please provide only one (eiter URL or PDF).'
145
-
146
- if url.strip() != '':
147
- glob_url = url
148
- download_pdf(glob_url, 'corpus.pdf')
149
- load_recommender('corpus.pdf')
150
-
151
- else:
152
- old_file_name = file.name
153
- file_name = file.name
154
- file_name = file_name[:-12] + file_name[-4:]
155
- os.rename(old_file_name, file_name)
156
- load_recommender(file_name)
157
-
158
- if question.strip() == '':
159
- return '[ERROR]: Question field is empty'
160
-
161
- return generate_answer(question,openAI_key)
162
 
163
 
164
  recommender = SemanticSearch()
165
 
166
- title = 'PDF GPT'
167
- description = """ PDF GPT allows you to chat with your PDF file using Universal Sentence Encoder and Open AI. It gives hallucination free response than other tools as the embeddings are better than OpenAI. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly."""
168
 
 
169
 
170
- with gr.Blocks() as demo:
171
-
172
- gr.Markdown(f'<center><h1>{title}</h1></center>')
173
  gr.Markdown(description)
174
 
175
  with gr.Row():
176
 
177
  with gr.Group():
178
  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>')
179
- openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
180
- url = gr.Textbox(label='Enter PDF URL here')
181
- gr.Markdown("<center><h4>OR<h4></center>")
182
- file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
 
183
  question = gr.Textbox(label='Enter your question here')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  btn = gr.Button(value='Submit')
 
185
  btn.style(full_width=True)
186
- #openai.api_key = os.getenv('Your_Key_Here')
187
  with gr.Group():
188
- answer = gr.Textbox(label='The answer to your question is :')
 
 
 
 
 
 
 
 
 
 
 
189
 
190
- btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
191
 
192
- demo.launch()
 
1
+ import urllib.request
2
+ import fitz
3
  import re
4
  import numpy as np
5
  import tensorflow_hub as hub
 
98
  recommender.fit(chunks)
99
  return 'Corpus Loaded.'
100
 
101
+ def generate_text(openAI_key, prompt, model="gpt-3.5-turbo"):
102
+ openai.api_key = openAI_key
103
+ temperature=0.7
104
+ max_tokens=256
105
+ top_p=1
106
+ frequency_penalty=0
107
+ presence_penalty=0
108
+
109
+ if model == "text-davinci-003":
110
+ completions = openai.Completion.create(
111
+ engine=model,
112
+ prompt=prompt,
113
+ max_tokens=max_tokens,
114
+ n=1,
115
+ stop=None,
116
+ temperature=temperature,
117
+ )
118
+ message = completions.choices[0].text
119
+ else:
120
+ message = openai.ChatCompletion.create(
121
+ model=model,
122
+ messages=[
123
+ {"role": "system", "content": "You are a helpful assistant."},
124
+ {"role": "assistant", "content": "Here is some initial assistant message."},
125
+ {"role": "user", "content": prompt}
126
+ ],
127
+ temperature=.3,
128
+ max_tokens=max_tokens,
129
+ top_p=top_p,
130
+ frequency_penalty=frequency_penalty,
131
+ presence_penalty=presence_penalty,
132
+ ).choices[0].message['content']
133
+ return message
134
+
135
+
136
+ def generate_answer(question, openAI_key, model):
137
+ topn_chunks = recommender(question)
138
+ prompt = 'search results:\n\n'
139
+ for c in topn_chunks:
140
+ prompt += c + '\n\n'
141
+
142
+ prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
143
+ "Cite each reference using [ Page Number] notation. "\
144
+ "Only answer what is asked. The answer should be short and concise. \n\nQuery: "
145
+
146
+ prompt += f"{question}\nAnswer:"
147
+ answer = generate_text(openAI_key, prompt, model)
148
+ return answer
149
+
150
+
151
+ def question_answer(chat_history, url, file, question, openAI_key, model):
152
+ try:
153
+ if openAI_key.strip()=='':
154
+ return '[ERROR]: Please enter your Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
155
+ if url.strip() == '' and file is None:
156
+ return '[ERROR]: Both URL and PDF is empty. Provide at least one.'
157
+ if url.strip() != '' and file is not None:
158
+ return '[ERROR]: Both URL and PDF is provided. Please provide only one (either URL or PDF).'
159
+ if model is None or model =='':
160
+ return '[ERROR]: You have not selected any model. Please choose an LLM model.'
161
+ if url.strip() != '':
162
+ glob_url = url
163
+ download_pdf(glob_url, 'corpus.pdf')
164
+ load_recommender('corpus.pdf')
165
+ else:
166
+ old_file_name = file.name
167
+ file_name = file.name
168
+ file_name = file_name[:-12] + file_name[-4:]
169
+ os.rename(old_file_name, file_name)
170
+ load_recommender(file_name)
171
+ if question.strip() == '':
172
+ return '[ERROR]: Question field is empty'
173
+ if model == "text-davinci-003" or model == "gpt-4" or model == "gpt-4-32k":
174
+ answer = generate_answer_text_davinci_003(question, openAI_key)
175
+ else:
176
+ answer = generate_answer(question, openAI_key, model)
177
+ chat_history.append([question, answer])
178
+ return chat_history
179
+ except openai.error.InvalidRequestError as e:
180
+ return f'[ERROR]: Either you do not have access to GPT4 or you have exhausted your quota!'
181
+
182
 
183
+
184
+ def generate_text_text_davinci_003(openAI_key,prompt, engine="text-davinci-003"):
185
  openai.api_key = openAI_key
186
  completions = openai.Completion.create(
187
  engine=engine,
 
195
  return message
196
 
197
 
198
+ def generate_answer_text_davinci_003(question,openAI_key):
199
  topn_chunks = recommender(question)
200
  prompt = ""
201
  prompt += 'search results:\n\n'
 
209
  "don't add any additional information. Make sure the answer is correct and don't output false content. "\
210
  "If the text does not relate to the query, simply state 'Found Nothing'. Ignore outlier "\
211
  "search results which has nothing to do with the question. Only answer what is asked. The "\
212
+ "answer should be short and concise. \n\nQuery: {question}\nAnswer: "
213
 
214
  prompt += f"Query: {question}\nAnswer:"
215
+ answer = generate_text_text_davinci_003(openAI_key, prompt,"text-davinci-003")
216
  return answer
217
 
218
+ # pre-defined questions
219
+ questions = [
220
+ "What did the study investigate?",
221
+ "Can you provide a summary of this paper?",
222
+ "what are the methodologies used in this study?",
223
+ "what are the data intervals used in this study? Give me the start dates and end dates?",
224
+ "what are the main limitations of this study?",
225
+ "what are the main shortcomings of this study?",
226
+ "what are the main findings of the study?",
227
+ "what are the main results of the study?",
228
+ "what are the main contributions of this study?",
229
+ "what is the conclusion of this paper?",
230
+ "what are the input features used in this study?",
231
+ "what is the dependent variable in this study?",
232
+ ]
 
 
 
 
 
 
 
 
 
 
 
233
 
234
 
235
  recommender = SemanticSearch()
236
 
237
+ title = 'PDF GPT Turbo'
238
+ 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."""
239
 
240
+ with gr.Blocks(css="""#chatbot { font-size: 14px; min-height: 1200; }""") as demo:
241
 
242
+ gr.Markdown(f'<center><h3>{title}</h3></center>')
 
 
243
  gr.Markdown(description)
244
 
245
  with gr.Row():
246
 
247
  with gr.Group():
248
  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>')
249
+ with gr.Accordion("API Key"):
250
+ openAI_key = gr.Textbox(label='Enter your OpenAI API key here')
251
+ url = gr.Textbox(label='Enter PDF URL here (Example: https://arxiv.org/pdf/1706.03762.pdf )')
252
+ gr.Markdown("<center><h4>OR<h4></center>")
253
+ file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
254
  question = gr.Textbox(label='Enter your question here')
255
+ gr.Examples(
256
+ [[q] for q in questions],
257
+ inputs=[question],
258
+ label="PRE-DEFINED QUESTIONS: Click on a question to auto-fill the input box, then press Enter!",
259
+ )
260
+ model = gr.Radio([
261
+ 'gpt-3.5-turbo',
262
+ 'gpt-3.5-turbo-16k',
263
+ 'gpt-3.5-turbo-0613',
264
+ 'gpt-3.5-turbo-16k-0613',
265
+ 'text-davinci-003',
266
+ 'gpt-4',
267
+ 'gpt-4-32k'
268
+ ], label='Select Model', default='gpt-3.5-turbo')
269
  btn = gr.Button(value='Submit')
270
+
271
  btn.style(full_width=True)
272
+
273
  with gr.Group():
274
+ chatbot = gr.Chatbot(placeholder="Chat History", label="Chat History", lines=50, elem_id="chatbot")
275
+
276
+
277
+ #
278
+ # Bind the click event of the button to the question_answer function
279
+ btn.click(
280
+ question_answer,
281
+ inputs=[chatbot, url, file, question, openAI_key, model],
282
+ outputs=[chatbot],
283
+ )
284
+
285
+ demo.launch()
286
 
 
287