51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
import gradio as gr
|
|
from transformers import AutoModelForSequenceClassification
|
|
from transformers import AutoTokenizer, AutoConfig
|
|
import numpy as np
|
|
from scipy.special import softmax
|
|
# Preprocess text (username and link placeholders)
|
|
def preprocess(text):
|
|
new_text = []
|
|
for t in text.split(" "):
|
|
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
|
t = 'http' if t.startswith('http') else t
|
|
new_text.append(t)
|
|
return " ".join(new_text)
|
|
|
|
def inference(text):
|
|
model_path="twitter-roberta-base-sentiment-latest"
|
|
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
config = AutoConfig.from_pretrained(model_path)
|
|
# PT
|
|
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
|
|
|
text = preprocess(text)
|
|
encoded_input = tokenizer(text, return_tensors='pt')
|
|
output = model(**encoded_input)
|
|
scores = output[0][0].detach().numpy()
|
|
scores = softmax(scores)
|
|
|
|
ranking = np.argsort(scores)
|
|
ranking = ranking[::-1]
|
|
for i in range(scores.shape[0]):
|
|
l = config.id2label[ranking[i]]
|
|
s = scores[ranking[i]]
|
|
return f"{i+1}) {l} {np.round(float(s), 4)}"
|
|
|
|
|
|
title = "sentiment analysis:twitter-roberta-base-sentiment"
|
|
description = "这是twitter-roberta-base-sentiment的Gradio Demo。 上传你想要的图像或者点击下面的示例来加载它。"
|
|
article = "<p style='text-align: center'><a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>Github Repo Pytorch</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_animegan' alt='visitor badge'></center></p>"
|
|
examples=[["Covid cases are increasing fast!"]]
|
|
|
|
demo = gr.Interface(
|
|
fn=inference,
|
|
inputs=[gr.inputs.Textbox()],
|
|
outputs=gr.outputs.Textbox(),
|
|
title=title,
|
|
description=description,
|
|
article=article,
|
|
examples=examples)
|
|
|
|
demo.launch(server_name="0.0.0.0")
|