Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- README.md +6 -9
- app.py +60 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
---
|
2 |
-
title: Spanish
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
|
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.15.0
|
8 |
-
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Spanish-bert-based-NER
|
3 |
+
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: yellow
|
6 |
+
app_file: app.py
|
7 |
sdk: gradio
|
8 |
sdk_version: 5.15.0
|
9 |
+
---
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from collections import defaultdict
|
4 |
+
|
5 |
+
ner_pipeline = pipeline("ner", model="syubraj/spanish_bert_based_ner")
|
6 |
+
|
7 |
+
ENTITY_COLORS = {
|
8 |
+
"B-PER": "#FF5733",
|
9 |
+
"I-PER": "#FF8D1A",
|
10 |
+
"B-LOC": "#33FF57",
|
11 |
+
"I-LOC": "#2EB82E",
|
12 |
+
"B-ORG": "#3380FF",
|
13 |
+
"I-ORG": "#1A66FF",
|
14 |
+
"O": "#E0E0E0"
|
15 |
+
}
|
16 |
+
|
17 |
+
def highlight_entities(text):
|
18 |
+
"""
|
19 |
+
This function takes a Spanish text input and applies Named Entity Recognition (NER) using a pre-trained model.
|
20 |
+
The recognized entities are highlighted with distinct colors based on their categories.
|
21 |
+
A legend is also generated to indicate the meaning of each color.
|
22 |
+
|
23 |
+
Parameters:
|
24 |
+
text (str): The input Spanish text.
|
25 |
+
|
26 |
+
Returns:
|
27 |
+
str: The HTML formatted output with highlighted entities and a legend.
|
28 |
+
"""
|
29 |
+
entities = ner_pipeline(text)
|
30 |
+
formatted_text = ""
|
31 |
+
last_idx = 0
|
32 |
+
|
33 |
+
for ent in entities:
|
34 |
+
word = ent['word']
|
35 |
+
start, end = ent['start'], ent['end']
|
36 |
+
entity_label = ent['entity']
|
37 |
+
color = ENTITY_COLORS.get(entity_label, "#E0E0E0")
|
38 |
+
|
39 |
+
formatted_text += text[last_idx:start] + f'<span style="background-color:{color}; padding:2px; border-radius:5px;">{word}</span>'
|
40 |
+
last_idx = end
|
41 |
+
|
42 |
+
formatted_text += text[last_idx:]
|
43 |
+
|
44 |
+
# Generate legend
|
45 |
+
legend_html = "<div><b>Legend:</b><br>"
|
46 |
+
for label, color in ENTITY_COLORS.items():
|
47 |
+
legend_html += f'<span style="background-color:{color}; padding:2px 5px; border-radius:5px; margin-right:5px;">{label}</span>'
|
48 |
+
legend_html += "</div><br>"
|
49 |
+
|
50 |
+
return legend_html + formatted_text
|
51 |
+
|
52 |
+
grn = gr.Interface(
|
53 |
+
fn=highlight_entities,
|
54 |
+
inputs=gr.Textbox(label="Enter Spanish Text"),
|
55 |
+
outputs=gr.HTML(label="NER Highlighted Text"),
|
56 |
+
title="Spanish Named Entity Recognition",
|
57 |
+
description="This interactive demo performs Named Entity Recognition (NER) on Spanish text. Recognized entities such as persons, locations, and organizations are highlighted in distinct colors for better readability. A legend is provided to help interpret the color coding.",
|
58 |
+
)
|
59 |
+
|
60 |
+
grn.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio==5.15.0
|
2 |
+
transformers==4.48.2
|