Views
No views yet
1from transformers import AutoProcessor, Gemma3ForConditionalGeneration
2from peft import PeftModel
3from PIL import Image
4import torch
5
6# Load base model
7base_model = Gemma3ForConditionalGeneration.from_pretrained(
8 "google/gemma-3-4b-it",
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12
13# Load LoRA adapter
14model = PeftModel.from_pretrained(base_model, "Nefflymicn/gemma3-4b-bean-captioning")
15processor = AutoProcessor.from_pretrained("Nefflymicn/gemma3-4b-bean-captioning")
16
17# Prepare input
18image = Image.open("bean_plant.jpg")
19messages = [
20 {
21 "role": "user",
22 "content": [
23 {"type": "image"},
24 {"type": "text", "text": "Describe this plant image."}
25 ]
26 }
27]
28
29text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
30inputs = processor(text=text, images=image, return_tensors="pt").to(model.device)
31
32# Generate
33outputs = model.generate(**inputs, max_new_tokens=50, do_sample=False)
34response = processor.decode(outputs[0], skip_special_tokens=True)
35print(response)1@misc{gemma3-bean-captioning,
2 author = {younaice},
3 title = {Gemma-3-4B Fine-tuned for Bean Disease Classification},
4 year = {2024},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/Nefflymicn/gemma3-4b-bean-captioning}}
7}