eaedk commited on
Commit
0eb7b58
1 Parent(s): f438bd6
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +59 -0
  3. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .vscode/
2
+ *venv/
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification
3
+ from transformers import TFAutoModelForSequenceClassification
4
+ from transformers import AutoTokenizer, AutoConfig
5
+ import numpy as np
6
+ from scipy.special import softmax
7
+
8
+ # Setup
9
+ model_path = f"GhylB/Sentiment_Analysis_DistilBERT"
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
12
+ config = AutoConfig.from_pretrained(model_path)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
14
+
15
+ # Functions
16
+
17
+ # Preprocess text (username and link placeholders)
18
+
19
+
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
+
29
+ def sentiment_analysis(text):
30
+ text = preprocess(text)
31
+
32
+ # PyTorch-based models
33
+ encoded_input = tokenizer(text, return_tensors='pt')
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
+
45
+ demo = gr.Interface(
46
+ fn=sentiment_analysis,
47
+ inputs=gr.Textbox(placeholder="Copy and paste/Write a tweet here..."),
48
+ outputs="text",
49
+ interpretation="default",
50
+ examples=[["What's up with the vaccine"],
51
+ ["Covid cases are increasing fast!"],
52
+ ["Covid has been invented by Mavis"],
53
+ ["I'm going to party this weekend"],
54
+ ["Covid is hoax"]],
55
+ title="Tutorial : Sentiment Analysis App",
56
+ description="This Application assesses if a twitter post relating to vaccinations is positive, neutral, or negative.", )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers==4.28.1
2
+ gradio==3.28.0
3
+ scikit-learn==1.2.2
4
+ scipy==1.10.1
5
+ torch
6
+ black