eaedk commited on
Commit
29f83a3
1 Parent(s): b11fbd6
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification
3
+ from transformers import AutoTokenizer, AutoConfig
4
+ import numpy as np
5
+ from scipy.special import softmax
6
+
7
+ # Setup
8
+ model_path = f"GhylB/Sentiment_Analysis_DistilBERT"
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
11
+ config = AutoConfig.from_pretrained(model_path)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
13
+
14
+ # Functions
15
+
16
+ # Preprocess text (username and link placeholders)
17
+
18
+
19
+ def preprocess(text):
20
+ new_text = []
21
+ for t in text.split(" "):
22
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
23
+ t = 'http' if t.startswith('http') else t
24
+ new_text.append(t)
25
+ return " ".join(new_text)
26
+
27
+
28
+ def sentiment_analysis(text):
29
+ text = preprocess(text)
30
+
31
+ # PyTorch-based models
32
+ encoded_input = tokenizer(text, return_tensors='pt')
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
+ demo = gr.Interface(
45
+ fn=sentiment_analysis,
46
+ inputs=gr.Textbox(placeholder="Copy and paste/Write a tweet here..."),
47
+ outputs="text",
48
+ interpretation="default",
49
+ examples=[["What's up with the vaccine"],
50
+ ["Covid cases are increasing fast!"],
51
+ ["Covid has been invented by Mavis"],
52
+ ["I'm going to party this weekend"],
53
+ ["Covid is hoax"]],
54
+ title="Tutorial : Sentiment Analysis App",
55
+ description="This Application assesses if a twitter post relating to vaccinations is positive, neutral, or negative.", )
56
+
57
+ if __name__ == "__main__":
58
+ demo.launch(server_name="0.0.0.0", server_port=7860) # 8080