Downloads · 30 days
10
33% of all-time downloads
harry-kpv-hf/ai-rec-ner-onnx-model
ai-rec-ner-onnx-model is a machine learning model from harry-kpv-hf. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This repository provides an ONNX-optimized Named Entity Recognition (NER) model for extracting key personal data such as names, cities, and countries from text. The model is optimized for fast inference using ONNX Run…
Downloads · 30 days
10
33% of all-time downloads
All-time downloads
30
Public
Repo size
709 MB
Likes
0
Public
Click a slice to open those files.
.onnx709 MB · 99%
From the Hugging Face model README
This repository provides an ONNX-optimized Named Entity Recognition (NER) model for extracting key personal data such as names, cities, and countries from text. The model is optimized for fast inference using ONNX Runtime.
optimum library.pip install optimum[onnxruntime] transformers
from typing import Dict, List
from optimum.onnxruntime import ORTModelForTokenClassification
from transformers import AutoTokenizer, pipeline
def extract_entities(text: str) -> Dict[str, str]:
"""
Extracts personal data entities from a given text using the ONNX model.
Args:
text (str): Input text to extract entities from.
Returns:
dict: Extracted entities, e.g., {'first_name': 'John', 'city': 'New York'}
"""
# Load the ONNX model and tokenizer
model = ORTModelForTokenClassification.from_pretrained(
"harry-kpv-hf/ai-rec-ner-onnx-model", # Replace with your Hugging Face model repo
provider="CPUExecutionProvider"
)
tokenizer = AutoTokenizer.from_pretrained("harry-kpv-hf/ai-rec-ner-onnx-model")
# Create a pipeline for NER
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
# Run inference
ner_results = ner_pipeline(text)
# Define mapping of entity types to readable keys
entity_mapping = {
"U-FIRST_NAME": "first_name",
"U-MIDDLE_NAME": "middle_name",
"U-LAST_NAME": "last_name",
"U-CITY": "city",
"U-COUNTRY": "country",
}
# Process results to create a dictionary of extracted entities
extracted_data = {}
for entity in ner_results:
entity_type = entity["entity"]
if entity_type in entity_mapping and entity_mapping[entity_type] not in extracted_data:
extracted_data[entity_mapping[entity_type]] = entity["word"].replace("##", "")
return extracted_data
# Example usage
text = "John Doe lives in New York, USA."
print(extract_entities(text))
# Output: {'first_name': 'John', 'last_name': 'Doe', 'city': 'New York', 'country': 'USA'}