-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
30 lines (26 loc) · 1.01 KB
/
app.py
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
import gradio as gr
from transformers import pipeline
# Load NER model from Hugging Face Hub
model_checkpoint = "./" # Replace with your Hugging Face repo
ner_pipeline = pipeline("token-classification", model=model_checkpoint, tokenizer=model_checkpoint)
# Define function for Gradio
def ner_prediction(text):
entities = ner_pipeline(text)
output = []
for entity in entities:
word = entity["word"]
label = entity["entity"]
confidence = round(entity["score"], 4)
output.append(f"{word}: {label} ({confidence})")
return "\n".join(output)
# Create Gradio interface
iface = gr.Interface(
fn=ner_prediction,
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
outputs="text",
title="Named Entity Recognition (NER)",
description="Enter a sentence and see which words are recognized as named entities.",
examples=[["Barack Obama was born in Hawaii."], ["Google is headquartered in Mountain View, California."]]
)
# Run the Gradio app
iface.launch()