bambadij's picture
update
10bc06b
import gradio as gr
from scipy.special import softmax
from transformers import AutoTokenizer, AutoConfig
from transformers import AutoModelForSequenceClassification
import numpy as np
#Setup
model_path = f"bambadij/Tweet_sentiment_analysis_Distilbert"
tokenizer = AutoTokenizer.from_pretrained(model_path)
config = AutoConfig.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
#Function
# 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)
# Input preprocessing
text = "Covid cases are increasing fast!"
text = preprocess(text)
# PyTorch-based models
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
def sentiment_analysis(text):
text =preprocess(text)
#Pytorch-based models
encoded_input = tokenizer(text,return_tensors='pt')
output = model(**encoded_input)
scores_ = output[0][0].detach().numpy()
scores_ =softmax(scores_)
#Foramt ouptput dict of scores
labels =['Negative','Neutral','Positive']
scores = {l:float(s) for (l,s) in zip(labels,scores_)}
return scores
demo = gr.Interface(
fn=sentiment_analysis,
inputs=gr.Textbox(placeholder="Copy and paste /Write a tweet her..."),
outputs="label",
examples=[["what's up with the vaccine"],
["Covid cases are increasing fast!"],
["Covid has been invented by Issa"],
["I have a covid"],
["All the people are sick maybe it's covid"],
],
title="Covid 19 vaccin Sentiment Analysis App",
description = "This Aplication assesses if a twitter post relating vaccination is positive"
)
if __name__ == "__main__":
demo.launch()