From 16917ed1191c4c0eccc4b1ca11ba1f7f79bcb81d Mon Sep 17 00:00:00 2001 From: SOULOFCINDER <841135647@qq.com> Date: Thu, 30 Mar 2023 16:50:48 +0800 Subject: [PATCH] inital --- twitter_roberta_base_sentiment/app.py | 51 +++++++++++++++++++ .../twitter-roberta-base-sentiment-latest | 1 + 2 files changed, 52 insertions(+) create mode 100644 twitter_roberta_base_sentiment/app.py create mode 160000 twitter_roberta_base_sentiment/twitter-roberta-base-sentiment-latest diff --git a/twitter_roberta_base_sentiment/app.py b/twitter_roberta_base_sentiment/app.py new file mode 100644 index 0000000..f031c34 --- /dev/null +++ b/twitter_roberta_base_sentiment/app.py @@ -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 = "