Commit
·
5436b2b
1
Parent(s):
b5d1f19
lets highlight some entiteis
Browse files
app.py
CHANGED
|
@@ -7,34 +7,40 @@ MODEL_NAME = "impresso-project/ner-stacked-bert-multilingual"
|
|
| 7 |
# Load the tokenizer and model using the pipeline
|
| 8 |
ner_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
|
| 10 |
-
ner_pipeline = pipeline(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
for entity in entities:
|
| 19 |
-
entity_info = f"Entity: {entity['entity']} | Confidence: {entity['score']:.2f}% | Text: {entity['word'].strip()} | Start: {entity['start']} | End: {entity['end']}"
|
| 20 |
-
entity_details.append(entity_info)
|
| 21 |
-
return "\n".join(entity_details)
|
| 22 |
|
| 23 |
# Function to process the sentence and extract entities
|
| 24 |
def extract_entities(sentence):
|
| 25 |
results = ner_pipeline(sentence)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Extract and format the entities
|
| 29 |
-
for
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Create Gradio interface
|
| 35 |
def ner_app_interface():
|
| 36 |
-
input_sentence = gr.Textbox(
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Interface definition
|
| 40 |
interface = gr.Interface(
|
|
@@ -42,12 +48,17 @@ def ner_app_interface():
|
|
| 42 |
inputs=input_sentence,
|
| 43 |
outputs=output_entities,
|
| 44 |
title="Named Entity Recognition",
|
| 45 |
-
description="Enter a sentence to extract named entities using the NER model from the Impresso project."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
-
|
| 48 |
interface.launch()
|
| 49 |
|
|
|
|
| 50 |
# Run the app
|
| 51 |
if __name__ == "__main__":
|
| 52 |
ner_app_interface()
|
| 53 |
-
|
|
|
|
| 7 |
# Load the tokenizer and model using the pipeline
|
| 8 |
ner_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
|
| 10 |
+
ner_pipeline = pipeline(
|
| 11 |
+
"generic-ner",
|
| 12 |
+
model=MODEL_NAME,
|
| 13 |
+
tokenizer=ner_tokenizer,
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
device="cpu",
|
| 16 |
+
)
|
| 17 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Function to process the sentence and extract entities
|
| 20 |
def extract_entities(sentence):
|
| 21 |
results = ner_pipeline(sentence)
|
| 22 |
+
entities_with_confidences = []
|
| 23 |
+
|
| 24 |
+
# Extract and format the entities for highlighting
|
| 25 |
+
for entity in results:
|
| 26 |
+
entities_with_confidences.append(
|
| 27 |
+
(
|
| 28 |
+
entity["word"],
|
| 29 |
+
entity["start"],
|
| 30 |
+
entity["end"],
|
| 31 |
+
f"{entity['entity']} ({entity['score']:.2f}%)",
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return {"text": sentence, "entities": entities_with_confidences}
|
| 36 |
+
|
| 37 |
|
| 38 |
# Create Gradio interface
|
| 39 |
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.HighlightedText(label="Extracted Entities")
|
| 44 |
|
| 45 |
# Interface definition
|
| 46 |
interface = gr.Interface(
|
|
|
|
| 48 |
inputs=input_sentence,
|
| 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()
|
| 60 |
|
| 61 |
+
|
| 62 |
# Run the app
|
| 63 |
if __name__ == "__main__":
|
| 64 |
ner_app_interface()
|
|
|