Spaces:
Runtime error
Runtime error
antagonico
commited on
Commit
路
a8efbc5
1
Parent(s):
314e98e
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,31 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
# Carga el modelo
|
7 |
+
model = SentenceTransformer('Maite89/Roberta_finetuning_semantic_similarity_stsb_multi_mt')
|
8 |
|
9 |
+
# Funci贸n para obtener embeddings del modelo
|
10 |
+
def get_embeddings(sentences):
|
11 |
+
embeddings = model.encode(sentences, show_progress_bar=False)
|
12 |
+
return np.array(embeddings)
|
13 |
+
|
14 |
+
# Funci贸n para comparar las sentencias
|
15 |
+
def compare(source_sentence, compare_sentence):
|
16 |
+
# Calcula la similitud
|
17 |
+
embeddings = get_embeddings([source_sentence, compare_sentence])
|
18 |
+
similarity = float(cosine_similarity(embeddings[0].reshape(1, -1), embeddings[1].reshape(1, -1))[0][0]) # Convert the numpy.float32 to Python float
|
19 |
+
|
20 |
+
return similarity
|
21 |
+
|
22 |
+
# Define las interfaces de entrada y salida de Gradio
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=compare,
|
25 |
+
inputs=["text", "text"],
|
26 |
+
outputs="number",
|
27 |
+
live=True
|
28 |
+
)
|
29 |
+
|
30 |
+
# Inicia la interfaz de Gradio
|
31 |
iface.launch()
|