File size: 695 Bytes
1f91fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from transformers import T5ForConditionalGeneration, T5Tokenizer
import gradio as gr

model_name = "t5-small"
text2text_tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)

def text2text_sentiment(text):
    input = "Sentiment analysis for this sentence:" + text
    encoded = text2text_tokenizer(input, return_tensors="pt")
    tokens = model.generate(**encoded)
    response = text2text_tokenizer.batch_decode(tokens)
    return response

in_para = gr.Textbox(lines=1, label="English text", placeholder="Text in English")
out = gr.Textbox(lines=1, label="Sentiment")

gr.Interface(text2text_sentiment, inputs=in_para, outputs=out)