|
|
|
import gradio as gr |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
from scipy.special import softmax |
|
import nltk |
|
import re |
|
from nltk.corpus import stopwords |
|
from nltk.stem import WordNetLemmatizer |
|
|
|
|
|
|
|
nltk.download('stopwords') |
|
nltk.download('wordnet') |
|
|
|
|
|
model_path = "rasmodev/Covid-19_Sentiment_Analysis_RoBERTa_Model" |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_path) |
|
|
|
|
|
def preprocess(text): |
|
|
|
text = text.lower() |
|
|
|
|
|
text = re.sub(r'[^a-zA-Z\s]', '', text) |
|
|
|
|
|
stop_words = set(stopwords.words('english')) |
|
words = text.split() |
|
words = [word for word in words if word not in stop_words] |
|
|
|
|
|
lemmatizer = WordNetLemmatizer() |
|
words = [lemmatizer.lemmatize(word) for word in words] |
|
|
|
|
|
processed_text = ' '.join(words) |
|
|
|
|
|
new_text = [] |
|
for t in processed_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 sentiment_analysis(text): |
|
text = preprocess(text) |
|
|
|
|
|
inputs = tokenizer(text, return_tensors='pt') |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
scores_ = outputs.logits[0].detach().numpy() |
|
scores_ = softmax(scores_) |
|
|
|
|
|
labels = ['Negative', 'Neutral', 'Positive'] |
|
colors = ['red', 'yellow', 'green'] |
|
font_colors = ['white', 'black', 'white'] |
|
|
|
|
|
max_label = labels[scores_.argmax()] |
|
max_percentage = scores_.max() * 100 |
|
|
|
|
|
label_html = f'<div style="display: flex; justify-content: center;"><button style="text-align: center; font-size: 16px; padding: 10px; border-radius: 15px; background-color: {colors[labels.index(max_label)]}; color: {font_colors[labels.index(max_label)]};">{max_label}({max_percentage:.2f}%)</button></div>' |
|
|
|
return label_html |
|
|
|
|
|
interface = gr.Interface( |
|
fn=sentiment_analysis, |
|
inputs=gr.Textbox(placeholder="Write your tweet here..."), |
|
outputs=gr.HTML(), |
|
title="COVID-19 Sentiment Analysis App", |
|
description="This App Analyzes the sentiment of COVID-19 related tweets. Negative: Indicates a negative sentiment, Neutral: Indicates a neutral sentiment, Positive: Indicates a positive sentiment.", |
|
theme="default", |
|
layout="horizontal", |
|
examples=[ |
|
["Covid vaccines are irrelevant"], |
|
["The Vaccine is Good I have had no issues!"] |
|
] |
|
) |
|
|
|
|
|
interface.launch() |