Spaces:
Sleeping
Sleeping
AbstractQbit
commited on
Commit
·
2058f83
1
Parent(s):
f28d6ee
Add regression trained electra
Browse files- app.py +15 -5
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from transformers import AutoTokenizer,
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
import pickle
|
@@ -9,7 +9,10 @@ sklearn_model = pickle.load(open('classic_pipeline.pickle', 'rb'))
|
|
9 |
|
10 |
model_name = "AbstractQbit/electra_large_imdb_htsplice"
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
-
model =
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
def tokenize_with_splicing(text):
|
@@ -21,20 +24,27 @@ def tokenize_with_splicing(text):
|
|
21 |
tokens['attention_mask'] = [1]*512
|
22 |
return tokens
|
23 |
|
24 |
-
def
|
25 |
stars = round(1 + prob*9)
|
26 |
return '★'*stars + '☆'*(10-stars)
|
27 |
|
|
|
|
|
|
|
|
|
28 |
def run_models(review):
|
29 |
prob_sklearn = float(sklearn_model.predict_proba([review])[0][1])
|
30 |
label_sklearn = 'positive' if prob_sklearn > 0.5 else 'negative'
|
31 |
-
res = f"TF-IDF SVC thinks the review is {label_sklearn} ({100*prob_sklearn:.2f}% positive).\n{
|
32 |
|
33 |
input = tokenize_with_splicing(review).convert_to_tensors('pt', True)
|
34 |
output = torch.nn.functional.softmax(model(**input).logits, dim=1)
|
35 |
prob_electra = float(output[0][1])
|
36 |
label_electra = 'positive' if prob_electra > 0.5 else 'negative'
|
37 |
-
res += f"ELECTRA thinks the review is {label_electra} ({100*prob_electra:.2f}% positive).\n{
|
|
|
|
|
|
|
38 |
|
39 |
return res
|
40 |
|
|
|
1 |
+
from transformers import AutoTokenizer, ElectraForSequenceClassification
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
import pickle
|
|
|
9 |
|
10 |
model_name = "AbstractQbit/electra_large_imdb_htsplice"
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = ElectraForSequenceClassification.from_pretrained(model_name)
|
13 |
+
|
14 |
+
model_reg_name = "AbstractQbit/electra_large_imdb_regression_htsplice"
|
15 |
+
model_reg = ElectraForSequenceClassification.from_pretrained(model_reg_name)
|
16 |
|
17 |
|
18 |
def tokenize_with_splicing(text):
|
|
|
24 |
tokens['attention_mask'] = [1]*512
|
25 |
return tokens
|
26 |
|
27 |
+
def make_stars_from_confidence(prob):
|
28 |
stars = round(1 + prob*9)
|
29 |
return '★'*stars + '☆'*(10-stars)
|
30 |
|
31 |
+
def make_stars_from_rating(rating):
|
32 |
+
stars = round(float(torch.clamp(rating, 1, 10)))
|
33 |
+
return '★'*stars + '☆'*(10-stars)
|
34 |
+
|
35 |
def run_models(review):
|
36 |
prob_sklearn = float(sklearn_model.predict_proba([review])[0][1])
|
37 |
label_sklearn = 'positive' if prob_sklearn > 0.5 else 'negative'
|
38 |
+
res = f"TF-IDF SVC trained with polarity classification thinks the review is {label_sklearn} ({100*prob_sklearn:.2f}% positive confidence).\n{make_stars_from_confidence(prob_sklearn):s}\n\n"
|
39 |
|
40 |
input = tokenize_with_splicing(review).convert_to_tensors('pt', True)
|
41 |
output = torch.nn.functional.softmax(model(**input).logits, dim=1)
|
42 |
prob_electra = float(output[0][1])
|
43 |
label_electra = 'positive' if prob_electra > 0.5 else 'negative'
|
44 |
+
res += f"ELECTRA trained with polarity classification thinks the review is {label_electra} ({100*prob_electra:.2f}% positive confidence).\n{make_stars_from_confidence(prob_electra):s}\n\n"
|
45 |
+
|
46 |
+
rating_electra_reg = model_reg(**input).logits[0,0]
|
47 |
+
res += f"ELECTRA trained with rating regression thinks the review is rated {rating_electra_reg:.2f}★.\n{make_stars_from_rating(rating_electra_reg):s}"
|
48 |
|
49 |
return res
|
50 |
|
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ scikit-learn
|
|
2 |
torch
|
3 |
transformers
|
4 |
tokenizers
|
|
|
|
2 |
torch
|
3 |
transformers
|
4 |
tokenizers
|
5 |
+
accelerate
|