bhaskartripathi commited on
Commit
dbd6aa4
·
1 Parent(s): 02b70dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -14
app.py CHANGED
@@ -110,7 +110,7 @@ def generate_text(openAI_key,prompt, engine="text-davinci-003"):
110
  message = completions.choices[0].text
111
  return message
112
 
113
- def generate_answer(question,openAI_key):
114
  topn_chunks = recommender(question)
115
  prompt = ""
116
  prompt += 'search results:\n\n'
@@ -130,15 +130,65 @@ def generate_answer(question,openAI_key):
130
  answer = generate_text(openAI_key, prompt,"text-davinci-003")
131
  return answer
132
 
 
 
 
 
 
 
 
133
 
134
- def question_answer(url, file, question, openAI_key):
135
- if openAI_key.strip() == '':
136
- return '[ERROR]: Please enter your Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
137
- if url.strip() == '' and (file is None or file.size == 0):
138
- return '[ERROR]: Both URL and PDF is empty. Provide at least one.'
139
 
140
- if url.strip() != '' and (file is not None and file.size != 0):
141
- return '[ERROR]: Both URL and PDF is provided. Please provide only one (either URL or PDF).'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  if url.strip() != '':
144
  glob_url = url
@@ -146,9 +196,6 @@ def question_answer(url, file, question, openAI_key):
146
  load_recommender('corpus.pdf')
147
 
148
  else:
149
- if file.size == 0:
150
- return '[ERROR]: The uploaded file is empty. Please provide a non-empty PDF file.'
151
-
152
  old_file_name = file.name
153
  file_name = file.name
154
  file_name = file_name[:-12] + file_name[-4:]
@@ -158,8 +205,7 @@ def question_answer(url, file, question, openAI_key):
158
  if question.strip() == '':
159
  return '[ERROR]: Question field is empty'
160
 
161
- return generate_answer(question, openAI_key)
162
-
163
 
164
 
165
  recommender = SemanticSearch()
@@ -187,6 +233,6 @@ with gr.Blocks() as demo:
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],api_name="pdfGPTAPI")
191
  #openai.api_key = os.getenv('Your_Key_Here')
192
  demo.launch()
 
110
  message = completions.choices[0].text
111
  return message
112
 
113
+ def generate_answer1(question,openAI_key):
114
  topn_chunks = recommender(question)
115
  prompt = ""
116
  prompt += 'search results:\n\n'
 
130
  answer = generate_text(openAI_key, prompt,"text-davinci-003")
131
  return answer
132
 
133
+ def generate_answer(question, openAI_key):
134
+ topn_chunks = recommender(question)
135
+
136
+ max_tokens = 4096 # Maximum tokens allowed for text-davinci-003
137
+ completion_tokens = 512 # Tokens reserved for the completion
138
+ tokenizer = OpenAITokenizer()
139
+ max_prompt_tokens = max_tokens - completion_tokens
140
 
141
+ # Split search results into groups based on token count
142
+ search_results_groups = []
143
+ current_group = []
144
+ current_group_tokens = 0
 
145
 
146
+ for c in topn_chunks:
147
+ c_tokens = len(tokenizer.tokenize(c))
148
+ if current_group_tokens + c_tokens <= max_prompt_tokens:
149
+ current_group.append(c)
150
+ current_group_tokens += c_tokens
151
+ else:
152
+ search_results_groups.append(current_group)
153
+ current_group = [c]
154
+ current_group_tokens = c_tokens
155
+
156
+ if current_group:
157
+ search_results_groups.append(current_group)
158
+
159
+ # Generate response for each group of search results
160
+ responses = []
161
+ for search_results in search_results_groups:
162
+ prompt = 'search results:\n\n'
163
+ for c in search_results:
164
+ prompt += c + '\n\n'
165
+
166
+ prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
167
+ "Cite each reference using [ Page Number] notation (every result has this number at the beginning). "\
168
+ "Citation should be done at the end of each sentence. If the search results mention multiple subjects "\
169
+ "with the same name, create separate answers for each. Only include information found in the results and "\
170
+ "don't add any additional information. Make sure the answer is correct and don't output false content. "\
171
+ "If the text does not relate to the query, simply state 'Text Not Found in PDF'. Ignore outlier "\
172
+ "search results which has nothing to do with the question. Only answer what is asked. The "\
173
+ "answer should be short and concise. Answer step-by-step. \n\nQuery: {question}\nAnswer: "
174
+
175
+ response = generate_text(openAI_key, prompt, "text-davinci-003")
176
+ responses.append(response)
177
+
178
+ # Combine and clean up the responses
179
+ final_answer = " ".join(responses).strip()
180
+
181
+ return final_answer
182
+
183
+
184
+ def question_answer(url, file, question,openAI_key):
185
+ if openAI_key.strip()=='':
186
+ return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
187
+ if url.strip() == '' and file == None:
188
+ return '[ERROR]: Both URL and PDF is empty. Provide atleast one.'
189
+
190
+ if url.strip() != '' and file != None:
191
+ return '[ERROR]: Both URL and PDF is provided. Please provide only one (eiter URL or PDF).'
192
 
193
  if url.strip() != '':
194
  glob_url = url
 
196
  load_recommender('corpus.pdf')
197
 
198
  else:
 
 
 
199
  old_file_name = file.name
200
  file_name = file.name
201
  file_name = file_name[:-12] + file_name[-4:]
 
205
  if question.strip() == '':
206
  return '[ERROR]: Question field is empty'
207
 
208
+ return generate_answer(question,openAI_key)
 
209
 
210
 
211
  recommender = SemanticSearch()
 
233
  with gr.Group():
234
  answer = gr.Textbox(label='The answer to your question is :')
235
 
236
+ btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
237
  #openai.api_key = os.getenv('Your_Key_Here')
238
  demo.launch()