MixoMax commited on
Commit
33d0cf1
1 Parent(s): f1cd8f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import tensorflow_hub as hub
3
+ from tensorflow_text import SentencepieceTokenizer
4
+ import gradio as gr
5
+ import math
6
+
7
+ model_url = "https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3"
8
+ model = hub.load(model_url)
9
+
10
+ def embed_text(text: str) -> dict:
11
+ embeddings = model(text)
12
+ return embeddings.numpy().tolist()
13
+
14
+ embed_text_inter = gr.Interface(
15
+ fn = embed_text,
16
+ inputs = "text",
17
+ outputs = gr.JSON(),
18
+ title = "Universal Sentence Encoder 3 Large"
19
+ )
20
+
21
+ def distance(text_1: str, text_2: str) -> float:
22
+ embeddings_1 = model(text_1)
23
+ embeddings_2 = model(text_2)
24
+ dist = math.sqrt(sum((embeddings_1 - embeddings_2)**2))
25
+ return dist
26
+
27
+ distance_inter = gr.Interface(
28
+ fn = distance,
29
+ inputs = ["text", "text"],
30
+ outputs = "number",
31
+ title = "Universal Sentence Encoder 3 Large"
32
+ )
33
+
34
+
35
+ iface = gr.TabbedInterface(
36
+ interface_list=[embed_text_inter, distance_inter],
37
+ title="Universal Sentence Encoder 3 Large"
38
+ )
39
+
40
+ iface.launch()