Views
No views yet
bert-base-multilingual-cased. Обучена на размеченных данных в формате JSON для выделения специализированных терминов и сущностей.1ner-model/
2├── config.json
3├── pytorch_model.bin
4├── special_tokens_map.json
5├── tokenizer_config.json
6├── vocab.txtprocessed_data.json) без заголовков, где:tokens - список токенов предложенияner_tags - соответствующие метки сущностейcbert-base-multilingual-cased50264AdamW1from transformers import BertTokenizerFast, BertForTokenClassification
2import torch
3
4# Загрузка модели и токенизатора
5model = BertForTokenClassification.from_pretrained("RimasZzz/agricultural-ner-model")
6tokenizer = BertTokenizerFast.from_pretrained("RimasZzz/agricultural-ner-model")
7
8def predict_entities(text):
9 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=64)
10 with torch.no_grad():
11 outputs = model(**inputs)
12 predictions = torch.argmax(outputs.logits, dim=-1)[0].tolist()
13
14 tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
15 entities = []
16 current_entity = []
17 current_label = None
18
19 for token, pred in zip(tokens, predictions):
20 label = model.config.id2label[pred]
21 if label.startswith("B-"):
22 if current_entity:
23 entities.append((" ".join(current_entity), current_label))
24 current_entity = [token]
25 current_label = label[2:]
26 elif label.startswith("I-") and current_label == label[2:]:
27 current_entity.append(token)
28 else:
29 if current_entity:
30 entities.append((" ".join(current_entity), current_label))
31 current_entity = []
32 current_label = None
33
34 return entities
35
36# Пример:
37text = "Провели обработку посевов пшеницы гербицидом"
38entities = predict_entities(text)
39print(entities)
40# [('пшеницы', 'CROP')...]