Views
No views yet
0: Positive1: Neutral2: Negative1LoraConfig(
2 task_type=TaskType.SEQ_CLS,
3 r=16,
4 lora_alpha=32,
5 target_modules=["query", "value"],
6 lora_dropout=0.01,
7 bias="none"
8)1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2from peft import PeftModel, PeftConfig
3import torch
4
5# Load configuration
6config = PeftConfig.from_pretrained("YOUR_USERNAME/{REPO_NAME}")
7
8# Load base model
9tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
10base_model = AutoModelForSequenceClassification.from_pretrained(
11 config.base_model_name_or_path,
12 num_labels=3,
13 id2label={{0: "positive", 1: "neutral", 2: "negative"}},
14 label2id={{"positive": 0, "neutral": 1, "negative": 2}}
15)
16
17# Load LoRA adapters
18model = PeftModel.from_pretrained(base_model, "YOUR_USERNAME/{REPO_NAME}")
19model.eval()
20
21# Predict
22def predict(text):
23 inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True, padding=True)
24 with torch.no_grad():
25 outputs = model(**inputs)
26 logits = outputs.logits
27 predicted_class = logits.argmax(dim=-1).item()
28 probabilities = torch.softmax(logits, dim=-1)[0]
29
30 return {{
31 "label": model.config.id2label[predicted_class],
32 "confidence": probabilities[predicted_class].item(),
33 "probabilities": {{
34 "positive": probabilities[0].item(),
35 "neutral": probabilities[1].item(),
36 "negative": probabilities[2].item()
37 }}
38 }}
39
40# Example
41result = predict("Świetny produkt, polecam!")
42print(result)
43# Output: {{'label': 'positive', 'confidence': 0.95, ...}}1@misc{{herbert-sentiment-lora,
2 author = Rafał Adamczyk,
3 title = {{HerBERT Polish Sentiment Analysis with LoRA}},
4 year = {{2025}},
5 publisher = {{HuggingFace}},
6 howpublished = {{\\url{{https://huggingface.co/rafal-adamczyk/herbert-polish-sentiment-lora}}}}
7}}