#!pip install -U transformers accelerate bitsandbytes peftimport torchfrom transformers import AutoTokenizer, BitsAndBytesConfigfrom peft import AutoPeftModelForCausalLM model_id = "kingabzpro/gemma4-emotion-lora" bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True,) tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True) if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token model = AutoPeftModelForCausalLM.from_pretrained( model_id, quantization_config=bnb_config, device_map="cuda:0", dtype=torch.bfloat16,) messages = [ { "role": "system", "content": ( "You are an emotion classification assistant. " "Read the user's text and answer with exactly one label. " "Only choose from: sadness, joy, love, anger, fear, surprise. " "Return only the label and nothing else." ), }, { "role": "user", "content": "Classify the emotion of this text:\n\nThis is the best day of my life!", },] inputs = tokenizer.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt",).to(model.device) input_len = inputs["input_ids"].shape[-1] with torch.no_grad(): outputs = model.generate(**inputs, max_new_tokens=4) print(tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip())#joy