Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,7 @@ def wrap_token_with_color(token, labels):
|
|
| 32 |
# Build nested highlights
|
| 33 |
style = "position: relative;"
|
| 34 |
for label in labels:
|
| 35 |
-
if label != "O":
|
| 36 |
style += f"background: {label_colors[label]};"
|
| 37 |
return f"<span style='{style}'>{token}</span>"
|
| 38 |
|
|
@@ -56,7 +56,10 @@ def predict_ner_tags(sentence):
|
|
| 56 |
if token not in tokenizer.all_special_tokens:
|
| 57 |
# Extract the labels for this token
|
| 58 |
label_indices = (predicted_labels[0][i] == 1).nonzero(as_tuple=False).squeeze(-1)
|
| 59 |
-
labels = [id2label[idx.item()][2:] for idx in label_indices
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Check if labels are the same as the previous token (for seamless highlighting)
|
| 62 |
if labels != prev_labels:
|
|
@@ -65,7 +68,9 @@ def predict_ner_tags(sentence):
|
|
| 65 |
|
| 66 |
# Start a new span
|
| 67 |
if labels != ["O"]:
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Add the token to the span
|
| 71 |
highlighted_sentence += token.replace("##", "")
|
|
@@ -90,4 +95,4 @@ iface = gr.Interface(
|
|
| 90 |
)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
iface.launch()
|
|
|
|
| 32 |
# Build nested highlights
|
| 33 |
style = "position: relative;"
|
| 34 |
for label in labels:
|
| 35 |
+
if label != "O" and label in label_colors:
|
| 36 |
style += f"background: {label_colors[label]};"
|
| 37 |
return f"<span style='{style}'>{token}</span>"
|
| 38 |
|
|
|
|
| 56 |
if token not in tokenizer.all_special_tokens:
|
| 57 |
# Extract the labels for this token
|
| 58 |
label_indices = (predicted_labels[0][i] == 1).nonzero(as_tuple=False).squeeze(-1)
|
| 59 |
+
labels = [id2label[idx.item()][2:] for idx in label_indices if idx.item() in id2label] # Safe lookup
|
| 60 |
+
|
| 61 |
+
if not labels: # Handle empty labels gracefully
|
| 62 |
+
labels = ["O"]
|
| 63 |
|
| 64 |
# Check if labels are the same as the previous token (for seamless highlighting)
|
| 65 |
if labels != prev_labels:
|
|
|
|
| 68 |
|
| 69 |
# Start a new span
|
| 70 |
if labels != ["O"]:
|
| 71 |
+
highlight_colors = [label_colors[label] for label in labels if label in label_colors]
|
| 72 |
+
if highlight_colors: # Only create gradient if valid colors exist
|
| 73 |
+
highlighted_sentence += f"<span style='background: linear-gradient({', '.join(highlight_colors)});'>"
|
| 74 |
|
| 75 |
# Add the token to the span
|
| 76 |
highlighted_sentence += token.replace("##", "")
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
iface.launch(share=True)
|