Calistus commited on
Commit
f54fab2
·
1 Parent(s): f5239bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip3 install -q transformers gradio
2
+
3
+ from transformers import AutoModelForSequenceClassification
4
+ from transformers import TFAutoModelForSequenceClassification
5
+ from transformers import AutoTokenizer, AutoConfig
6
+ import numpy as np
7
+ from scipy.special import softmax
8
+ import gradio as gr
9
+
10
+ # Requirements
11
+ model_path = f"Calistus/test_trainer"
12
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
13
+ config = AutoConfig.from_pretrained(model_path)
14
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
15
+
16
+ # Preprocess text (username and link placeholders)
17
+ def preprocess(text):
18
+ new_text = []
19
+ for t in text.split(" "):
20
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
21
+ t = 'http' if t.startswith('http') else t
22
+ new_text.append(t)
23
+ return " ".join(new_text)
24
+
25
+
26
+ def sentiment_analysis(text):
27
+ text = preprocess(text)
28
+
29
+ # PyTorch-based models
30
+ encoded_input = tokenizer(text, return_tensors='pt')
31
+ output = model(**encoded_input)
32
+ scores_ = output[0][0].detach().numpy()
33
+ scores_ = softmax(scores_)
34
+
35
+ # Format output dict of scores
36
+ labels = ['Negative', 'Neutral', 'Positive']
37
+ scores = {l:float(s) for (l,s) in zip(labels, scores_) }
38
+
39
+ return scores
40
+
41
+ app = gr.Interface(
42
+ fn=sentiment_analysis,
43
+ inputs=gr.Textbox(placeholder="Write your tweet here..."),
44
+ outputs="label",
45
+ interpretation="default",
46
+ examples=[["Please don't listen to anyone. Vaccinate your child"],['My kid has a lump on his hand because of the vaccine']])
47
+
48
+ app.launch()