Spaces:
Sleeping
Sleeping
Uploaded required files
Browse files- app.py +60 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Sentiment Analysis App.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1t6wAnMPDdEHuioRZofR8_JEPrzuT7KAJ
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Import the required Libraries
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import numpy as np
|
| 15 |
+
import transformers
|
| 16 |
+
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification, TFAutoModelForSequenceClassification
|
| 17 |
+
from scipy.special import softmax
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Requirements
|
| 21 |
+
model_path = "Queensly/finetuned_albert_base_v2"
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 23 |
+
config = AutoConfig.from_pretrained(model_path)
|
| 24 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 25 |
+
|
| 26 |
+
# Preprocess text (username and link placeholders)
|
| 27 |
+
def preprocess(text):
|
| 28 |
+
new_text = []
|
| 29 |
+
for t in text.split(" "):
|
| 30 |
+
t = "@user" if t.startswith("@") and len(t) > 1 else t
|
| 31 |
+
t = "http" if t.startswith("http") else t
|
| 32 |
+
new_text.append(t)
|
| 33 |
+
return " ".join(new_text)
|
| 34 |
+
|
| 35 |
+
#Function to process the input and return prediction
|
| 36 |
+
def sentiment_analysis(text):
|
| 37 |
+
text = preprocess(text)
|
| 38 |
+
|
| 39 |
+
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
| 40 |
+
output = model(**encoded_input)
|
| 41 |
+
scores_ = output[0][0].detach().numpy()
|
| 42 |
+
scores_ = softmax(scores_)
|
| 43 |
+
|
| 44 |
+
#Output of scores by converting a list of labels and scores into a dictionary format
|
| 45 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 46 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
| 47 |
+
return scores
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
#App interface with gradio
|
| 51 |
+
app = gr.Interface(fn = sentiment_analysis,
|
| 52 |
+
inputs = gr.Textbox("Write your text or tweet here..."),
|
| 53 |
+
outputs = "label",
|
| 54 |
+
title = "Sentiment Analysis of Tweets on COVID-19 Vaccines",
|
| 55 |
+
description = "This app analyzes sentiment of text based on tweets about COVID-19 Vaccines using a fine-tuned albert_base_v2 model",
|
| 56 |
+
interpretation = "default",
|
| 57 |
+
examples=[["covid vaccines are great!"]]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
scipy
|
| 3 |
+
torch
|
| 4 |
+
transformers
|