Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,19 @@ from model.xlmr_for_token_classification import XLMRForTokenClassification
|
|
| 4 |
from utils.data_utils import NerProcessor
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def load_model(pretrained_path, model_path, device):
|
| 8 |
# Load the processor and label map
|
| 9 |
processor = NerProcessor()
|
|
@@ -37,7 +50,7 @@ def predict_ner(model, tokenizer, text, label_map, device):
|
|
| 37 |
|
| 38 |
# Predict NER labels
|
| 39 |
with torch.no_grad():
|
| 40 |
-
logits = model(
|
| 41 |
|
| 42 |
# Convert logits to label IDs
|
| 43 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
|
@@ -46,49 +59,18 @@ def predict_ner(model, tokenizer, text, label_map, device):
|
|
| 46 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
|
| 47 |
labels = [label_map[label_id] for label_id in label_ids[0]]
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
current_word = ''
|
| 52 |
-
current_label = 'O'
|
| 53 |
for token, label in zip(tokens, labels):
|
| 54 |
if token in ["<s>", "</s>"]:
|
| 55 |
continue
|
|
|
|
| 56 |
if token.startswith("▁"):
|
| 57 |
-
if
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
current_label = label
|
| 61 |
else:
|
| 62 |
-
|
| 63 |
-
if label != 'O':
|
| 64 |
-
current_label = label
|
| 65 |
-
if current_word:
|
| 66 |
-
result.append((current_word, current_label))
|
| 67 |
-
|
| 68 |
-
# Remove punctuation from results
|
| 69 |
-
result = [(word, label) for word, label in result if word]
|
| 70 |
-
|
| 71 |
-
return result
|
| 72 |
-
|
| 73 |
-
def ner(text):
|
| 74 |
-
predictions = predict_ner(model, tokenizer, text, label_map, device)
|
| 75 |
-
|
| 76 |
-
# Generate HTML output with color-coded entities
|
| 77 |
-
html_output = ""
|
| 78 |
-
last_index = 0
|
| 79 |
-
colors = {
|
| 80 |
-
'PERSON': 'lightblue',
|
| 81 |
-
'ORG': 'lightgreen',
|
| 82 |
-
'LOC': 'lightcoral',
|
| 83 |
-
# Add more colors for other labels if needed
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
for word, label in predictions:
|
| 87 |
-
color = colors.get(label, 'yellow') # Default color for unknown labels
|
| 88 |
-
html_output += f"{text[last_index:text.find(word, last_index)]}<span style='background-color: {color};'>{word}</span>"
|
| 89 |
-
last_index = text.find(word, last_index) + len(word)
|
| 90 |
-
|
| 91 |
-
html_output += text[last_index:] # Add the rest of the text after the last entity
|
| 92 |
|
| 93 |
return html_output
|
| 94 |
|
|
@@ -96,13 +78,16 @@ def ner(text):
|
|
| 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 |
|
|
|
|
|
|
|
|
|
|
| 99 |
# Define Gradio interface
|
| 100 |
interface = gr.Interface(
|
| 101 |
fn=ner,
|
| 102 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 103 |
-
outputs="
|
| 104 |
title="Named Entity Recognition with XLM-RoBERTa",
|
| 105 |
-
description="This app uses a fine-tuned XLM-RoBERTa model for Named Entity Recognition (NER). Enter
|
| 106 |
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from utils.data_utils import NerProcessor
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# Define colors for entity types
|
| 8 |
+
COLOR_MAP = {
|
| 9 |
+
'B-PER': 'red',
|
| 10 |
+
'I-PER': 'darkred',
|
| 11 |
+
'B-ORG': 'blue',
|
| 12 |
+
'I-ORG': 'darkblue',
|
| 13 |
+
'B-LOC': 'green',
|
| 14 |
+
'I-LOC': 'darkgreen',
|
| 15 |
+
'B-MISC': 'purple',
|
| 16 |
+
'I-MISC': 'darkpurple',
|
| 17 |
+
'O': 'black'
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
def load_model(pretrained_path, model_path, device):
|
| 21 |
# Load the processor and label map
|
| 22 |
processor = NerProcessor()
|
|
|
|
| 50 |
|
| 51 |
# Predict NER labels
|
| 52 |
with torch.no_grad():
|
| 53 |
+
logits = model(inputs_ids=input_ids, labels=None, labels_mask=None, valid_mask=None)
|
| 54 |
|
| 55 |
# Convert logits to label IDs
|
| 56 |
label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
|
|
|
|
| 59 |
tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
|
| 60 |
labels = [label_map[label_id] for label_id in label_ids[0]]
|
| 61 |
|
| 62 |
+
# Build HTML with colored entities
|
| 63 |
+
html_output = ""
|
|
|
|
|
|
|
| 64 |
for token, label in zip(tokens, labels):
|
| 65 |
if token in ["<s>", "</s>"]:
|
| 66 |
continue
|
| 67 |
+
color = COLOR_MAP.get(label, 'black')
|
| 68 |
if token.startswith("▁"):
|
| 69 |
+
if html_output:
|
| 70 |
+
html_output += ' '
|
| 71 |
+
html_output += f'<span style="color: {color};">{token[1:]}</span>'
|
|
|
|
| 72 |
else:
|
| 73 |
+
html_output += f'<span style="color: {color};">{token}</span>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
return html_output
|
| 76 |
|
|
|
|
| 78 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 79 |
model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)
|
| 80 |
|
| 81 |
+
def ner(text):
|
| 82 |
+
return predict_ner(model, tokenizer, text, label_map, device)
|
| 83 |
+
|
| 84 |
# Define Gradio interface
|
| 85 |
interface = gr.Interface(
|
| 86 |
fn=ner,
|
| 87 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here...", label="Input Text"),
|
| 88 |
+
outputs=gr.HTML(label="NER Output"),
|
| 89 |
title="Named Entity Recognition with XLM-RoBERTa",
|
| 90 |
+
description="This app uses a fine-tuned XLM-RoBERTa model for Named Entity Recognition (NER). Enter text to see NER predictions with colored entity highlights."
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|