Spaces:
Sleeping
Sleeping
File size: 1,727 Bytes
f460415 45c2f2e fd727eb f460415 fd727eb f460415 fd727eb f460415 45c2f2e fd727eb 45c2f2e fd727eb 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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(*args):
intent = args[0]
cand_list = args[1:]
cand_list = [cand for cand in cand_list if cand]
sim_output = {}
if not cand_list:
return sim_output
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]
for i, sim in zip(cand_list, similarity):
if i:
sim_output[i] = float(sim)
return sim_output
with gr.Blocks(title="意圖相似度計算") as demo:
gr.Markdown(
"""
按 Calculate 計算 user query與 candidate list之間的相似度。
"""
)
# Row 1: Buttons
with gr.Row():
submit_button = gr.Button("Calculate")
clear_button = gr.Button("Clear")
# Row 2: Inputs and Output Side by Side
with gr.Row():
# Left column: User input and candidates
with gr.Column():
user_query = gr.Textbox(label="User Query")
candidate_boxes = [gr.Textbox(label=f"Candidate {i+1}") for i in range(30)]
# Right column: Output label
with gr.Column():
output_label = gr.Label(label="Similarity Results")
# Link buttons to functions
inputs = [user_query] + candidate_boxes
submit_button.click(fn=cal_sim, inputs=inputs, outputs=output_label)
clear_button.click(lambda: (None,) * 31, inputs=[], outputs=inputs)
# Launch the app
if __name__ == "__main__":
demo.launch(share=True, debug=True)
|