Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,7 +43,7 @@ def predict_ner(model, tokenizer, text, label_map, device):
|
|
| 43 |
attention_mask = inputs["attention_mask"].to(device)
|
| 44 |
|
| 45 |
with torch.no_grad():
|
| 46 |
-
logits = model(
|
| 47 |
|
| 48 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
| 49 |
|
|
@@ -75,21 +75,27 @@ def predict_ner(model, tokenizer, text, label_map, device):
|
|
| 75 |
def format_output(text, predictions):
|
| 76 |
formatted_text = ""
|
| 77 |
i = 0
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
formatted_text += text[i]
|
| 81 |
i += 1
|
| 82 |
-
|
| 83 |
formatted_text += text[i]
|
| 84 |
i += 1
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
else:
|
| 88 |
-
color = BACKGROUND_COLOR_MAP[label]
|
| 89 |
-
formatted_text += f"<span style='background-color:{color}; padding: 2px 5px; border: 1px solid {color}; font-weight: bold;'>{word} <span style='background-color:{color}; border: 1px solid {color};'>{label}</span></span> "
|
| 90 |
-
i += len(word)
|
| 91 |
-
|
| 92 |
-
return formatted_text
|
| 93 |
|
| 94 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 95 |
model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)
|
|
|
|
| 43 |
attention_mask = inputs["attention_mask"].to(device)
|
| 44 |
|
| 45 |
with torch.no_grad():
|
| 46 |
+
logits = model(input_ids=input_ids, attention_mask=attention_mask, labels=None)
|
| 47 |
|
| 48 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
| 49 |
|
|
|
|
| 75 |
def format_output(text, predictions):
|
| 76 |
formatted_text = ""
|
| 77 |
i = 0
|
| 78 |
+
pos = 0
|
| 79 |
+
|
| 80 |
+
while i < len(text):
|
| 81 |
+
if pos < len(predictions) and text[i] == predictions[pos][0]:
|
| 82 |
+
word, label = predictions[pos]
|
| 83 |
+
color = BACKGROUND_COLOR_MAP[label]
|
| 84 |
+
formatted_text += (
|
| 85 |
+
f"<span style='background-color:{color}; padding: 2px 5px; border: 1px solid {color}; font-weight: bold;'>"
|
| 86 |
+
f"{word} <span style='background-color:{color}; border: 1px solid {color}; padding: 2px; border-radius: 3px; font-weight: bold;'>"
|
| 87 |
+
f"{label}</span></span>"
|
| 88 |
+
)
|
| 89 |
+
i += len(word)
|
| 90 |
+
pos += 1
|
| 91 |
+
elif text[i] in ".,!?;:()[]{}":
|
| 92 |
formatted_text += text[i]
|
| 93 |
i += 1
|
| 94 |
+
else:
|
| 95 |
formatted_text += text[i]
|
| 96 |
i += 1
|
| 97 |
+
|
| 98 |
+
return f"<div style='border: 2px solid #cccccc; padding: 10px; border-radius: 5px;'>{formatted_text}</div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 101 |
model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)
|