FKBaffour commited on
Commit
cba9dee
1 Parent(s): 515dcf7

App script

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