Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -1,29 +1,60 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
-
|
5 |
-
#
|
6 |
-
models =
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
"
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Load the NER models
|
5 |
+
# Load the NER models
|
6 |
+
models = {
|
7 |
+
"dslim/bert-base-NER": pipeline(
|
8 |
+
"ner", model="dslim/bert-base-NER", grouped_entities=True
|
9 |
+
),
|
10 |
+
"dslim/bert-base-NER-uncased": pipeline(
|
11 |
+
"ner", model="dslim/bert-base-NER-uncased", grouped_entities=True
|
12 |
+
),
|
13 |
+
"dslim/bert-large-NER": pipeline(
|
14 |
+
"ner", model="dslim/bert-large-NER", grouped_entities=True
|
15 |
+
),
|
16 |
+
"dslim/distilbert-NER": pipeline(
|
17 |
+
"ner", model="dslim/distilbert-NER", grouped_entities=True
|
18 |
+
),
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
def process(text, model_name):
|
23 |
+
ner = models[model_name]
|
24 |
+
ner_results = ner(text)
|
25 |
+
highlighted_text = []
|
26 |
+
last_idx = 0
|
27 |
+
for entity in ner_results:
|
28 |
+
start = entity["start"]
|
29 |
+
end = entity["end"]
|
30 |
+
label = entity["entity_group"]
|
31 |
+
# Add non-entity text
|
32 |
+
if start > last_idx:
|
33 |
+
highlighted_text.append((text[last_idx:start], None))
|
34 |
+
# Add entity text
|
35 |
+
highlighted_text.append((text[start:end], label))
|
36 |
+
last_idx = end
|
37 |
+
# Add any remaining text after the last entity
|
38 |
+
if last_idx < len(text):
|
39 |
+
highlighted_text.append((text[last_idx:], None))
|
40 |
+
return highlighted_text
|
41 |
+
|
42 |
+
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("# Named Entity Recognition with BERT Models")
|
45 |
+
with gr.Row():
|
46 |
+
model_selector = gr.Dropdown(
|
47 |
+
choices=list(models.keys()),
|
48 |
+
value=list(models.keys())[0],
|
49 |
+
label="Select Model",
|
50 |
+
)
|
51 |
+
text_input = gr.Textbox(
|
52 |
+
label="Enter Text",
|
53 |
+
lines=5,
|
54 |
+
value="Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very close to the Manhattan Bridge.",
|
55 |
+
)
|
56 |
+
output = gr.HighlightedText(label="Named Entities")
|
57 |
+
analyze_button = gr.Button("Analyze")
|
58 |
+
analyze_button.click(process, inputs=[text_input, model_selector], outputs=output)
|
59 |
|
60 |
demo.launch()
|