File size: 1,232 Bytes
32b0b31
f91fe8a
32b0b31
f91fe8a
 
32b0b31
00dd6df
f91fe8a
00dd6df
 
 
 
 
 
 
0957d59
bdeb50c
00dd6df
0957d59
97199d0
 
0957d59
 
dee456b
 
00dd6df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Workaround to install the lib without "setup.py"
import sys
from git import Repo
Repo.clone_from("https://github.com/dimitreOliveira/hub.git", "./hub")
sys.path.append("/hub")

import gradio as gr
from hub.tensorflow_hub.hf_utils import pull_from_hub
from sklearn.metrics.pairwise import cosine_similarity

model = pull_from_hub(repo_id="Dimitre/universal-sentence-encoder")

def get_similarity(sentence_a, sentence_b):
    embed_a = model([sentence_a])
    embed_b = model([sentence_b])
    similarity = cosine_similarity(embed_a, embed_b)[0][0]
    return f'The similarity score between sentence A and sentence B is: "{similarity:.2f}"'

iface = gr.Interface(fn=get_similarity, 
                     title="Sentence similarity", 
                     description="Measure the similarity between sentences using universal-sentence-encoder", 
                     inputs=[gr.Textbox(lines=2, placeholder="Sentence A here...", label="Sentence A"), 
                             gr.Textbox(lines=2, placeholder="Sentence B here...", label="Sentence B")], 
                     outputs="text",
                     examples=[["Hello! This is a random sentence", "Hello! This is a slightly different random sentence"]])
iface.launch()