Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import torch
|
| 2 |
from transformers import XLMRobertaTokenizer
|
| 3 |
from model.xlmr_for_token_classification import XLMRForTokenClassification
|
| 4 |
-
from utils.data_utils import NerProcessor
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
|
|
|
| 7 |
BACKGROUND_COLOR_MAP = {
|
| 8 |
'B-PER': '#FFDDDD',
|
| 9 |
'I-PER': '#FFAAAA',
|
|
@@ -17,28 +18,24 @@ BACKGROUND_COLOR_MAP = {
|
|
| 17 |
}
|
| 18 |
|
| 19 |
def load_model(pretrained_path, model_path, device):
|
| 20 |
-
# Load the processor and label map
|
| 21 |
-
processor = NerProcessor()
|
| 22 |
-
label_list = processor.get_labels()
|
| 23 |
-
label_map = {i: label for i, label in enumerate(label_list, 1)}
|
| 24 |
-
|
| 25 |
# Load the tokenizer
|
| 26 |
tokenizer = XLMRobertaTokenizer.from_pretrained(pretrained_path)
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
model = XLMRForTokenClassification(
|
| 30 |
pretrained_path=pretrained_path,
|
| 31 |
-
n_labels=
|
| 32 |
-
hidden_size=768, #
|
| 33 |
dropout_p=0.1,
|
| 34 |
device=device
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
# Load the model state
|
| 38 |
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 39 |
model.to(device)
|
| 40 |
model.eval()
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
return model, tokenizer, label_map
|
| 43 |
|
| 44 |
def predict_ner(model, tokenizer, text, label_map, device):
|
|
@@ -49,54 +46,42 @@ def predict_ner(model, tokenizer, text, label_map, device):
|
|
| 49 |
|
| 50 |
# Predict NER labels
|
| 51 |
with torch.no_grad():
|
| 52 |
-
logits = model(
|
| 53 |
-
|
| 54 |
# Convert logits to label IDs
|
| 55 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
| 56 |
-
|
| 57 |
# Convert label IDs to label names
|
| 58 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
|
| 59 |
labels = [label_map[label_id] for label_id in label_ids[0]]
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
current_word = ''
|
| 64 |
-
current_label = 'O'
|
| 65 |
for token, label in zip(tokens, labels):
|
| 66 |
if token in ["<s>", "</s>"]:
|
| 67 |
continue
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
current_label = label
|
| 78 |
-
if current_word:
|
| 79 |
-
background_color = BACKGROUND_COLOR_MAP.get(current_label, 'white')
|
| 80 |
-
formatted_result += f'<span style="background-color: {background_color};">{current_word}</span>'
|
| 81 |
-
|
| 82 |
-
return formatted_result
|
| 83 |
-
|
| 84 |
-
# Load the model and tokenizer
|
| 85 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 86 |
-
model, tokenizer, label_map = load_model(
|
| 87 |
|
|
|
|
| 88 |
def ner(text):
|
| 89 |
-
|
| 90 |
-
return formatted_text
|
| 91 |
|
| 92 |
-
|
| 93 |
-
interface = gr.Interface(
|
| 94 |
fn=ner,
|
| 95 |
-
inputs=
|
| 96 |
outputs="html",
|
| 97 |
-
title="Named Entity Recognition
|
| 98 |
-
description="
|
| 99 |
)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
interface.launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import XLMRobertaTokenizer
|
| 3 |
from model.xlmr_for_token_classification import XLMRForTokenClassification
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Background color map for named entities
|
| 8 |
BACKGROUND_COLOR_MAP = {
|
| 9 |
'B-PER': '#FFDDDD',
|
| 10 |
'I-PER': '#FFAAAA',
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
def load_model(pretrained_path, model_path, device):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Load the tokenizer
|
| 22 |
tokenizer = XLMRobertaTokenizer.from_pretrained(pretrained_path)
|
| 23 |
+
|
| 24 |
+
# Load the model
|
| 25 |
model = XLMRForTokenClassification(
|
| 26 |
pretrained_path=pretrained_path,
|
| 27 |
+
n_labels=6, # Adjust number of labels based on your use case
|
| 28 |
+
hidden_size=768, # Adjust based on your model size
|
| 29 |
dropout_p=0.1,
|
| 30 |
device=device
|
| 31 |
)
|
|
|
|
|
|
|
| 32 |
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 33 |
model.to(device)
|
| 34 |
model.eval()
|
| 35 |
+
|
| 36 |
+
# Define label map
|
| 37 |
+
label_map = {0: 'O', 1: 'B-PER', 2: 'I-PER', 3: 'B-ORG', 4: 'I-ORG', 5: 'B-LOC', 6: 'I-LOC'}
|
| 38 |
+
|
| 39 |
return model, tokenizer, label_map
|
| 40 |
|
| 41 |
def predict_ner(model, tokenizer, text, label_map, device):
|
|
|
|
| 46 |
|
| 47 |
# Predict NER labels
|
| 48 |
with torch.no_grad():
|
| 49 |
+
logits = model(inputs_ids=input_ids, labels=None, labels_mask=None, valid_mask=None)
|
| 50 |
+
|
| 51 |
# Convert logits to label IDs
|
| 52 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
| 53 |
+
|
| 54 |
# Convert label IDs to label names
|
| 55 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
|
| 56 |
labels = [label_map[label_id] for label_id in label_ids[0]]
|
| 57 |
+
|
| 58 |
+
# Generate highlighted text
|
| 59 |
+
highlighted_text = ""
|
|
|
|
|
|
|
| 60 |
for token, label in zip(tokens, labels):
|
| 61 |
if token in ["<s>", "</s>"]:
|
| 62 |
continue
|
| 63 |
+
token = token.replace("▁", " ")
|
| 64 |
+
color = BACKGROUND_COLOR_MAP.get(label, "white")
|
| 65 |
+
highlighted_text += f'<span style="background-color: {color}">{token}</span> '
|
| 66 |
+
|
| 67 |
+
return highlighted_text
|
| 68 |
+
|
| 69 |
+
# Load model and tokenizer
|
| 70 |
+
pretrained_path = "./pretrained_models/xlmr.base"
|
| 71 |
+
model_path = "./model.pt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 73 |
+
model, tokenizer, label_map = load_model(pretrained_path, model_path, device)
|
| 74 |
|
| 75 |
+
# Define Gradio interface
|
| 76 |
def ner(text):
|
| 77 |
+
return predict_ner(model, tokenizer, text, label_map, device)
|
|
|
|
| 78 |
|
| 79 |
+
iface = gr.Interface(
|
|
|
|
| 80 |
fn=ner,
|
| 81 |
+
inputs="text",
|
| 82 |
outputs="html",
|
| 83 |
+
title="XLM-RoBERTa Named Entity Recognition",
|
| 84 |
+
description="Enter text to get Named Entity Recognition results with highlighted entities.",
|
| 85 |
)
|
| 86 |
|
| 87 |
+
iface.launch()
|
|
|