3v324v23 commited on
Commit
58536d8
·
1 Parent(s): 5f9f028

change to MMR Retriever

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -29,6 +29,7 @@ huggingface_embeddings= HuggingFaceEmbeddings(
29
  encode_kwargs = encode_kwargs
30
  )
31
 
 
32
  # %%
33
  persist_directory = 'chroma/'
34
  vectordb = Chroma(embedding_function=huggingface_embeddings,persist_directory=persist_directory)
@@ -58,14 +59,11 @@ def answer(text,context=""):
58
 
59
  # helper function for RAG
60
  def helper_rag(text):
61
- docs_out = vectordb.similarity_search_with_relevance_scores(text,k=5)
 
62
  context = ""
63
  for doc in docs_out:
64
- if doc[1] >= 0.7:
65
- context += doc[0].page_content + "\n"
66
-
67
- if context == "" and len(docs_out) > 0:
68
- context = docs_out[0][0].page_content + "\n"
69
 
70
  return context
71
 
@@ -99,8 +97,10 @@ def helper_text(text_input,radio=None):
99
 
100
  return output[0]
101
 
 
102
  # Gradio function for configure the language of UI
103
- def change_language(radio,text_input,text_output,clear_btn,submit_btn,markdown, markdown_msg1, markdown_msg2):
 
104
  if radio == "简体中文":
105
  index = 0
106
  text_input_update=gr.Textbox.update(value = chinese_converter.to_simplified(text_input), label = text_input_label[index])
@@ -122,6 +122,7 @@ def change_language(radio,text_input,text_output,clear_btn,submit_btn,markdown,
122
  markdown_update=gr.Markdown.update(value = markdown)
123
  markdown_msg1_update=gr.Markdown.update(value = markdown_msg1)
124
  markdown_msg2_update=gr.Markdown.update(value = markdown_msg2)
 
125
  else:
126
  index = 0
127
  text_input_update=gr.Textbox.update(label = text_input_label[index])
@@ -133,7 +134,8 @@ def change_language(radio,text_input,text_output,clear_btn,submit_btn,markdown,
133
  clear_btn_update = gr.ClearButton.update(value = clear_label[index])
134
  submit_btn_update = gr.Button.update(value = submit_label[index])
135
 
136
- return [text_input_update,text_output_update,clear_btn_update,submit_btn_update,markdown_update, markdown_msg1_update ,markdown_msg2_update ]
 
137
 
138
 
139
  def clear_text():
@@ -182,6 +184,7 @@ with gr.Blocks() as demo:
182
 
183
  text_output = gr.Textbox(label=text_output_label[index])
184
 
 
185
  examples = gr.Examples(
186
  examples=example_list,
187
  inputs=text_input,
@@ -234,7 +237,7 @@ with gr.Blocks() as demo:
234
  该模型是以 [apache-2.0](https://www.apache.org/licenses/LICENSE-2.0) 发行。
235
 
236
  This application utilizes [text2vec-base-chinese](https://huggingface.co/shibing624/text2vec-base-chinese) to generate text vector embeddings.
237
- The model is released under [apache-2.0](https://www.apache.org/licenses/LICENSE-2.0).
238
  """)
239
 
240
 
@@ -243,14 +246,17 @@ with gr.Blocks() as demo:
243
 
244
 
245
  submit_btn.click(fn=helper_text, inputs=[text_input,radio], outputs=text_output)
 
246
  clear_btn.click(fn=clear_text,outputs=[text_input,text_output])
247
- radio.change(fn=change_language,inputs=[radio,text_input,text_output,clear_btn,submit_btn,markdown, markdown_msg1,markdown_msg2],
248
- outputs=[text_input,text_output,clear_btn,submit_btn, markdown, markdown_msg1,markdown_msg2])
 
 
249
 
250
  #demo = gr.Interface(fn=helper_text, inputs=text_input, outputs=text_output,
251
  # flagging_options=["Inappropriate"],allow_flagging="never",
252
  # title="aaa",description="aaa",article="aaa")
253
- demo.launch(show_api=False)
254
 
255
  # %%
256
 
 
29
  encode_kwargs = encode_kwargs
30
  )
31
 
32
+
33
  # %%
34
  persist_directory = 'chroma/'
35
  vectordb = Chroma(embedding_function=huggingface_embeddings,persist_directory=persist_directory)
 
59
 
60
  # helper function for RAG
61
  def helper_rag(text):
62
+ #docs_out = vectordb.similarity_search_with_relevance_scores(text,k=5)
63
+ docs_out = vectordb.max_marginal_relevance_search(text,k=5,fetch_k = 20, lambda_mult = 0.5)
64
  context = ""
65
  for doc in docs_out:
66
+ context += doc.page_content + "\n"
 
 
 
 
67
 
68
  return context
69
 
 
97
 
98
  return output[0]
99
 
100
+
101
  # Gradio function for configure the language of UI
102
+ def change_language(radio,text_input,text_output,clear_btn,submit_btn,markdown,
103
+ markdown_msg1, markdown_msg2):
104
  if radio == "简体中文":
105
  index = 0
106
  text_input_update=gr.Textbox.update(value = chinese_converter.to_simplified(text_input), label = text_input_label[index])
 
122
  markdown_update=gr.Markdown.update(value = markdown)
123
  markdown_msg1_update=gr.Markdown.update(value = markdown_msg1)
124
  markdown_msg2_update=gr.Markdown.update(value = markdown_msg2)
125
+
126
  else:
127
  index = 0
128
  text_input_update=gr.Textbox.update(label = text_input_label[index])
 
134
  clear_btn_update = gr.ClearButton.update(value = clear_label[index])
135
  submit_btn_update = gr.Button.update(value = submit_label[index])
136
 
137
+ return [text_input_update,text_output_update,clear_btn_update,submit_btn_update,markdown_update,
138
+ markdown_msg1_update ,markdown_msg2_update]
139
 
140
 
141
  def clear_text():
 
184
 
185
  text_output = gr.Textbox(label=text_output_label[index])
186
 
187
+
188
  examples = gr.Examples(
189
  examples=example_list,
190
  inputs=text_input,
 
237
  该模型是以 [apache-2.0](https://www.apache.org/licenses/LICENSE-2.0) 发行。
238
 
239
  This application utilizes [text2vec-base-chinese](https://huggingface.co/shibing624/text2vec-base-chinese) to generate text vector embeddings.
240
+ The model is released under [apache-2.0](https://www.apache.org/licenses/LICENSE-2.0)
241
  """)
242
 
243
 
 
246
 
247
 
248
  submit_btn.click(fn=helper_text, inputs=[text_input,radio], outputs=text_output)
249
+
250
  clear_btn.click(fn=clear_text,outputs=[text_input,text_output])
251
+ radio.change(fn=change_language,inputs=[radio,text_input,text_output,clear_btn,submit_btn,
252
+ markdown, markdown_msg1,markdown_msg2],
253
+ outputs=[text_input,text_output,clear_btn,submit_btn,
254
+ markdown, markdown_msg1,markdown_msg2])
255
 
256
  #demo = gr.Interface(fn=helper_text, inputs=text_input, outputs=text_output,
257
  # flagging_options=["Inappropriate"],allow_flagging="never",
258
  # title="aaa",description="aaa",article="aaa")
259
+ demo.launch()
260
 
261
  # %%
262