Spaces:
Runtime error
Runtime error
import gradio as gr | |
from sentence_transformers import SentenceTransformer | |
from sklearn.metrics.pairwise import cosine_similarity | |
import numpy as np | |
# Carga el modelo | |
model = SentenceTransformer('Maite89/Roberta_finetuning_semantic_similarity_stsb_multi_mt') | |
# Funci贸n para obtener embeddings del modelo | |
def get_embeddings(sentences): | |
embeddings = model.encode(sentences, show_progress_bar=False) | |
return np.array(embeddings) | |
# Funci贸n para comparar las sentencias | |
def compare(source_sentence, compare_sentence): | |
# Calcula la similitud | |
embeddings = get_embeddings([source_sentence, compare_sentence]) | |
similarity = float(cosine_similarity(embeddings[0].reshape(1, -1), embeddings[1].reshape(1, -1))[0][0]) # Convert the numpy.float32 to Python float | |
return similarity | |
# Define las interfaces de entrada y salida de Gradio | |
iface = gr.Interface( | |
fn=compare, | |
inputs=["text", "text"], | |
outputs="number", | |
live=True | |
) | |
# Inicia la interfaz de Gradio | |
iface.launch() | |