Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,24 +17,20 @@ 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 |
-
# Initialize the model
|
| 29 |
model = XLMRForTokenClassification(
|
| 30 |
pretrained_path=pretrained_path,
|
| 31 |
n_labels=len(label_list) + 1,
|
| 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()
|
|
@@ -42,23 +38,18 @@ def load_model(pretrained_path, model_path, device):
|
|
| 42 |
return model, tokenizer, label_map
|
| 43 |
|
| 44 |
def predict_ner(model, tokenizer, text, label_map, device):
|
| 45 |
-
# Tokenize input text
|
| 46 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 47 |
input_ids = inputs["input_ids"].to(device)
|
| 48 |
attention_mask = inputs["attention_mask"].to(device)
|
| 49 |
|
| 50 |
-
# Predict NER labels
|
| 51 |
with torch.no_grad():
|
| 52 |
logits = model(inputs_ids=input_ids, labels=None, labels_mask=None, valid_mask=None)
|
| 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 |
-
# Filter out unwanted tokens and remove prefix "▁"
|
| 62 |
result = []
|
| 63 |
current_word = ''
|
| 64 |
current_label = 'O'
|
|
@@ -77,22 +68,21 @@ def predict_ner(model, tokenizer, text, label_map, device):
|
|
| 77 |
if current_word:
|
| 78 |
result.append((current_word, current_label))
|
| 79 |
|
| 80 |
-
|
| 81 |
-
result = [(word, label) for word, label in result if word and word.isalnum()]
|
| 82 |
|
| 83 |
return result
|
| 84 |
|
| 85 |
def format_output(text, predictions):
|
| 86 |
formatted_text = ""
|
| 87 |
for word, label in predictions:
|
| 88 |
-
if label
|
| 89 |
-
color = BACKGROUND_COLOR_MAP.get(label, 'white')
|
| 90 |
-
formatted_text += f"<span style='background-color:{color}; border:1px solid {color}; padding:2px;'>{word} <b>{label}</b></span> "
|
| 91 |
-
else:
|
| 92 |
formatted_text += f"{word} "
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
# Load the model and tokenizer
|
| 96 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 97 |
model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)
|
| 98 |
|
|
@@ -100,7 +90,6 @@ def ner(text):
|
|
| 100 |
predictions = predict_ner(model, tokenizer, text, label_map, device)
|
| 101 |
return format_output(text, predictions)
|
| 102 |
|
| 103 |
-
# Define Gradio interface
|
| 104 |
interface = gr.Interface(
|
| 105 |
fn=ner,
|
| 106 |
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
def load_model(pretrained_path, model_path, device):
|
|
|
|
| 20 |
processor = NerProcessor()
|
| 21 |
label_list = processor.get_labels()
|
| 22 |
label_map = {i: label for i, label in enumerate(label_list, 1)}
|
| 23 |
|
|
|
|
| 24 |
tokenizer = XLMRobertaTokenizer.from_pretrained(pretrained_path)
|
| 25 |
|
|
|
|
| 26 |
model = XLMRForTokenClassification(
|
| 27 |
pretrained_path=pretrained_path,
|
| 28 |
n_labels=len(label_list) + 1,
|
| 29 |
+
hidden_size=768,
|
| 30 |
dropout_p=0.1,
|
| 31 |
device=device
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 35 |
model.to(device)
|
| 36 |
model.eval()
|
|
|
|
| 38 |
return model, tokenizer, label_map
|
| 39 |
|
| 40 |
def predict_ner(model, tokenizer, text, label_map, device):
|
|
|
|
| 41 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 42 |
input_ids = inputs["input_ids"].to(device)
|
| 43 |
attention_mask = inputs["attention_mask"].to(device)
|
| 44 |
|
|
|
|
| 45 |
with torch.no_grad():
|
| 46 |
logits = model(inputs_ids=input_ids, labels=None, labels_mask=None, valid_mask=None)
|
| 47 |
|
|
|
|
| 48 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
| 49 |
|
|
|
|
| 50 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
|
| 51 |
labels = [label_map[label_id] for label_id in label_ids[0]]
|
| 52 |
|
|
|
|
| 53 |
result = []
|
| 54 |
current_word = ''
|
| 55 |
current_label = 'O'
|
|
|
|
| 68 |
if current_word:
|
| 69 |
result.append((current_word, current_label))
|
| 70 |
|
| 71 |
+
result = [(word, label) for word, label in result if word]
|
|
|
|
| 72 |
|
| 73 |
return result
|
| 74 |
|
| 75 |
def format_output(text, predictions):
|
| 76 |
formatted_text = ""
|
| 77 |
for word, label in predictions:
|
| 78 |
+
if label == 'O':
|
|
|
|
|
|
|
|
|
|
| 79 |
formatted_text += f"{word} "
|
| 80 |
+
else:
|
| 81 |
+
color = BACKGROUND_COLOR_MAP[label]
|
| 82 |
+
formatted_text += f"<span style='background-color:{color}; padding: 2px 5px; border: 1px solid {color}; font-weight: bold;'>{word} ({label})</span> "
|
| 83 |
+
|
| 84 |
+
return formatted_text
|
| 85 |
|
|
|
|
| 86 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 87 |
model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)
|
| 88 |
|
|
|
|
| 90 |
predictions = predict_ner(model, tokenizer, text, label_map, device)
|
| 91 |
return format_output(text, predictions)
|
| 92 |
|
|
|
|
| 93 |
interface = gr.Interface(
|
| 94 |
fn=ner,
|
| 95 |
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|