Views
No views yet
1pip install unsloth transformers accelerate bitsandbytes torch pillow
21from unsloth import FastVisionModel
2from PIL import Image
3import torch
4from transformers import BitsAndBytesConfig
5
6device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
8bnb_config = BitsAndBytesConfig(
9 load_in_8bit=True,
10 llm_int8_enable_fp32_cpu_offload=True,
11)
12
13model, tokenizer = FastVisionModel.from_pretrained(
14 "ozertuu/Llama-3.2-11B-VL-Turkish-Captioner",
15 use_gradient_checkpointing="unsloth",
16 device_map="auto",
17 quantization_config=bnb_config,
18)
19
20FastVisionModel.for_inference(model)
21
22def predict_radiology_description(image, instruction):
23 try:
24 messages = [{"role": "user", "content": [
25 {"type": "image"},
26 {"type": "text", "text": instruction}
27 ]}]
28 input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
29
30 inputs = tokenizer(
31 image,
32 input_text,
33 add_special_tokens=False,
34 return_tensors="pt",
35 ).to(device)
36
37 output_ids = model.generate(
38 **inputs,
39 max_new_tokens=256,
40 temperature=1.5,
41 min_p=0.1
42 )
43
44 generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
45 return generated_text.replace("assistant", "\n\nassistant").strip()
46 except Exception as e:
47 return f"Error: {str(e)}"
48
49
50image_path = "1695678287384.jpg"
51instruction = "Bu resmi detaylı bir şekilde açıkla"
52
53image = Image.open(image_path).convert("RGB")
54output = predict_radiology_description(image, instruction)
55print(output)
1Görüntü, kısa koyu saçlı ve açık kahverengi gözlü, gözlük takan ve siyah bir ceket giyen bir adamın portresidir.
2Koyu bir arka plana karşı duran büyük bir siyah harf G ile loş bir şekilde aydınlatılmış bir ortamda duruyor.
3Adam, yüzünde kararlı bir ifade ile doğrudan kameraya bakıyor.
4İfadesi, belirli bir görevi veya projeyi yerine getiriyor gibi bir hedefe odaklanmış olduğunu gösteriyor.