Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
|
5 |
+
# Load transformer-based spaCy NER pipeline
|
6 |
+
nlp = spacy.load("en_core_web_trf") # Transformer-based model
|
7 |
+
|
8 |
+
def ner_extraction(text):
|
9 |
+
if not text.strip():
|
10 |
+
return "Please enter some text."
|
11 |
+
doc = nlp(text)
|
12 |
+
ents = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
|
13 |
+
if not ents:
|
14 |
+
return "No named entities found."
|
15 |
+
return ents
|
16 |
+
|
17 |
+
# Optional: visual output
|
18 |
+
def ner_visualizer(text):
|
19 |
+
doc = nlp(text)
|
20 |
+
html = displacy.render(doc, style="ent", minify=True)
|
21 |
+
return html
|
22 |
+
|
23 |
+
with gr.Blocks() as demo:
|
24 |
+
gr.Markdown("## Named Entity Recognition using spaCy + Transformers (en_core_web_trf)")
|
25 |
+
with gr.Tab("Extract Entities"):
|
26 |
+
inp = gr.Textbox(label="Enter Text", lines=3, placeholder="Type a sentence...")
|
27 |
+
out = gr.JSON(label="Named Entities")
|
28 |
+
btn = gr.Button("Run NER")
|
29 |
+
btn.click(ner_extraction, inputs=inp, outputs=out)
|
30 |
+
|
31 |
+
with gr.Tab("Visualize Entities"):
|
32 |
+
vis_inp = gr.Textbox(label="Enter Text", lines=3, placeholder="Type a sentence...")
|
33 |
+
vis_out = gr.HTML(label="Visualization")
|
34 |
+
vis_btn = gr.Button("Visualize")
|
35 |
+
vis_btn.click(ner_visualizer, inputs=vis_inp, outputs=vis_out)
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch()
|