Matteo Mendula
commited on
Commit
Β·
28e566d
1
Parent(s):
c20f8d9
code refactoring
Browse files
app.py
CHANGED
@@ -1,49 +1,43 @@
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
|
3 |
-
# import torch
|
4 |
-
# device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
5 |
-
# device
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
download = False
|
10 |
save_model_locally= False
|
11 |
if download:
|
12 |
tokenizer = AutoTokenizer.from_pretrained("MilaNLProc/feel-it-italian-sentiment", cache_dir="data/")
|
13 |
-
|
14 |
-
|
15 |
tokenizer_emo = AutoTokenizer.from_pretrained("MilaNLProc/feel-it-italian-emotion", cache_dir="data/")
|
16 |
model_emo = AutoModelForSequenceClassification.from_pretrained("MilaNLProc/feel-it-italian-emotion", cache_dir="data/")
|
17 |
model_emo.eval()
|
18 |
if save_model_locally:
|
19 |
-
|
20 |
tokenizer.save_pretrained('./local_models/sentiment_ITA')
|
21 |
model_emo.save_pretrained('./local_models/emotion_ITA')
|
22 |
tokenizer_emo.save_pretrained('./local_models/emotion_ITA')
|
23 |
else:
|
24 |
tokenizer = AutoTokenizer.from_pretrained("./local_models/sentiment_ITA/")
|
25 |
-
|
26 |
-
|
27 |
|
28 |
tokenizer_emo = AutoTokenizer.from_pretrained("./local_models/emotion_ITA/")
|
29 |
model_emo = AutoModelForSequenceClassification.from_pretrained("./local_models/emotion_ITA/", num_labels=4)
|
30 |
model_emo.eval()
|
31 |
|
32 |
|
33 |
-
#%%
|
34 |
|
35 |
from transformers import pipeline
|
36 |
import re
|
37 |
|
38 |
-
|
39 |
-
generator_emo = pipeline(task="text-classification",
|
40 |
|
41 |
def sentiment_emoji(input_abs):
|
42 |
|
43 |
if(input_abs ==""):
|
44 |
return "π€·ββοΈ"
|
45 |
|
46 |
-
res =
|
47 |
res = {res[x]["label"]: res[x]["score"] for x in range(len(res))}
|
48 |
res["π positive"] = res.pop("positive")
|
49 |
res["π negative"] = res.pop("negative")
|
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
download = False
|
4 |
save_model_locally= False
|
5 |
if download:
|
6 |
tokenizer = AutoTokenizer.from_pretrained("MilaNLProc/feel-it-italian-sentiment", cache_dir="data/")
|
7 |
+
model_sent = AutoModelForSequenceClassification.from_pretrained("MilaNLProc/feel-it-italian-sentiment", cache_dir="data/")
|
8 |
+
model_sent.eval()
|
9 |
tokenizer_emo = AutoTokenizer.from_pretrained("MilaNLProc/feel-it-italian-emotion", cache_dir="data/")
|
10 |
model_emo = AutoModelForSequenceClassification.from_pretrained("MilaNLProc/feel-it-italian-emotion", cache_dir="data/")
|
11 |
model_emo.eval()
|
12 |
if save_model_locally:
|
13 |
+
model_sent.save_pretrained('./local_models/sentiment_ITA')
|
14 |
tokenizer.save_pretrained('./local_models/sentiment_ITA')
|
15 |
model_emo.save_pretrained('./local_models/emotion_ITA')
|
16 |
tokenizer_emo.save_pretrained('./local_models/emotion_ITA')
|
17 |
else:
|
18 |
tokenizer = AutoTokenizer.from_pretrained("./local_models/sentiment_ITA/")
|
19 |
+
model_sent = AutoModelForSequenceClassification.from_pretrained("./local_models/sentiment_ITA/", num_labels=2)
|
20 |
+
model_sent.eval()
|
21 |
|
22 |
tokenizer_emo = AutoTokenizer.from_pretrained("./local_models/emotion_ITA/")
|
23 |
model_emo = AutoModelForSequenceClassification.from_pretrained("./local_models/emotion_ITA/", num_labels=4)
|
24 |
model_emo.eval()
|
25 |
|
26 |
|
27 |
+
#%%generator_sent
|
28 |
|
29 |
from transformers import pipeline
|
30 |
import re
|
31 |
|
32 |
+
generator_sent = pipeline(task="text-classification", model_sent=model_sent, tokenizer=tokenizer, return_all_scores =True)
|
33 |
+
generator_emo = pipeline(task="text-classification", model_sent=model_emo, tokenizer=tokenizer_emo, return_all_scores =True)
|
34 |
|
35 |
def sentiment_emoji(input_abs):
|
36 |
|
37 |
if(input_abs ==""):
|
38 |
return "π€·ββοΈ"
|
39 |
|
40 |
+
res = generator_sent(input_abs)[0]
|
41 |
res = {res[x]["label"]: res[x]["score"] for x in range(len(res))}
|
42 |
res["π positive"] = res.pop("positive")
|
43 |
res["π negative"] = res.pop("negative")
|