Spaces:
Sleeping
Sleeping
Vincent Wu
commited on
Commit
·
f460415
1
Parent(s):
7027233
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
|
| 4 |
+
model_name = "BAAI/bge-large-zh-v1.5"
|
| 5 |
+
model = SentenceTransformer(model_name, device="cpu")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def cal_sim(intent, cand1, cand2, cand3, cand4, cand5):
|
| 9 |
+
cand_list = [cand1, cand2, cand3, cand4, cand5]
|
| 10 |
+
cand_list = [cand for cand in cand_list if cand]
|
| 11 |
+
embeddings_1 = model.encode([intent], normalize_embeddings=True)
|
| 12 |
+
embeddings_2 = model.encode(cand_list, normalize_embeddings=True)
|
| 13 |
+
similarity = embeddings_1 @ embeddings_2.T
|
| 14 |
+
similarity = similarity[0]
|
| 15 |
+
sim_output = {}
|
| 16 |
+
for i, sim in zip(cand_list, similarity):
|
| 17 |
+
if i:
|
| 18 |
+
sim_output[i] = float(sim)
|
| 19 |
+
return sim_output
|
| 20 |
+
|
| 21 |
+
demo = gr.Interface(fn=cal_sim,
|
| 22 |
+
inputs=[gr.components.Textbox(label="User query"),
|
| 23 |
+
gr.components.Textbox(label="candidate01"),
|
| 24 |
+
gr.components.Textbox(label="candidate02"),
|
| 25 |
+
gr.components.Textbox(label="candidate03"),
|
| 26 |
+
gr.components.Textbox(label="candidate04"),
|
| 27 |
+
gr.components.Textbox(label="candidate05"),
|
| 28 |
+
],
|
| 29 |
+
outputs=gr.components.Label())
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
demo.launch(share=True, debug=True)
|