Views
No views yet
xlm-roberta-largeminute_id, date, meeting_type, location, begin_time, end_time, participant1from transformers import AutoTokenizer, AutoModelForTokenClassification
2import torch
3
4# Load model and tokenizer
5MODEL_NAME = "inesctec/Citilink-XLMR-large-Metadata-en-baseline"
6tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
7model = AutoModelForTokenClassification.from_pretrained(MODEL_NAME)
8model.eval()
9
10# Example text
11text = "MINUTES NO. 2 – Mandate 2021-2025\nEXTRAORDINARY MEETING 10/22/2021\nMUNICIPALITY OF ALANDROAL\nMr. João Maria Aranha Grilo, Mayor of Alandroal, presided.\nCouncillors João Carlos Camões Roma Balsante\nPaulo Jorge da Silva Gonçalves\nFernanda Manuela Brites Romão\nJosé Francisco Figueira Andrezo Rodrigues\nHe was the secretary of the meeting ************************************************\nIn the Headquarters Building of the Municipality of Alandroal, the Mayor, João Maria Aranha Grilo, declared the meeting open, it was 2.15 pm. \n \n1. REQUEST FOR SCHEDULING AN EXTRAORDINARY MEETING OF THE MUNICIPAL ASSEMBLY.\n"
12
13# Tokenize with offset mapping
14inputs = tokenizer(
15 text,
16 return_tensors="en",
17 truncation=True,
18 max_length=512,
19 return_offsets_mapping=True
20)
21offsets = inputs.pop("offset_mapping")[0]
22
23# Predict
24with torch.no_grad():
25 outputs = model(**inputs)
26
27predictions = torch.argmax(outputs.logits, dim=2)[0]
28labels = [model.config.id2label[p.item()] for p in predictions]
29
30# Extract entities using character spans
31entities = []
32current = None
33
34for (start, end), label in zip(offsets.tolist(), labels):
35 if label == "O" or start == end:
36 if current:
37 entities.append(current)
38 current = None
39 continue
40
41 if label.startswith("B-"):
42 if current:
43 entities.append(current)
44 current = {"label": label[2:], "start": start, "end": end}
45 elif label.startswith("I-") and current and label[2:] == current["label"]:
46 current["end"] = end
47 else:
48 if current:
49 entities.append(current)
50 current = None
51
52if current:
53 entities.append(current)
54
55# Print results
56print("\nDetected Entities:")
57for ent in entities:
58 span = text[ent["start"]:ent["end"]]
59 print(f"- {ent['label']}: {span}")| Metric | Score |
|---|---|
| F1 score | 0.94 |
| Precision | 0.94 |
| Recall | 0.94 |