import torchfrom transformers import AutoTokenizer, AutoModelForCausalLMfrom peft import PeftModel # Repository ConfigurationsBASE_MODEL_ID = "Qwen/Qwen3-4B-Base"ADAPTER_PATH = "KlibiYoussef/smart-plate-nutrition-adapter" # 1. Load Tokenizer and Base Model with the LoRA Adaptertokenizer = AutoTokenizer.from_pretrained(ADAPTER_PATH)base_model = AutoModelForCausalLM.from_pretrained( BASE_MODEL_ID, torch_dtype=torch.float16, device_map="auto")model = PeftModel.from_pretrained(base_model, ADAPTER_PATH)model.eval() # 2. Construct the Prompt in ChatML format using the official platform promptprompt = ( "<|im_start|>system\n" "You are the 'Smart Plate Nutrition' AI assistant. Your role is to provide ultra-precise, " "scientific, and personalized nutritional information. You must always be polite, helpful, " "start with a warm greeting, and end with an encouraging health-focused closing statement." "<|im_end|>\n" "<|im_start|>user\n" "How can I lose fat without losing muscle?\n" "<|im_end|>\n" "<|im_start|>assistant\n") # 3. Generate Responseinputs = tokenizer(prompt, return_tensors="pt").to(model.device)with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=256, temperature=0.3, do_sample=True, repetition_penalty=1.1 ) # Decode the Outputgenerated_tokens = outputs[0][inputs.input_ids.shape[1]:]response = tokenizer.decode(generated_tokens, skip_special_tokens=True)print(response)