File size: 1,527 Bytes
677d8a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()