Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
# Load the NER pipeline from Hugging Face
|
|
@@ -6,21 +6,30 @@ ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-e
|
|
| 6 |
|
| 7 |
# Function to perform NER
|
| 8 |
def perform_ner(text):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"Entity":
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Define the Gradio interface
|
| 26 |
title = "Named Entity Recognition (NER) App"
|
|
@@ -44,3 +53,4 @@ interface.launch()
|
|
| 44 |
|
| 45 |
|
| 46 |
|
|
|
|
|
|
| 1 |
+
iimport gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
# Load the NER pipeline from Hugging Face
|
|
|
|
| 6 |
|
| 7 |
# Function to perform NER
|
| 8 |
def perform_ner(text):
|
| 9 |
+
try:
|
| 10 |
+
# Run NER on the input text
|
| 11 |
+
results = ner_pipeline(text)
|
| 12 |
+
|
| 13 |
+
# Check for empty results
|
| 14 |
+
if not results:
|
| 15 |
+
return [{"Entity": "-", "Word": "-", "Score": "-", "Start": "-", "End": "-"}]
|
| 16 |
+
|
| 17 |
+
# Format results for better readability
|
| 18 |
+
formatted_results = [
|
| 19 |
+
{
|
| 20 |
+
"Entity": result.get("entity", "-"),
|
| 21 |
+
"Word": result.get("word", "-"),
|
| 22 |
+
"Score": round(result.get("score", 4), 4),
|
| 23 |
+
"Start": result.get("start", "-"),
|
| 24 |
+
"End": result.get("end", "-")
|
| 25 |
+
}
|
| 26 |
+
for result in results
|
| 27 |
+
]
|
| 28 |
+
return formatted_results
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error: {e}")
|
| 31 |
+
# Return a default error response
|
| 32 |
+
return [{"Entity": "Error", "Word": "Error", "Score": 0, "Start": 0, "End": 0}]
|
| 33 |
|
| 34 |
# Define the Gradio interface
|
| 35 |
title = "Named Entity Recognition (NER) App"
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
+
|