Spaces:
Sleeping
Sleeping
Hanna Hjelmeland
commited on
Commit
·
ad3a545
1
Parent(s):
b7fa0eb
Change app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,49 @@
|
|
1 |
__all__ = ['is_flower', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
|
2 |
|
3 |
-
# Cell
|
4 |
from fastai.vision.all import *
|
5 |
import gradio as gr
|
|
|
|
|
6 |
|
7 |
-
def is_cat(x): return x[0].isupper()
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
def classify_image(img):
|
16 |
-
pred,idx,probs = learn.predict(img)
|
17 |
-
return dict(zip(categories, map(float,probs)))
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
intf.launch(inline=False)
|
|
|
1 |
__all__ = ['is_flower', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
|
2 |
|
|
|
3 |
from fastai.vision.all import *
|
4 |
import gradio as gr
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
6 |
+
import torch
|
7 |
|
|
|
8 |
|
9 |
+
model_name = "NbAiLab/nb-bert-base"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
|
12 |
+
model_path = "/models/first_model"
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
14 |
|
|
|
|
|
|
|
15 |
|
16 |
+
def classify_text(test_text):
|
17 |
+
inputs = tokenizer(test_text, return_tensors="pt")
|
18 |
+
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model(**inputs)
|
21 |
+
logits = outputs.logits
|
22 |
+
probabilities = torch.softmax(logits, dim=1)
|
23 |
+
|
24 |
+
predicted_class = torch.argmax(probabilities, dim=1).item()
|
25 |
+
class_labels = model.config.id2label
|
26 |
+
predicted_label = class_labels[predicted_class]
|
27 |
+
probabilities = probabilities[0].tolist()
|
28 |
|
29 |
+
categories = ['Kvinner 30-40', 'Kvinner 40-55', 'Menn 30-40', 'Menn 40-55']
|
30 |
|
31 |
+
category_probabilities = list(zip(categories, probabilities))
|
32 |
+
|
33 |
+
max_category = max(category_probabilities, key=lambda x: x[1])
|
34 |
+
|
35 |
+
#print('The model predicts that this text lead would have a majority of readers in the target group', max_category[0])
|
36 |
+
|
37 |
+
return dict(zip(categories, map(float,probabilities)))
|
38 |
+
|
39 |
+
# Cell
|
40 |
+
label = gr.outputs.Label()
|
41 |
+
categories = ('Kvinner 30-40', 'Kvinner 40-55', 'Menn 30-40', 'Menn 40-55')
|
42 |
+
app_title = "Target group classifier"
|
43 |
+
|
44 |
+
examples = ["Moren leter etter sønnen i et ihjelbombet leilighetskompleks.",
|
45 |
+
"De første månedene av krigen gikk så som så. Nå har Putin skiftet strategi."
|
46 |
+
"Fotballstadion tok fyr i helgen"
|
47 |
+
]
|
48 |
+
intf = gr.Interface(fn=classify_text, inputs="text", outputs=label, examples=examples, title=app_title)
|
49 |
intf.launch(inline=False)
|