it run good in colab t4

#16
by asdgad - opened

import torch
from transformers import BitsAndBytesConfig, AutoProcessor, Glm4vForConditionalGeneration
from PIL import Image

MODEL_PATH = "zai-org/GLM-4.6V-Flash"

إعداد ضغط 4-بت

bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=torch.float16
)

processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)

model = Glm4vForConditionalGeneration.from_pretrained(
MODEL_PATH,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True
)

--- اقرأ الصورة من ملف محلي ---

image_path = "/content/Grayscale_8bits_palette_sample_image.png" # عدِّل هذا إلى مسار الصورة عندك
image = Image.open(image_path).convert("RGB")

إعداد رسالة: صورة + نص

messages = [
{
"role": "user",
"content": [
# لن تحتاج url لأن الصورة محليّة
{"type": "image"},
{"type": "text", "text": "Describe this image in detail."}
],
}
]

تجهيز المدخلات: الصور + النص

chat_template = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(
text=chat_template,
images=[image],
return_tensors="pt"
)

نقل التينسورات إلى نفس جهاز النموذج (GPU أو CPU)

inputs = {k: v.to(model.device) for k, v in inputs.items()}
inputs.pop("token_type_ids", None)

توليد النص (الوصف)

with torch.no_grad():
generated_ids = model.generate(**inputs, max_new_tokens=51)

output = processor.decode(
generated_ids[0][ inputs["input_ids"].shape[1]: ],
skip_special_tokens=True
)

print(output)

Using a slow image processor as use_fast is unset and a slow processor was saved with this model. use_fast=True will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with use_fast=False.
Unrecognized keys in rope_parameters for 'rope_type'='default': {'partial_rotary_factor', 'mrope_section'}

Download complete: 
 0.00/0.00 [00:00<?, ?B/s]
Fetching 4 files: 100%
 4/4 [00:00<00:00, 164.82it/s]
Loading weights: 100%
 704/704 [02:02<00:00,  5.74it/s, Materializing param=model.visual.post_layernorm.weight]

Got it, let's describe this black-and-white image of a parrot. First, the main subject is a parrot, likely a macaw or similar species, with a prominent crest on its head. The parrot is perched on a

from IPython.display import Image as IPImage

تأكد أن مسار الصورة صحيح

image_path = "/content/Grayscale_8bits_palette_sample_image.png"

عرض الصورة

IPImage(filename=image_path)

Grayscale_8bits_palette_sample_image

!pip install transformers==5.0.0rc0

No description provided.

Screenshot from 2025-12-11 01-18-21

Sign up or log in to comment