HOLYBOY commited on
Commit
f34f70d
·
1 Parent(s): 5499a4f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import pandas as pd
4
+ import pickle
5
+ import transformers
6
+ from transformers import AutoTokenizer
7
+ from transformers import AutoConfig
8
+ from transformers import AutoModelForSequenceClassification
9
+ from transformers import TFAutoModelForSequenceClassification
10
+ from transformers import pipeline
11
+ from scipy.special import softmax
12
+
13
+ # Requirements
14
+ model_path ="HOLYBOY/Sentiment_Analysis"
15
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
16
+ config = AutoConfig.from_pretrained(model_path)
17
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
18
+
19
+ # Preprocess text (username and link placeholders)
20
+ def preprocess(text):
21
+ new_text = []
22
+ for t in text.split(" "):
23
+ t = "@user" if t.startswith("@") and len(t) > 1 else t
24
+ t = "http" if t.startswith("http") else t
25
+ new_text.append(t)
26
+ return " ".join(new_text)
27
+
28
+ # ---- Function to process the input and return prediction
29
+ def sentiment_analysis(text):
30
+ text = preprocess(text)
31
+
32
+ encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
33
+ output = model(**encoded_input)
34
+ scores_ = output[0][0].detach().numpy()
35
+ scores_ = softmax(scores_)
36
+
37
+ # Format output dict of scores
38
+ labels = ["Negative", "Neutral", "Positive"]
39
+ scores = {l:float(s) for (l,s) in zip(labels, scores_) }
40
+
41
+ return scores
42
+
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)