Spaces:
Running
Running
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the multilingual NER pipeline | |
| ner = pipeline("ner", model="Davlan/xlm-roberta-base-ner-hrl", grouped_entities=True) | |
| # Inference function | |
| def extract_entities(text): | |
| results = ner(text) | |
| return [(ent['word'], ent['entity_group']) for ent in results] | |
| # User instructions and placeholder to show above app | |
| instructions = """ | |
| ### π Multilingual Named Entity Recognition (NER) App | |
| πΉ Enter a sentence in **any supported language** (English, French, Hindi, Spanish, etc.) | |
| πΉ The app will detect and classify entities such as: | |
| - `PER` (Person) | |
| - `LOC` (Location) | |
| - `ORG` (Organization) | |
| - `MISC` (Miscellaneous) | |
| β οΈ Some languages like **Japanese** may show minor inaccuracies. | |
| **Example Input:** | |
| `Barack Obama was born in Hawaii and served as the 44th President of the United States.` | |
| **Expected Output:** | |
| - Barack Obama β PER | |
| - Hawaii β LOC | |
| - United States β LOC | |
| """ | |
| # Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown(instructions) | |
| with gr.Row(): | |
| inp = gr.Textbox(label="Enter Text", placeholder="Type a sentence in any language...", lines=3) | |
| out = gr.HighlightedText(label="Named Entities") | |
| btn = gr.Button("Extract Entities") | |
| btn.click(fn=extract_entities, inputs=inp, outputs=out) | |
| # Launch | |
| if __name__ == "__main__": | |
| demo.launch() | |