File size: 2,003 Bytes
677d8a9 436cc00 677d8a9 436cc00 677d8a9 845c4ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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 = []
word1 = None
entity_past = None
for dicti in model(text):
entity,word = dicti['entity'],dicti['word']
if entity[0]=='B':
if word1 is not None:
if entity_past =='B-PER':
entity_past = 'Person'
elif entity_past =='B-ORG':
entity_past = 'Organization'
elif entity_past =='B-MISC':
entity_past = 'Miscellaneous'
elif entity_past =='B-LOC':
entity_past = 'Location'
result.append([word1,entity_past])
word1 = word;
entity_past = entity;
else:
word1 = word1 + word.lstrip("#");
if entity_past =='B-PER':
entity_past = 'Person'
elif entity_past =='B-ORG':
entity_past = 'Organization'
elif entity_past =='B-MISC':
entity_past = 'Miscellaneous'
elif entity_past =='B-LOC':
entity_past = 'Location'
result.append([word1,entity_past])
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(share=True) |