Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
pipeline,
|
| 4 |
+
AutoModelForTokenClassification,
|
| 5 |
+
AutoTokenizer
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
# 1️⃣ Load a fast tokenizer and model for NER
|
| 9 |
+
ner_tokenizer = AutoTokenizer.from_pretrained(
|
| 10 |
+
"elastic/distilbert-base-uncased-finetuned-conll03-english",
|
| 11 |
+
use_fast=True
|
| 12 |
+
)
|
| 13 |
+
ner_model = AutoModelForTokenClassification.from_pretrained(
|
| 14 |
+
"elastic/distilbert-base-uncased-finetuned-conll03-english"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# 2️⃣ Create the NER pipeline with proper aggregation
|
| 18 |
+
def get_ner_pipeline():
|
| 19 |
+
return pipeline(
|
| 20 |
+
"ner",
|
| 21 |
+
model=ner_model,
|
| 22 |
+
tokenizer=ner_tokenizer,
|
| 23 |
+
aggregation_strategy="simple"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# 3️⃣ Define the Gradio processing function
|
| 27 |
+
def process(text, features):
|
| 28 |
+
result = {}
|
| 29 |
+
if "Entities" in features:
|
| 30 |
+
ner = get_ner_pipeline()
|
| 31 |
+
ents = ner(text)
|
| 32 |
+
# ents will now contain properly merged tokens
|
| 33 |
+
result["entities"] = [
|
| 34 |
+
{"word": e["word"], "type": e["entity_group"]}
|
| 35 |
+
for e in ents
|
| 36 |
+
]
|
| 37 |
+
return result
|
| 38 |
+
|
| 39 |
+
# 4️⃣ Build and launch the Gradio UI
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## Named Entity Recognition Fix Demo")
|
| 42 |
+
inp = gr.Textbox(lines=2, placeholder="Enter text here…")
|
| 43 |
+
feats = gr.CheckboxGroup(
|
| 44 |
+
["Entities"], label="Select features to run"
|
| 45 |
+
)
|
| 46 |
+
btn = gr.Button("Run")
|
| 47 |
+
out = gr.JSON(label="Results")
|
| 48 |
+
btn.click(process, [inp, feats], out)
|
| 49 |
+
|
| 50 |
+
demo.queue(api_open=True).launch()
|