Spaces:
Runtime error
Runtime error
import gradio as gr | |
from sentence_transformers import SentenceTransformer | |
texts1 = ["胡子长得太快怎么办?", "在香港哪里买手表好"] | |
texts2 = ["胡子长得快怎么办?", "怎样使胡子不浓密!", "香港买手表哪里好", "在杭州手机到哪里买"] | |
demo_text = "" | |
model = SentenceTransformer('DMetaSoul/Dmeta-embedding') | |
embs1 = model.encode(texts1, normalize_embeddings=True) | |
embs2 = model.encode(texts2, normalize_embeddings=True) | |
similarity = embs1 @ embs2.T | |
demo_text += str(similarity) | |
demo_text += "\n" | |
for i in range(len(texts1)): | |
scores = [] | |
for j in range(len(texts2)): | |
scores.append([texts2[j], similarity[i][j]]) | |
scores = sorted(scores, key=lambda x:x[1], reverse=True) | |
print(f"查询文本:{texts1[i]}") | |
demo_text += f"查询文本:{texts1[i]}" | |
demo_text += "\n" | |
for text2, score in scores: | |
print(f"相似文本:{text2},打分:{score}") | |
demo_text += f"相似文本:{text2},打分:{score}" | |
demo_text += "\n" | |
print() | |
# gr.load("models/DMetaSoul/Dmeta-embedding-zh").launch() | |
with gr.Row(): | |
gr.Markdown(demo_text) | |
gr.launch() | |