File size: 1,375 Bytes
8d0e09f
 
 
 
 
 
 
 
aa01698
8d0e09f
 
 
 
 
 
 
 
 
 
 
058e4f4
 
8d0e09f
 
 
 
 
 
 
 
ceeed84
 
8d0e09f
2e8db20
 
8d0e09f
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
27
28
29
30
31
32
33
34
35
36
# 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
import tensorflow as tf
import tensorflow_text as text  # Registers the ops. (needed for "bert_preprocessor")
from hub.tensorflow_hub.hf_utils import pull_from_hub


def model_fn(preprocessor, encoder):
    text_input = tf.keras.layers.Input(shape=(), dtype=tf.string)
    encoder_inputs = preprocessor(text_input)
    outputs = encoder(encoder_inputs)
    pooled_output = outputs["pooled_output"]
    return tf.keras.Model(text_input, pooled_output)

def predict_fn(text):
    print(text)
    print(tf.constant([text]))
    embed = model(tf.constant([text]))[0].numpy()
    return embed

preprocessor = pull_from_hub(repo_id="Dimitre/bert_en_cased_preprocess")
encoder = pull_from_hub(repo_id="Dimitre/bert_en_cased_L-12_H-768_A-12")
model = model_fn(preprocessor, encoder)

iface = gr.Interface(fn=predict_fn, 
                     title="BERT sentence embeddings", 
                     description="Get the embeddings from your sentences using BERT", 
                     inputs=gr.Textbox(lines=2, placeholder="Text input here...", label="Text"), 
                     outputs="text", 
                     examples=[["Hello! This is a random sentence"]])
iface.launch()