Commit
·
e1c577b
1
Parent(s):
5436b2b
update app
Browse files
app.py
CHANGED
|
@@ -16,23 +16,25 @@ ner_pipeline = pipeline(
|
|
| 16 |
)
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Function to process the sentence and extract entities
|
| 20 |
def extract_entities(sentence):
|
| 21 |
results = ner_pipeline(sentence)
|
| 22 |
-
|
| 23 |
|
| 24 |
-
# Extract and format the entities
|
| 25 |
-
for
|
| 26 |
-
|
| 27 |
-
(
|
| 28 |
-
entity["word"],
|
| 29 |
-
entity["start"],
|
| 30 |
-
entity["end"],
|
| 31 |
-
f"{entity['entity']} ({entity['score']:.2f}%)",
|
| 32 |
-
)
|
| 33 |
-
)
|
| 34 |
|
| 35 |
-
return
|
| 36 |
|
| 37 |
|
| 38 |
# Create Gradio interface
|
|
@@ -40,7 +42,7 @@ def ner_app_interface():
|
|
| 40 |
input_sentence = gr.Textbox(
|
| 41 |
lines=5, label="Input Sentence", placeholder="Enter a sentence for NER..."
|
| 42 |
)
|
| 43 |
-
output_entities = gr.
|
| 44 |
|
| 45 |
# Interface definition
|
| 46 |
interface = gr.Interface(
|
|
@@ -49,11 +51,6 @@ def ner_app_interface():
|
|
| 49 |
outputs=output_entities,
|
| 50 |
title="Named Entity Recognition",
|
| 51 |
description="Enter a sentence to extract named entities using the NER model from the Impresso project.",
|
| 52 |
-
examples=[
|
| 53 |
-
[
|
| 54 |
-
"In the year 1789, King Louis XVI, ruler of France, convened the Estates-General at the Palace of Versailles."
|
| 55 |
-
]
|
| 56 |
-
],
|
| 57 |
)
|
| 58 |
|
| 59 |
interface.launch()
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
|
| 19 |
+
# Helper function to print entities nicely
|
| 20 |
+
def print_nicely(entities):
|
| 21 |
+
entity_details = []
|
| 22 |
+
for entity in entities:
|
| 23 |
+
entity_info = f"Entity: {entity['entity']} | Confidence: {entity['score']:.2f}% | Text: {entity['word'].strip()} | Start: {entity['start']} | End: {entity['end']}"
|
| 24 |
+
entity_details.append(entity_info)
|
| 25 |
+
return "\n".join(entity_details)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
# Function to process the sentence and extract entities
|
| 29 |
def extract_entities(sentence):
|
| 30 |
results = ner_pipeline(sentence)
|
| 31 |
+
entity_results = []
|
| 32 |
|
| 33 |
+
# Extract and format the entities
|
| 34 |
+
for key in results.keys():
|
| 35 |
+
entity_results.append(print_nicely(results[key]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
return "\n".join(entity_results)
|
| 38 |
|
| 39 |
|
| 40 |
# Create Gradio interface
|
|
|
|
| 42 |
input_sentence = gr.Textbox(
|
| 43 |
lines=5, label="Input Sentence", placeholder="Enter a sentence for NER..."
|
| 44 |
)
|
| 45 |
+
output_entities = gr.Textbox(label="Extracted Entities")
|
| 46 |
|
| 47 |
# Interface definition
|
| 48 |
interface = gr.Interface(
|
|
|
|
| 51 |
outputs=output_entities,
|
| 52 |
title="Named Entity Recognition",
|
| 53 |
description="Enter a sentence to extract named entities using the NER model from the Impresso project.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
interface.launch()
|