Commit
·
d5d2a07
1
Parent(s):
4a4219b
first version
Browse files- app.py +53 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Define the model name
|
| 5 |
+
MODEL_NAME = "impresso-project/ner-stacked-bert-multilingual"
|
| 6 |
+
|
| 7 |
+
# Load the tokenizer and model using the pipeline
|
| 8 |
+
ner_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
|
| 10 |
+
ner_pipeline = pipeline("generic-ner", model=MODEL_NAME,
|
| 11 |
+
tokenizer=ner_tokenizer,
|
| 12 |
+
trust_remote_code=True,
|
| 13 |
+
device='cpu')
|
| 14 |
+
|
| 15 |
+
# Helper function to print entities nicely
|
| 16 |
+
def print_nicely(entities):
|
| 17 |
+
entity_details = []
|
| 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 |
+
entity_results = []
|
| 27 |
+
|
| 28 |
+
# Extract and format the entities
|
| 29 |
+
for key in results.keys():
|
| 30 |
+
entity_results.append(print_nicely(results[key]))
|
| 31 |
+
|
| 32 |
+
return "\n".join(entity_results)
|
| 33 |
+
|
| 34 |
+
# Create Gradio interface
|
| 35 |
+
def ner_app_interface():
|
| 36 |
+
input_sentence = gr.Textbox(lines=5, label="Input Sentence", placeholder="Enter a sentence for NER...")
|
| 37 |
+
output_entities = gr.Textbox(label="Extracted Entities")
|
| 38 |
+
|
| 39 |
+
# Interface definition
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=extract_entities,
|
| 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 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.3.1
|
| 2 |
+
transformers==4.36.0
|
| 3 |
+
sentencepiece
|