BF_NER
36
license:mit
by
CharlesAbdoulaye
Other
OTHER
New
36 downloads
Early-stage
Edge AI:
Mobile
Laptop
Server
Unknown
Mobile
Laptop
Server
Quick Summary
AI model with specialized capabilities.
Code Examples
Usagebash
pip install transformers torchUsagepythontransformers
from transformers import CamembertTokenizerFast, CamembertForTokenClassification
import torch
# Load model and tokenizer
tokenizer = CamembertTokenizerFast.from_pretrained("CharlesAbdoulaye/BF_NER")
model = CamembertForTokenClassification.from_pretrained("CharlesAbdoulaye/BF_NER")
model.eval()
# Entity labels
label_list = [
"O",
"B-country", "I-country",
"B-region", "I-region",
"B-departement", "I-departement",
"B-province", "I-province",
"B-village", "I-village"
]
id2label = {i: label for i, label in enumerate(label_list)}
# Example text with all 5 entity types
text = "La crise alimentaire au Burkina Faso a frappe la region des Hauts-Bassins et la province du Houet. La ville de Bobo-Dioulasso et le village de Sya sont particulierement touches."
# Tokenize with offset mapping for span extraction
inputs = tokenizer(text, return_tensors="pt", truncation=True, return_offsets_mapping=True)
offset_mapping = inputs.pop("offset_mapping")[0].tolist()
with torch.no_grad():
outputs = model(**inputs)
preds = torch.argmax(outputs.logits, dim=2)[0].tolist()
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
# Reconstruct entities with character spans
entities, current = [], None
for idx, (pred_id, (start, end)) in enumerate(zip(preds, offset_mapping)):
if start == 0 and end == 0:
if current: entities.append(current); current = None
continue
label = id2label[pred_id]
is_subword = not tokens[idx].startswith("\u2581")
if label.startswith("B-"):
if current: entities.append(current)
current = {"type": label[2:], "start": start, "end": end}
elif label.startswith("I-") or (is_subword and current):
if current: current["end"] = end
else:
if current: entities.append(current); current = None
if current: entities.append(current)
# Merge consecutive same-type entities (handles hyphenated names)
merged = []
for ent in entities:
if merged and merged[-1]["type"] == ent["type"]:
gap = text[merged[-1]["end"]:ent["start"]]
if gap in ("", "-", " -", "- "):
merged[-1]["end"] = ent["end"]; continue
merged.append(dict(ent))
for ent in merged:
ent["text"] = text[ent["start"]:ent["end"]].rstrip(".,;:!?")
print(f'{ent["text"]:20s} | {ent["type"]:15s} | span=({ent["start"]}, {ent["end"]})')Deploy This Model
Production-ready deployment in minutes
Together.ai
Instant API access to this model
Production-ready inference API. Start free, scale to millions.
Try Free APIReplicate
One-click model deployment
Run models in the cloud with simple API. No DevOps required.
Deploy NowDisclosure: We may earn a commission from these partners. This helps keep LLMYourWay free.