File size: 1,306 Bytes
f460415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
from sentence_transformers import SentenceTransformer

model_name = "BAAI/bge-large-zh-v1.5"
model = SentenceTransformer(model_name, device="cpu")


def cal_sim(intent, cand1, cand2, cand3, cand4, cand5):
    cand_list = [cand1, cand2, cand3, cand4, cand5]
    cand_list = [cand for cand in cand_list if cand]
    embeddings_1 = model.encode([intent], normalize_embeddings=True)
    embeddings_2 = model.encode(cand_list, normalize_embeddings=True)
    similarity = embeddings_1 @ embeddings_2.T
    similarity = similarity[0]
    sim_output = {}
    for i, sim in zip(cand_list, similarity):
        if i:
            sim_output[i] = float(sim)
    return sim_output

demo = gr.Interface(fn=cal_sim,
                    inputs=[gr.components.Textbox(label="User query"),
                            gr.components.Textbox(label="candidate01"),
                            gr.components.Textbox(label="candidate02"),
                            gr.components.Textbox(label="candidate03"),
                            gr.components.Textbox(label="candidate04"),
                            gr.components.Textbox(label="candidate05"),
                            ],
                    outputs=gr.components.Label())
    
if __name__ == "__main__":
    demo.launch(share=True, debug=True)