File size: 4,204 Bytes
876e501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d5ce1d
876e501
 
 
 
 
 
 
 
 
 
 
 
 
5d5ce1d
876e501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d5ce1d
876e501
 
9b28fe7
 
876e501
 
 
 
 
 
 
 
 
 
 
9b28fe7
876e501
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import gradio as gr
from huggingface_hub import snapshot_download

import os  # utility library

# libraries to load the model and serve inference
import tensorflow_text
import tensorflow as tf

CACHE_DIR = "hfhub_cache"  # where the library's fork would be stored once downloaded
if not os.path.exists(CACHE_DIR):
    os.mkdir(CACHE_DIR)

# download the files from huggingface repo and load the model with tensorflow
snapshot_download(repo_id="stevekola/T5", cache_dir=CACHE_DIR)
saved_model_path = os.path.join(CACHE_DIR, os.listdir(CACHE_DIR)[0])
model = tf.saved_model.load(saved_model_path, ["serve"])

title = "Interactive demo: T5 Multitasking Demo"
description = "Demo for T5's different tasks including machine translation, \
    text summarization, document similarity, and grammatical correctness of sentences."


def predict_fn(x):
    """Function to get inferences from model on live data points.
    params:
        x    input text to run get output on
    returns:
        a numpy array representing the output
    """
    return model.signatures['serving_default'](tf.constant(x))['outputs'].numpy()


def predict(task_type, sentence):
    """Function to parse the user inputs, run the parsed text through the
    model and return output in a readable format.
    params:
        task_type   sentence representing the type of task to run on T5 model
        sentence    sentence to get inference on
    returns:
        text decoded into a human-readable format.
    """
    task_dict = {
        "Translate English to French": "Translate English to French",
        "Translate English to German": "Translate English to German",
        "Translate English to Romanian": "Translate English to Romanian",
        "Grammatical Correctness of Sentence": "cola sentence",
        "Text Summarization": "summarize",
        "Document Similarity Score (separate the 2 sentences with 3 dashes `---`)": "stsb",
    }
    question = f"{task_dict[task_type]}: {sentence}"  # parsing the user inputs into a format recognized by T5

    # Document Similarity takes in two sentences so it has to be parsed in a separate manner
    if task_type.startswith("Document Similarity"):
        sentences = sentence.split('---')
        question = f"{task_dict[task_type]} sentence1: {sentences[0]} sentence2: {sentences[1]}"

    return predict_fn([question])[0].decode('utf-8')


iface = gr.Interface(fn=predict,
                     inputs=[gr.inputs.Radio(
                         choices=["Translate English to French",
                                  "Translate English to German",
                                  "Translate English to Romanian",
                                  "Grammatical Correctness of Sentence",
                                  "Text Summarization",                                  
                                  "Document Similarity Score (separate the 2 sentences with 3 dashes `---`)"],
                         label="Task Type"
                     ),
                         gr.inputs.Textbox(label="Sentence")],
                     outputs="text",
                     title=title,
                     description=description,
                     examples=[
                         ["Translate English to French", "I am Steven and I live in Lagos, Nigeria"],
                         ["Translate English to German", "I am Steven and I live in Lagos, Nigeria"],
                         ["Translate English to Romanian", "I am Steven and I live in Lagos, Nigeria"],
                         ["Grammatical Correctness of Sentence", "I am Steven and I live in Lagos, Nigeria"],
                         ["Text Summarization",
                          "I don't care about those doing the comparison, but comparing the Ghanaian Jollof Rice to \
                          Nigerian Jollof Rice is an insult to Nigerians"],
                         ["Document Similarity Score (separate the 2 sentences with 3 dashes `---`)",
                          "I reside in the commercial capital city of Nigeria, which is Lagos---I live in Lagos"],
                     ],
                     theme="huggingface",
                     enable_queue=True)

iface.launch()