File size: 1,779 Bytes
156d719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc3eeef
ead6d87
 
 
 
 
 
 
156d719
4fa5fa9
 
e4e2d8c
4fa5fa9
 
 
 
 
ead6d87
4fa5fa9
156d719
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
import gradio as gr
from transformers import pipeline

def token_classification(sentence):
    model_checkpoint = "AirrStorm/bert-ner-finetuned"
    token_classifier = pipeline(
        "token-classification", model=model_checkpoint, aggregation_strategy="simple"
    )

    # Get predictions
    results = token_classifier(sentence)

    # Clean and format the results
    formatted_results = "Named Entities Found:\n"
    for result in results:
        entity = result['entity_group']
        word = result['word']
        formatted_results += f"- {entity}: {word}\n"
    
    return formatted_results

examples = [
        ["Elon Musk founded SpaceX in California."],
        ["Cristiano Ronaldo plays for Al Nassr in Saudi Arabia."],
        ["Apple Inc. was founded by Steve Jobs. It is headquartered in Cupertino, California."],
        ["Albert Einstein developed the theory of relativity in Germany."],
        ["Harry Potter studied at Hogwarts in the United Kingdom."]
                ]

# Gradio interface
demo = gr.Interface(
    fn=token_classification, 
    inputs=gr.Textbox(label="Enter your sentence here", placeholder= "Enter a sentence to extract named entities (e.g., 'Lionel Messi plays for Inter Miami in the United States.')", lines=3),
    outputs=gr.Textbox(label="Extracted Named Entities", placeholder="The extracted entities will appear here", lines=6),
    title="Named Entity Recognition",
    description="Extract and classify named entities like people, organizations, and locations from the input text.",
    theme="dark-grass",  # Optional: Use a clean and minimal theme
    allow_flagging="never",  # Optional: Disable flagging to reduce distractions
    examples=examples,  # Optional: Add examples to make it easier for users
)
demo.launch()