binh99 commited on
Commit
1d0638d
·
2 Parent(s): 2c36339 2cb7b56
Files changed (2) hide show
  1. .idea/.name +1 -0
  2. app.py +36 -10
.idea/.name ADDED
@@ -0,0 +1 @@
 
 
1
+ app.py
app.py CHANGED
@@ -26,17 +26,25 @@ def add_details(lst):
26
 
27
 
28
  prompt_template = "Instructions: Compose a comprehensive reply to the query using the search results given. " \
29
- "Make sure to cite results using [number] notation after the reference. " \
30
  "If the search results mention multiple subjects " \
31
  "with the same name, create separate answers for each. Only include information found in the results and " \
32
  "don't add any additional information. Make sure the answer is correct and don't output false content. " \
33
  "Ignore outlier search results which has nothing to do with the question. Only answer what is asked. " \
34
- "The answer should be short and concise.\n\nQuery: {question}\nAnswer: "
35
 
36
- # MODELS = ["universal-sentence-encoder", "instructor-large"]
37
  MODELS = ["text-davinci-001", "text-davinci-002", "text-davinci-003"]
 
 
 
 
 
 
 
 
 
 
38
 
39
-
40
  def add_source_numbers(lst):
41
  return [item[:3] + '\t' + item[3:] for item in (lst)]
42
 
@@ -60,11 +68,11 @@ def preprocess(text):
60
  def pdf_to_text(files_src, start_page=1, end_page=None):
61
  text_list = []
62
  for file in files_src:
63
- if os.path.splitext(file.name)[1] == ".pdf":
64
  doc = fitz.open(file.name)
65
  total_pages = doc.page_count
66
- if end_page is None:
67
- end_page = total_pages
68
  for i in range(start_page - 1, end_page):
69
  text = doc.load_page(i).get_text("text")
70
  text = preprocess(text)
@@ -124,6 +132,7 @@ def predict(
124
  chatbot,
125
  inputs,
126
  temperature,
 
127
  selected_model=MODELS[0],
128
  files=None
129
  ):
@@ -142,7 +151,7 @@ def predict(
142
  for c in topn_chunks:
143
  prompt += c + '\n\n'
144
  prompt += prompt_template
145
- prompt += f"Query: {inputs}\nAnswer:"
146
  inputs = prompt
147
  reference_results = add_source_numbers(topn_chunks)
148
  display_reference = add_details(reference_results)
@@ -215,7 +224,7 @@ with gr.Blocks(css=customCSS, theme=beautiful_theme) as demo:
215
  with gr.Column(scale=5):
216
  with gr.Row():
217
  chatbot = gr.Chatbot(elem_id="chatbot").style(height="100%")
218
- with gr.Row():
219
  with gr.Column(scale=12):
220
  user_input = gr.Textbox(
221
  show_label=False, placeholder="Enter here"
@@ -227,12 +236,21 @@ with gr.Blocks(css=customCSS, theme=beautiful_theme) as demo:
227
  with gr.Column(min_width=50, scale=1):
228
  with gr.Tab(label="ChatGPT"):
229
  gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a '
 
230
  f'href="https://platform.openai.com/account/api-keys">here</a></p>')
231
  openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
 
 
 
 
232
  model_select_dropdown = gr.Dropdown(
233
  label="Select model", choices=MODELS, multiselect=False, value=MODELS[0]
234
  )
 
 
 
235
  index_files = gr.Files(label="Files", type="file", multiple=True)
 
236
  gr.Markdown(
237
  "⚠️Be careful to change ⚠️\n\nIf you can't use it, please restore the default settings")
238
  with gr.Tab(label="Advanced"):
@@ -245,10 +263,18 @@ with gr.Blocks(css=customCSS, theme=beautiful_theme) as demo:
245
  interactive=True,
246
  label="Temperature",
247
  )
 
248
  user_input.submit(predict, inputs=[openAI_key, history, chatbot, user_input, temperature, model_select_dropdown, index_files],
249
  outputs=[chatbot, history])
250
  user_input.submit(lambda: "", None, user_input)
251
  submitBtn.click(predict, inputs=[openAI_key, history, chatbot, user_input, temperature, model_select_dropdown, index_files],
 
 
 
 
 
 
 
252
  outputs=[chatbot, history])
253
  submitBtn.click(lambda: "", None, user_input)
254
- demo.queue(concurrency_count=10).launch(server_name="0.0.0.0", server_port=7860, debug=True)
 
26
 
27
 
28
  prompt_template = "Instructions: Compose a comprehensive reply to the query using the search results given. " \
 
29
  "If the search results mention multiple subjects " \
30
  "with the same name, create separate answers for each. Only include information found in the results and " \
31
  "don't add any additional information. Make sure the answer is correct and don't output false content. " \
32
  "Ignore outlier search results which has nothing to do with the question. Only answer what is asked. " \
33
+ "The answer should be short and concise. \n\nQuery: {question}\nAnswer: "
34
 
 
35
  MODELS = ["text-davinci-001", "text-davinci-002", "text-davinci-003"]
36
+ LANGUAGES = [
37
+ "English",
38
+ "简体中文",
39
+ "日本語",
40
+ "Deutsch",
41
+ "Vietnamese"
42
+ ]
43
+ def set_openai_api_key(my_api_key):
44
+ openai.api_key = my_api_key
45
+ return gr.update(visible = True)
46
 
47
+
48
  def add_source_numbers(lst):
49
  return [item[:3] + '\t' + item[3:] for item in (lst)]
50
 
 
68
  def pdf_to_text(files_src, start_page=1, end_page=None):
69
  text_list = []
70
  for file in files_src:
71
+ if (os.path.splitext(file.name)[1]).lower() == ".pdf":
72
  doc = fitz.open(file.name)
73
  total_pages = doc.page_count
74
+ # if end_page is None:
75
+ end_page = total_pages
76
  for i in range(start_page - 1, end_page):
77
  text = doc.load_page(i).get_text("text")
78
  text = preprocess(text)
 
132
  chatbot,
133
  inputs,
134
  temperature,
135
+ lang = LANGUAGES[0],
136
  selected_model=MODELS[0],
137
  files=None
138
  ):
 
151
  for c in topn_chunks:
152
  prompt += c + '\n\n'
153
  prompt += prompt_template
154
+ prompt += f"Query: {inputs}. Reply in {lang}\nAnswer:"
155
  inputs = prompt
156
  reference_results = add_source_numbers(topn_chunks)
157
  display_reference = add_details(reference_results)
 
224
  with gr.Column(scale=5):
225
  with gr.Row():
226
  chatbot = gr.Chatbot(elem_id="chatbot").style(height="100%")
227
+ with gr.Row(visible=False) as input_raws:
228
  with gr.Column(scale=12):
229
  user_input = gr.Textbox(
230
  show_label=False, placeholder="Enter here"
 
236
  with gr.Column(min_width=50, scale=1):
237
  with gr.Tab(label="ChatGPT"):
238
  gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a '
239
+ <<<<<<< HEAD
240
  f'href="https://platform.openai.com/account/api-keys">here</a></p>')
241
  openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
242
+ =======
243
+ f'href="https://platform.openai.com/account/api-keys">here</a></p>')
244
+ openAI_key=gr.Textbox(label='Enter your OpenAI API key here and press Enter')
245
+ >>>>>>> 2cb7b56a3054d0b5febf8ed9db8c59395b2d0327
246
  model_select_dropdown = gr.Dropdown(
247
  label="Select model", choices=MODELS, multiselect=False, value=MODELS[0]
248
  )
249
+ language_select_dropdown = gr.Dropdown(
250
+ label="Select reply language", choices=LANGUAGES, multiselect=False, value=LANGUAGES[0]
251
+ )
252
  index_files = gr.Files(label="Files", type="file", multiple=True)
253
+ with gr.Tab(label="Advanced"):
254
  gr.Markdown(
255
  "⚠️Be careful to change ⚠️\n\nIf you can't use it, please restore the default settings")
256
  with gr.Tab(label="Advanced"):
 
263
  interactive=True,
264
  label="Temperature",
265
  )
266
+ <<<<<<< HEAD
267
  user_input.submit(predict, inputs=[openAI_key, history, chatbot, user_input, temperature, model_select_dropdown, index_files],
268
  outputs=[chatbot, history])
269
  user_input.submit(lambda: "", None, user_input)
270
  submitBtn.click(predict, inputs=[openAI_key, history, chatbot, user_input, temperature, model_select_dropdown, index_files],
271
+ =======
272
+ openAI_key.submit(set_openai_api_key, [openAI_key], [input_raws])
273
+ user_input.submit(predict, inputs=[history, chatbot, user_input, temperature, language_select_dropdown, model_select_dropdown, index_files],
274
+ outputs=[chatbot, history])
275
+ user_input.submit(lambda: "", None, user_input)
276
+ submitBtn.click(predict, inputs=[history, chatbot, user_input, temperature, language_select_dropdown, model_select_dropdown, index_files],
277
+ >>>>>>> 2cb7b56a3054d0b5febf8ed9db8c59395b2d0327
278
  outputs=[chatbot, history])
279
  submitBtn.click(lambda: "", None, user_input)
280
+ demo.queue(concurrency_count=10).launch(server_name="0.0.0.0", server_port=7862)