This commit is contained in:
SOULOFCINDER 2023-03-30 16:50:48 +08:00
commit 16917ed119
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,51 @@
import gradio as gr
from transformers import AutoModelForSequenceClassification
from transformers import TFAutoModelForSequenceClassification
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 = "Gradio Demo for twitter-roberta-base-sentiment. To use it, simply upload your image, or click one of the examples to load them."
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()

@ -0,0 +1 @@
Subproject commit f8f82141e1c60fb0869fc353c1e84a944d4f920c