Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.py
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1SKjRNc67_9TZPKUGhtfiYMfcpZuMh6s0
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 11 |
+
# %pip install gradio transformers -q
|
| 12 |
+
|
| 13 |
+
# Import the key libraries
|
| 14 |
+
import gradio as gr
|
| 15 |
+
import torch
|
| 16 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 17 |
+
from scipy.special import softmax
|
| 18 |
+
|
| 19 |
+
# Load the tokenizer and model from Hugging Face
|
| 20 |
+
model_path = "rasmodev/Covid-19_Sentiment_Analysis_BERT_Model"
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 22 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 23 |
+
|
| 24 |
+
# Preprocess text (username and link placeholders)
|
| 25 |
+
def preprocess(text):
|
| 26 |
+
new_text = []
|
| 27 |
+
for t in text.split(" "):
|
| 28 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
| 29 |
+
t = 'http' if t.startswith('http') else t
|
| 30 |
+
new_text.append(t)
|
| 31 |
+
return " ".join(new_text)
|
| 32 |
+
|
| 33 |
+
# Perform sentiment analysis
|
| 34 |
+
def sentiment_analysis(text):
|
| 35 |
+
text = preprocess(text)
|
| 36 |
+
|
| 37 |
+
# Tokenize input text
|
| 38 |
+
inputs = tokenizer(text, return_tensors='pt')
|
| 39 |
+
|
| 40 |
+
# Forward pass through the model
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
outputs = model(**inputs)
|
| 43 |
+
|
| 44 |
+
# Get predicted probabilities
|
| 45 |
+
scores_ = outputs.logits[0].detach().numpy()
|
| 46 |
+
scores_ = softmax(scores_)
|
| 47 |
+
|
| 48 |
+
# Define labels and corresponding colors
|
| 49 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
| 50 |
+
colors = ['red', 'yellow', 'green']
|
| 51 |
+
font_colors = ['white', 'black', 'white']
|
| 52 |
+
|
| 53 |
+
# Find the label with the highest percentage
|
| 54 |
+
max_label = labels[scores_.argmax()]
|
| 55 |
+
max_percentage = scores_.max() * 100
|
| 56 |
+
|
| 57 |
+
# Create HTML for the label with the specified style
|
| 58 |
+
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>'
|
| 59 |
+
|
| 60 |
+
return label_html
|
| 61 |
+
|
| 62 |
+
# Create a Gradio interface
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=sentiment_analysis,
|
| 65 |
+
inputs=gr.Textbox(placeholder="Write your tweet here..."),
|
| 66 |
+
outputs=gr.HTML(),
|
| 67 |
+
title="COVID-19 Sentiment Analysis App",
|
| 68 |
+
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.",
|
| 69 |
+
theme="default",
|
| 70 |
+
layout="horizontal",
|
| 71 |
+
examples=[
|
| 72 |
+
["This vaccine is terrible!"],
|
| 73 |
+
["I don't have a strong opinion about this vaccines."],
|
| 74 |
+
["The Vaccine is Good I have had no issues!"]
|
| 75 |
+
]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Launch the Gradio app
|
| 79 |
+
if __name__ == '__main__':
|
| 80 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|