Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,44 @@
|
|
| 1 |
-
# Import the required libraries
|
| 2 |
-
import os # os provides a way of using operating system-dependent functionality
|
| 3 |
-
import io # this library provides core tools for working with streams of data
|
| 4 |
-
from IPython.display import Image, display, HTML # This is used for displaying rich content (e.g., images, HTML) in Jupyter Notebooks
|
| 5 |
-
from PIL import Image # Python Imaging Library (PIL) is used for opening, manipulating, and saving image files
|
| 6 |
-
import base64 # This library encodes and decodes data in base64 format
|
| 7 |
-
import requests
|
| 8 |
-
import json
|
| 9 |
-
from dotenv import load_dotenv, find_dotenv
|
| 10 |
import gradio as gr
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Load
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the NER pipeline from Hugging Face
|
| 5 |
+
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
|
| 6 |
+
|
| 7 |
+
# Function to perform NER
|
| 8 |
+
def perform_ner(text):
|
| 9 |
+
# Run NER on the input text
|
| 10 |
+
results = ner_pipeline(text)
|
| 11 |
+
|
| 12 |
+
# Format results for better readability
|
| 13 |
+
formatted_results = [
|
| 14 |
+
{
|
| 15 |
+
"Entity": result["entity"],
|
| 16 |
+
"Word": result["word"],
|
| 17 |
+
"Score": round(result["score"], 4),
|
| 18 |
+
"Start": result["start"],
|
| 19 |
+
"End": result["end"]
|
| 20 |
+
}
|
| 21 |
+
for result in results
|
| 22 |
+
]
|
| 23 |
+
return formatted_results
|
| 24 |
+
|
| 25 |
+
# Define the Gradio interface
|
| 26 |
+
title = "Named Entity Recognition (NER) App"
|
| 27 |
+
description = "Enter text below to extract named entities like persons, organizations, and locations using a Hugging Face model."
|
| 28 |
+
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=perform_ner, # The function to process input
|
| 31 |
+
inputs=gr.Textbox(lines=5, label="Input Text"), # Text input for the user
|
| 32 |
+
outputs=gr.Dataframe( # Display results as a table
|
| 33 |
+
headers=["Entity", "Word", "Score", "Start", "End"],
|
| 34 |
+
label="Recognized Entities"
|
| 35 |
+
),
|
| 36 |
+
title=title,
|
| 37 |
+
description=description
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Launch the Gradio app
|
| 41 |
+
interface.launch()
|
| 42 |
|
| 43 |
|
| 44 |
|