Spaces:
Sleeping
Sleeping
Emanuel Huber
commited on
Commit
•
b29b5d8
1
Parent(s):
7874b31
Added confidence scores
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from typing import List, Tuple
|
|
5 |
import gradio as gr
|
6 |
import pandas as pd
|
7 |
import spacy
|
|
|
8 |
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
9 |
|
10 |
try:
|
@@ -36,6 +37,7 @@ def predict(text, nlp, logger=None) -> Tuple[List[str], List[str]]:
|
|
36 |
|
37 |
i_token = 0
|
38 |
labels = []
|
|
|
39 |
for off, is_special_token, pred in zip(
|
40 |
input_tokens["offset_mapping"][0],
|
41 |
input_tokens["special_tokens_mask"][0],
|
@@ -47,17 +49,21 @@ def predict(text, nlp, logger=None) -> Tuple[List[str], List[str]]:
|
|
47 |
if logger is not None:
|
48 |
logger.info("{}, {}, {}".format(off, tokens[i_token], label))
|
49 |
labels.append(label)
|
|
|
|
|
|
|
50 |
i_token += 1
|
51 |
|
52 |
-
return tokens, labels
|
53 |
|
54 |
|
55 |
def text_analysis(text):
|
56 |
-
tokens, labels = predict(text, nlp, logger)
|
57 |
pos_count = pd.DataFrame(
|
58 |
{
|
59 |
"token": tokens,
|
60 |
"etiqueta": labels,
|
|
|
61 |
}
|
62 |
)
|
63 |
pos_tokens = []
|
|
|
5 |
import gradio as gr
|
6 |
import pandas as pd
|
7 |
import spacy
|
8 |
+
import torch
|
9 |
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
10 |
|
11 |
try:
|
|
|
37 |
|
38 |
i_token = 0
|
39 |
labels = []
|
40 |
+
scores = []
|
41 |
for off, is_special_token, pred in zip(
|
42 |
input_tokens["offset_mapping"][0],
|
43 |
input_tokens["special_tokens_mask"][0],
|
|
|
49 |
if logger is not None:
|
50 |
logger.info("{}, {}, {}".format(off, tokens[i_token], label))
|
51 |
labels.append(label)
|
52 |
+
scores.append(
|
53 |
+
"{:.2f}".format(100 * float(torch.softmax(pred, dim=-1).detach().max()))
|
54 |
+
)
|
55 |
i_token += 1
|
56 |
|
57 |
+
return tokens, labels, scores
|
58 |
|
59 |
|
60 |
def text_analysis(text):
|
61 |
+
tokens, labels, scores = predict(text, nlp, logger)
|
62 |
pos_count = pd.DataFrame(
|
63 |
{
|
64 |
"token": tokens,
|
65 |
"etiqueta": labels,
|
66 |
+
"confiança": scores,
|
67 |
}
|
68 |
)
|
69 |
pos_tokens = []
|