File size: 1,460 Bytes
3d1d2fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0294383
 
 
 
 
 
 
 
 
3d1d2fa
 
 
0294383
 
 
 
3d1d2fa
 
 
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
from transformers import pipeline
import gradio as gr

#reference appropriate Hugging Face model
model_name = 'koakande/bert-finetuned-ner'

# Load token classification pipeline modelfrom Hugging Face
model = pipeline("token-classification", model=model_name, aggregation_strategy="simple")

# write a prediction method for the model
def predict_entities(text):
    # Use the loaded model to identify entities in the text
    entities = model(text)
    # Highlight identified entities in the input text
    highlighted_text = text
    for entity in entities:
        entity_text = text[entity['start']:entity['end']]
        replacement = f"<span style='border: 2px solid green;'>{entity_text}</span>"
        highlighted_text = highlighted_text.replace(entity_text, replacement)
    return highlighted_text

# gradio interface
title = "Named Entity Recognizer"

description = """
This model has been trained to identify entities in a given text. It returns the input text with the entities highlighted in green. Give it a try!
"""

article = "The model is trained using bert-finetuned-ner."

iface  = gr.Interface(
    fn=predict_entities,
    inputs=gr.Textbox(lines=5, placeholder="Enter text..."),
    outputs=gr.HTML(),
    title=title,
    description=description,
    article=article,
    examples=[["Hello, I am Kabeer. I work as a machine learning engineer at OVO in the UK"], ["This is Maryam who is a Leicester based NHS Doctor"]],
)

iface.launch()