Calistus commited on
Commit
4ee549e
·
1 Parent(s): c1fccbf

Upload gradio_app.py

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