| import gradio as gr | |
| import numpy as np | |
| from transformers import pipeline | |
| title = "Token Classification" | |
| description = """ | |
| Label the entities of a sentence as: | |
| 1. person(PER), | |
| 2. organization(ORG), | |
| 3. location(LOC) | |
| 4. miscellaneous(MISC). | |
| <img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px> | |
| """ | |
| article = "Check out [my github repository](https://github.com/Neural-Net-Rahul/P2-Token-Classification-using-Fine-tuned-Hugging-face-transformer) and my [fine tuned model](https://huggingface.co/neural-net-rahul/bert-finetuned-ner)" | |
| textbox = gr.Textbox(label="Type your sentence here :", placeholder="My name is Bill Gates.", lines=3) | |
| model = pipeline('token-classification',model='neural-net-rahul/bert-finetuned-ner') | |
| def predict(text): | |
| result = [] | |
| for dicti in model(text): | |
| entity,word = dicti['entity'],dicti['word'] | |
| if entity == "B-PER" or entity=='I-PER': | |
| entity = "Person" | |
| elif entity == "B-LOC" or entity=='I-LOC': | |
| entity = "Location" | |
| elif entity == "B-ORG" or entity=='I-ORG': | |
| entity = "Organization" | |
| elif entity == "B-MISC" or entity=='I-MISC': | |
| entity = "Miscellaneous" | |
| result.append({entity,word}) | |
| return result | |
| gr.Interface( | |
| fn=predict, | |
| inputs=textbox, | |
| outputs=[gr.Text()], | |
| title=title, | |
| description=description, | |
| article=article, | |
| examples=[["Mark founded Facebook, shaping global social media connectivity."], ["Delhi is the most beautiful state after Kerala"]], | |
| ).launch() |