Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
ner = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def extract_entities(text):
|
| 7 |
results = ner(text)
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
with gr.Blocks() as demo:
|
| 11 |
gr.Markdown(instructions)
|
| 12 |
with gr.Row():
|
| 13 |
-
inp = gr.Textbox(label="Enter Text", placeholder="Type a sentence
|
| 14 |
-
|
|
|
|
| 15 |
btn = gr.Button("Extract Entities")
|
| 16 |
|
| 17 |
-
btn.click(fn=extract_entities, inputs=inp, outputs=
|
| 18 |
|
| 19 |
if __name__ == "__main__":
|
| 20 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
ner = pipeline(
|
| 5 |
+
"ner",
|
| 6 |
+
model="Keyurjotaniya007/xlm-roberta-base-xtreme-multilingual-ner-2.0",
|
| 7 |
+
aggregation_strategy="simple"
|
| 8 |
+
)
|
| 9 |
|
| 10 |
def extract_entities(text):
|
| 11 |
results = ner(text)
|
| 12 |
+
entities = [{"word": r["word"], "label": r.get("entity_group"), "score": float(r.get("score", 0))} for r in results]
|
| 13 |
+
|
| 14 |
+
annotated = " | ".join([f"{e['word']} ({e['label']})" for e in entities]) if entities else "No entities found."
|
| 15 |
+
|
| 16 |
+
return entities, annotated
|
| 17 |
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
gr.Markdown(instructions)
|
| 20 |
with gr.Row():
|
| 21 |
+
inp = gr.Textbox(label="Enter Text", placeholder="Type a sentence...", lines=3)
|
| 22 |
+
out_json = gr.JSON(label="Entities (JSON)")
|
| 23 |
+
out_text = gr.Textbox(label="Annotated Text")
|
| 24 |
btn = gr.Button("Extract Entities")
|
| 25 |
|
| 26 |
+
btn.click(fn=extract_entities, inputs=inp, outputs=[out_json, out_text])
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
demo.launch()
|