Spaces:
Runtime error
Runtime error
Tho Tran
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
7 |
+
# Preprocess text (username and link placeholders)
|
8 |
+
def preprocess(text):
|
9 |
+
new_text = []
|
10 |
+
for t in text.split(" "):
|
11 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
12 |
+
t = 'http' if t.startswith('http') else t
|
13 |
+
new_text.append(t)
|
14 |
+
return " ".join(new_text)
|
15 |
+
|
16 |
+
MODEL = f"cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
17 |
+
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
19 |
+
config = AutoConfig.from_pretrained(MODEL)
|
20 |
+
|
21 |
+
# PT
|
22 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
23 |
+
model.save_pretrained(MODEL)
|
24 |
+
|
25 |
+
text = "Good night 😊"
|
26 |
+
text = preprocess(text)
|
27 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
28 |
+
output = model(**encoded_input)
|
29 |
+
scores = output[0][0].detach().numpy()
|
30 |
+
scores = softmax(scores)
|
31 |
+
|
32 |
+
# Print labels and scores
|
33 |
+
ranking = np.argsort(scores)
|
34 |
+
ranking = ranking[::-1]
|
35 |
+
for i in range(scores.shape[0]):
|
36 |
+
l = config.id2label[ranking[i]]
|
37 |
+
s = scores[ranking[i]]
|
38 |
+
print(f"{i+1}) {l} {np.round(float(s), 4)}")
|