Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2from peft import PeftModel
3
4# Load the tokenizer
5tokenizer = AutoTokenizer.from_pretrained("Turalll/llama-1b-lora-instruct-classifier")
6
7# Load the base model (you must have access to LLaMA-1B)
8base_model = AutoModelForSequenceClassification.from_pretrained("path_to_llama-3.2-1B-Instruct_base_model", num_labels=10)
9
10# Load the LoRA adapter
11model = PeftModel.from_pretrained(base_model, "Turalll/llama-1b-lora-instruct-classifier")
12
13# Example inference
14text = "Your input text here"
15
16
17## Custom label_ids:labels map
18id2id = {
19 0: "Health and Wellbeing",
20 1: "Cinema",
21 2: "Environmental Science",
22 3: "Software Development",
23 4: "Fashion",
24 5: "Career Development",
25 6: "Culinary Guide",
26 7: "Cybersecurity",
27 8: "Economics",
28 9: "Music"
29}
30
31## Tokenize the input
32inputs = tokenizer(
33 text,
34 padding="max_length",
35 truncation=True,
36 max_length=128,
37 return_tensors="pt"
38)
39
40## Move inputs to the same device as the model
41inputs = {k: v.to(device) for k, v in inputs.items()}
42
43## Get predictions
44with torch.no_grad():
45 outputs = model(**inputs)
46 logits = outputs.logits
47 predicted_class_id = logits.argmax(dim=-1).item()
48
49## Map predicted class ID to label
50predicted_label = id2label[predicted_class_id]
51
52print(f"Predicted label: {predicted_label}")
53