Update app.py
Browse files
app.py
CHANGED
@@ -15,4 +15,69 @@ from sklearn import preprocessing
|
|
15 |
import sklearn
|
16 |
from sklearn.metrics import confusion_matrix
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
import sklearn
|
16 |
from sklearn.metrics import confusion_matrix
|
17 |
|
18 |
+
from transformers import AutoModelForSequenceClassification
|
19 |
+
from transformers import AutoTokenizer, AutoConfig
|
20 |
+
import numpy as np
|
21 |
+
from scipy.special import softmax
|
22 |
+
import gradio as gr
|
23 |
+
|
24 |
+
# Preprocess text (username and link placeholders)
|
25 |
+
def preprocess(text):
|
26 |
+
new_text = []
|
27 |
+
for t in text.split(" "):
|
28 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
29 |
+
t = 'http' if t.startswith('http') else t
|
30 |
+
new_text.append(t)
|
31 |
+
return " ".join(new_text)
|
32 |
+
|
33 |
+
# load model
|
34 |
+
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment-latest"
|
35 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
36 |
+
#model.save_pretrained(MODEL)
|
37 |
+
|
38 |
+
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
40 |
+
config = AutoConfig.from_pretrained(MODEL)
|
41 |
+
|
42 |
+
# create classifier function
|
43 |
+
def classify_sentiments(text):
|
44 |
+
text = preprocess(text)
|
45 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
46 |
+
output = model(**encoded_input)
|
47 |
+
scores = output[0][0].detach().numpy()
|
48 |
+
scores = softmax(scores)
|
49 |
+
|
50 |
+
# Print labels and scores
|
51 |
+
probs = {}
|
52 |
+
ranking = np.argsort(scores)
|
53 |
+
ranking = ranking[::-1]
|
54 |
+
|
55 |
+
for i in range(len(scores)):
|
56 |
+
l = config.id2label[ranking[i]]
|
57 |
+
s = scores[ranking[i]]
|
58 |
+
probs[l] = np.round(float(s), 4)
|
59 |
+
return probs
|
60 |
+
|
61 |
+
|
62 |
+
#build the Gradio app
|
63 |
+
#Instructuction = "Write an imaginary review about a product or service you might be interested in."
|
64 |
+
title="Text Sentiment Analysis"
|
65 |
+
description = """Write a Good or Bad review about an imaginary product or service,\
|
66 |
+
see how the machine learning model is able to predict your sentiments"""
|
67 |
+
article = """
|
68 |
+
- Click submit button to test sentiment analysis prediction
|
69 |
+
- Click clear button to refresh text
|
70 |
+
"""
|
71 |
|
72 |
+
gr.Interface(,
|
73 |
+
'text',
|
74 |
+
'label',
|
75 |
+
title = title,
|
76 |
+
description = description,
|
77 |
+
#Instruction = Instructuction,
|
78 |
+
article = article,
|
79 |
+
allow_flagging = "never",
|
80 |
+
live = False,
|
81 |
+
examples=["This has to be the best Introductory course in machine learning",
|
82 |
+
"I consider this training an absolute waste of time."]
|
83 |
+
).launch()
|