Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3import torch.nn.functional as F
4
5# Загрузка модели и токенизатора
6model_name = "galkinv42/RuModernBERT-Intent-Classifier"
7tokenizer = AutoTokenizer.from_pretrained("deepvk/RuModernBERT-base")
8model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
10def predict_intent(user_input):
11 # Токенизация без добавления контекста
12 inputs = tokenizer(
13 user_input,
14 return_tensors="pt",
15 truncation=True,
16 max_length=512
17 )
18
19 model.eval()
20 with torch.no_grad():
21 outputs = model(**inputs)
22
23 # Расчет вероятностей
24 probabilities = F.softmax(outputs.logits, dim=-1)
25 confidence, class_idx = torch.max(probabilities, dim=-1)
26
27 label = model.config.id2label[class_idx.item()]
28 return label, confidence.item()
29
30# Пример вызова
31intent, score = predict_intent("Спасибо за помощь, всего доброго")
32print(f"Интент: {intent}, Уверенность: {score:.4f}")
33| Класс | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
<ACTION> | 0.85 | 0.66 | 0.74 | 87 |
<FAREWELL> | 0.91 | 0.83 | 0.87 | 24 |
<GREETING> | 0.92 | 0.96 | 0.94 | 24 |
<NOISE> | 0.68 | 0.59 | 0.63 | 93 |
<QUESTION> | 0.85 | 0.91 | 0.88 | 188 |
<STATEMENT> | 0.62 | 0.74 | 0.67 | 97 |
<THANKS> | 1.00 | 0.67 | 0.80 | 3 |