Queensly commited on
Commit
abecc86
1 Parent(s): 83efb5e

Create app.py

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