Spaces:
Runtime error
Runtime error
import gradio as gr | |
from sentence_transformers import SentenceTransformer, util | |
model = None | |
def semantic(company_1, company_2): | |
global model | |
# Single list of sentences | |
sentences = [company_1, company_2] | |
if model is None: | |
model = SentenceTransformer('all-mpnet-base-v2') | |
#Compute embeddings | |
embeddings = model.encode(sentences, convert_to_tensor=True) | |
#Compute cosine-similarities for each sentence with each other sentence | |
cosine_scores = util.cos_sim(embeddings, embeddings) | |
#Find the pairs with the highest cosine similarity scores | |
pairs = [] | |
for i in range(len(cosine_scores)-1): | |
for j in range(i+1, len(cosine_scores)): | |
pairs.append({'index': [i, j], 'score': cosine_scores[i][j]}) | |
#Sort scores in decreasing order | |
pairs = sorted(pairs, key=lambda x: x['score'], reverse=True) | |
for pair in pairs: | |
return "{:.4f}".format(pair['score']) | |
company_1 = "Growth Capital Acquisition Corp" | |
company_2 = "Growth Capital Acquisition Corp III" | |
title = 'sentences_semantic' | |
gr.Interface(semantic,inputs=[gr.inputs.Textbox(lines=1, default=company_1, label="Company_1"), gr.inputs.Textbox(lines=1, default=company_2, label="Company_2")], | |
outputs=[gr.outputs.Textbox(type="auto",label="Score")],title = title).launch() |