Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,29 +8,50 @@ model = SentenceTransformer(model_name, device="cpu")
|
|
| 8 |
def cal_sim(*args):
|
| 9 |
intent = args[0]
|
| 10 |
cand_list = args[1:]
|
| 11 |
-
|
| 12 |
cand_list = [cand for cand in cand_list if cand]
|
|
|
|
|
|
|
|
|
|
| 13 |
embeddings_1 = model.encode([intent], normalize_embeddings=True)
|
| 14 |
embeddings_2 = model.encode(cand_list, normalize_embeddings=True)
|
| 15 |
similarity = embeddings_1 @ embeddings_2.T
|
| 16 |
similarity = similarity[0]
|
| 17 |
-
|
| 18 |
for i, sim in zip(cand_list, similarity):
|
| 19 |
if i:
|
| 20 |
sim_output[i] = float(sim)
|
| 21 |
return sim_output
|
| 22 |
|
| 23 |
|
| 24 |
-
|
| 25 |
-
gr.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
demo.launch(share=True, debug=True)
|
|
|
|
| 8 |
def cal_sim(*args):
|
| 9 |
intent = args[0]
|
| 10 |
cand_list = args[1:]
|
| 11 |
+
|
| 12 |
cand_list = [cand for cand in cand_list if cand]
|
| 13 |
+
sim_output = {}
|
| 14 |
+
if not cand_list:
|
| 15 |
+
return sim_output
|
| 16 |
embeddings_1 = model.encode([intent], normalize_embeddings=True)
|
| 17 |
embeddings_2 = model.encode(cand_list, normalize_embeddings=True)
|
| 18 |
similarity = embeddings_1 @ embeddings_2.T
|
| 19 |
similarity = similarity[0]
|
| 20 |
+
|
| 21 |
for i, sim in zip(cand_list, similarity):
|
| 22 |
if i:
|
| 23 |
sim_output[i] = float(sim)
|
| 24 |
return sim_output
|
| 25 |
|
| 26 |
|
| 27 |
+
with gr.Blocks(title="意圖相似度計算") as demo:
|
| 28 |
+
gr.Markdown(
|
| 29 |
+
"""
|
| 30 |
+
按 Calculate 計算 user query與 candidate list之間的相似度。
|
| 31 |
+
"""
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Row 1: Buttons
|
| 35 |
+
with gr.Row():
|
| 36 |
+
submit_button = gr.Button("Calculate")
|
| 37 |
+
clear_button = gr.Button("Clear")
|
| 38 |
+
|
| 39 |
+
# Row 2: Inputs and Output Side by Side
|
| 40 |
+
with gr.Row():
|
| 41 |
+
# Left column: User input and candidates
|
| 42 |
+
with gr.Column():
|
| 43 |
+
user_query = gr.Textbox(label="User Query")
|
| 44 |
+
candidate_boxes = [gr.Textbox(label=f"Candidate {i+1}") for i in range(30)]
|
| 45 |
+
|
| 46 |
+
# Right column: Output label
|
| 47 |
+
with gr.Column():
|
| 48 |
+
output_label = gr.Label(label="Similarity Results")
|
| 49 |
+
|
| 50 |
+
# Link buttons to functions
|
| 51 |
+
inputs = [user_query] + candidate_boxes
|
| 52 |
+
submit_button.click(fn=cal_sim, inputs=inputs, outputs=output_label)
|
| 53 |
+
clear_button.click(lambda: (None,) * 31, inputs=[], outputs=inputs)
|
| 54 |
|
| 55 |
+
# Launch the app
|
| 56 |
if __name__ == "__main__":
|
| 57 |
demo.launch(share=True, debug=True)
|