Views
No views yet
0 — нерелевантное сообщение1 — релевантное сообщениеai-forever/ruRoBERTa-largeAvitoRelevanceClassifier (encoder + GELU classification head)384 токенов3.0:10.912892 (epoch 5)0.9173990.9452630.97166779096067620.17model.safetensors через safetensors, затем создавайте архитектуру из config.json.1import json
2import torch
3from huggingface_hub import hf_hub_download
4from safetensors.torch import load_file
5from transformers import AutoModel, AutoTokenizer
6
7repo_id = "DanielNRU/Avito-relevance-ruroberta-v3-20260824"
8config_path = hf_hub_download(repo_id, "config.json")
9weights_path = hf_hub_download(repo_id, "model.safetensors")
10config = json.load(open(config_path, encoding="utf-8"))
11
12tokenizer = AutoTokenizer.from_pretrained(repo_id)
13
14class AvitoRelevanceClassifier(torch.nn.Module):
15 def __init__(self, cfg):
16 super().__init__()
17 self.encoder = AutoModel.from_pretrained(cfg["model_name"])
18 self.dropout1 = torch.nn.Dropout(cfg["dropout_encoder"])
19 self.dense = torch.nn.Linear(self.encoder.config.hidden_size, cfg["head_hidden_size"])
20 self.activation = torch.nn.GELU()
21 self.dropout2 = torch.nn.Dropout(cfg["dropout_head"])
22 self.classifier = torch.nn.Linear(cfg["head_hidden_size"], 1)
23
24 def forward(self, input_ids, attention_mask):
25 x = self.encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state[:, 0]
26 x = self.dropout1(x)
27 x = self.activation(self.dense(x))
28 return self.classifier(self.dropout2(x)).squeeze(-1)
29
30model = AvitoRelevanceClassifier(config)
31model.load_state_dict(load_file(weights_path, device="cpu"))
32model.eval()
33
34text = "Текст для проверки"
35inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=config["max_length"])
36with torch.inference_mode():
37 probability = torch.sigmoid(model(**inputs)).item()
38relevance = int(probability >= config["decision_threshold_relevant"])
39print({"relevance": relevance, "probability_relevant": probability})