alanwnl commited on
Commit
088b84b
·
1 Parent(s): 6123591

Add application file

Browse files
Files changed (1) hide show
  1. app.py +43 -21
app.py CHANGED
@@ -123,23 +123,34 @@ def generate_text(openAI_key,
123
 
124
  def generate_answer(question, openAI_key, openAI_base, openAI_API_version):
125
  topn_chunks = recommender(question)
126
- print(len(topn_chunks))
127
- print(*topn_chunks, sep = "\n")
128
  prompt = ""
129
  prompt += 'search results:\n\n'
130
  for c in topn_chunks:
131
  prompt += c + '\n\n'
132
 
 
 
 
 
 
 
 
 
133
  prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
134
  "Cite each reference using [ Page Number] notation (every result has this number at the beginning). "\
135
- "Citation should be done at the end of each sentence. If the search results mention multiple subjects "\
136
- "with the same name, create separate answers for each. Only include information found in the results and "\
137
- "don't add any additional information. Make sure the answer is correct and don't output false content. "\
138
- "If the text does not relate to the query, simply state 'Found Nothing'. Ignore outlier "\
139
- "search results which has nothing to do with the question. Only answer what is asked. The "\
140
- "answer should be short and concise. \n\nQuery: {question}\nAnswer: "
 
 
141
 
142
  prompt += f"Query: {question}\nAnswer:"
 
143
  answer = generate_text(openAI_key, openAI_base, openAI_API_version, prompt,
144
  "chatgpt")
145
  return answer
@@ -173,6 +184,15 @@ def question_answer(url, file, question, openAI_key, openAI_base,
173
  return generate_answer(question, openAI_key, openAI_base,
174
  openAI_API_version)
175
 
 
 
 
 
 
 
 
 
 
176
  recommender = SemanticSearch()
177
 
178
  title = 'PDF GPT Azure'
@@ -203,7 +223,8 @@ with gr.Blocks() as demo:
203
  ## REMEMBER to remove the key before public deploy
204
  ##
205
  #####################
206
- openAI_key = gr.Textbox(label='Enter your Azure OpenAI API key here')
 
207
  openAI_base = gr.Textbox(label='api_base',
208
  value="https://api.hku.hk")
209
  openAI_API_version = gr.Textbox(label='API version',
@@ -212,17 +233,18 @@ with gr.Blocks() as demo:
212
  gr.Markdown("<center><h4>OR<h4></center>")
213
  file = gr.File(label='Upload your PDF/ Research Paper / Book here',
214
  file_types=['.pdf'])
215
-
216
  with gr.Group():
217
- question = gr.Textbox(label='Enter your question here')
218
- btn = gr.Button(value='Submit', scale=1)
219
- answer = gr.Textbox(label='The answer to your question is :')
220
-
221
- btn.click(question_answer,
222
- inputs=[
223
- url, file, question, openAI_key, openAI_base,
224
- openAI_API_version
225
- ],
226
- outputs=[answer])
227
- #openai.api_key = os.getenv('Your_Key_Here')
 
 
228
  demo.launch()
 
123
 
124
  def generate_answer(question, openAI_key, openAI_base, openAI_API_version):
125
  topn_chunks = recommender(question)
126
+ # print(len(topn_chunks))
127
+ # print(*topn_chunks, sep="\n")
128
  prompt = ""
129
  prompt += 'search results:\n\n'
130
  for c in topn_chunks:
131
  prompt += c + '\n\n'
132
 
133
+
134
+ # prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
135
+ # "Cite each reference using [ Page Number] notation (every result has this number at the beginning). "\
136
+ # "Citation should be done at the end of each sentence. "\
137
+ # "If the search results mention multiple subjects with the same name, create separate answers for each. "\
138
+ # "Make sure the answer is correct and don't output false content. "\
139
+ # "Only answer what is asked. The answer should be in details."\
140
+ # "\n\nQuery: {question}\nAnswer: "
141
  prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
142
  "Cite each reference using [ Page Number] notation (every result has this number at the beginning). "\
143
+ "Citation should be done at the end of each sentence. "\
144
+ "If the search results mention multiple subjects with the same name, create separate answers for each. "\
145
+ "Only include information found in the results and don't add any additional information."\
146
+ "Make sure the answer is correct and don't output false content. "\
147
+ "If the text does not relate to the query, simply state 'Found Nothing'. "\
148
+ "Ignore outlier search results which has nothing to do with the question."\
149
+ "Only answer what is asked. The answer should be short and concise."\
150
+ "\n\nQuery: {question}\nAnswer: "
151
 
152
  prompt += f"Query: {question}\nAnswer:"
153
+ print(prompt)
154
  answer = generate_text(openAI_key, openAI_base, openAI_API_version, prompt,
155
  "chatgpt")
156
  return answer
 
184
  return generate_answer(question, openAI_key, openAI_base,
185
  openAI_API_version)
186
 
187
+
188
+ def chatbot_respond(question, chat_history, url, file, openAI_key, openAI_base,
189
+ openAI_API_version):
190
+
191
+ bot_message = question_answer(url, file, question, openAI_key, openAI_base,
192
+ openAI_API_version)
193
+ chat_history.append((message, bot_message))
194
+ return "", chat_history
195
+
196
  recommender = SemanticSearch()
197
 
198
  title = 'PDF GPT Azure'
 
223
  ## REMEMBER to remove the key before public deploy
224
  ##
225
  #####################
226
+ openAI_key = gr.Textbox(
227
+ label='Enter your Azure OpenAI API key here')
228
  openAI_base = gr.Textbox(label='api_base',
229
  value="https://api.hku.hk")
230
  openAI_API_version = gr.Textbox(label='API version',
 
233
  gr.Markdown("<center><h4>OR<h4></center>")
234
  file = gr.File(label='Upload your PDF/ Research Paper / Book here',
235
  file_types=['.pdf'])
 
236
  with gr.Group():
237
+ chatbot = gr.Chatbot()
238
+ question = gr.Textbox()
239
+ clear = gr.ClearButton([msg, chatbot])
240
+
241
+ msg.submit(
242
+ chatbot_respond,
243
+ inputs=[
244
+ question, chatbot, url, file, openAI_key, openAI_base,
245
+ openAI_API_version
246
+ ],
247
+ outputs=[msg, chatbot],
248
+ )
249
+
250
  demo.launch()