Commit
·
677d8a9
1
Parent(s):
c80c5c7
initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
title = "Token Classification"
|
6 |
+
description = """
|
7 |
+
Label the entities of a sentence as:
|
8 |
+
1. person(PER),
|
9 |
+
2. organization(ORG),
|
10 |
+
3. location(LOC)
|
11 |
+
4. miscellaneous(MISC).
|
12 |
+
<img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px>
|
13 |
+
"""
|
14 |
+
|
15 |
+
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)"
|
16 |
+
|
17 |
+
textbox = gr.Textbox(label="Type your sentence here :", placeholder="My name is Bill Gates.", lines=3)
|
18 |
+
|
19 |
+
model = pipeline('token-classification',model='neural-net-rahul/bert-finetuned-ner')
|
20 |
+
|
21 |
+
def predict(text):
|
22 |
+
result = []
|
23 |
+
for dicti in model(text):
|
24 |
+
entity,word = dicti['entity'],dicti['word']
|
25 |
+
if entity == "B-PER" or entity=='I-PER':
|
26 |
+
entity = "Person"
|
27 |
+
elif entity == "B-LOC" or entity=='I-LOC':
|
28 |
+
entity = "Location"
|
29 |
+
elif entity == "B-ORG" or entity=='I-ORG':
|
30 |
+
entity = "Organization"
|
31 |
+
elif entity == "B-MISC" or entity=='I-MISC':
|
32 |
+
entity = "Miscellaneous"
|
33 |
+
result.append({entity,word})
|
34 |
+
return result
|
35 |
+
|
36 |
+
gr.Interface(
|
37 |
+
fn=predict,
|
38 |
+
inputs=textbox,
|
39 |
+
outputs=[gr.Text()],
|
40 |
+
title=title,
|
41 |
+
description=description,
|
42 |
+
article=article,
|
43 |
+
examples=[["Mark founded Facebook, shaping global social media connectivity."], ["Delhi is the most beautiful state after Kerala"]],
|
44 |
+
).launch()
|