Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import the required Libraries
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import pickle
|
6 |
+
import transformers
|
7 |
+
from transformers import AutoTokenizer
|
8 |
+
from transformers import AutoConfig
|
9 |
+
from transformers import AutoModelForSequenceClassification
|
10 |
+
from transformers import TFAutoModelForSequenceClassification
|
11 |
+
from transformers import pipeline
|
12 |
+
from scipy.special import softmax
|
13 |
+
|
14 |
+
# Requirements
|
15 |
+
model_path ="HOLYBOY/Sentiment_Analysis"
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
17 |
+
config = AutoConfig.from_pretrained(model_path)
|
18 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
19 |
+
|
20 |
+
# Preprocess text (username and link placeholders)
|
21 |
+
def preprocess(text):
|
22 |
+
new_text = []
|
23 |
+
for t in text.split(" "):
|
24 |
+
t = "@user" if t.startswith("@") and len(t) > 1 else t
|
25 |
+
t = "http" if t.startswith("http") else t
|
26 |
+
new_text.append(t)
|
27 |
+
return " ".join(new_text)
|
28 |
+
|
29 |
+
# ---- Function to process the input and return prediction
|
30 |
+
def sentiment_analysis(text):
|
31 |
+
text = preprocess(text)
|
32 |
+
|
33 |
+
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
34 |
+
output = model(**encoded_input)
|
35 |
+
scores_ = output[0][0].detach().numpy()
|
36 |
+
scores_ = softmax(scores_)
|
37 |
+
|
38 |
+
# Format output dict of scores
|
39 |
+
labels = ["Negative", "Neutral", "Positive"]
|
40 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
41 |
+
|
42 |
+
return scores
|
43 |
+
|
44 |
+
# ---- Gradio app interface
|
45 |
+
app = gr.Interface(fn = sentiment_analysis,
|
46 |
+
inputs = gr.Textbox("Input your tweet to classify or use the example provided below..."),
|
47 |
+
outputs = "label",
|
48 |
+
title = "Public Perception of COVID-19 Vaccines",
|
49 |
+
description = "This app analyzes Perception of text based on tweets about COVID-19 Vaccines using a fine-tuned distilBERT model",
|
50 |
+
interpretation = "default",
|
51 |
+
examples = [["The idea of introducing the vaccine is good"],
|
52 |
+
["I am definately not taking the jab"],
|
53 |
+
["The vaccine is bad and can cause serious health implications"],
|
54 |
+
["I dont have any opinion "]]
|
55 |
+
)
|
56 |
+
|
57 |
+
app.launch(share =True)
|