Blame view

offline_tasks/scripts/a.py 1.36 KB
d35d18eb   tangwang   fix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  from modelscope import AutoProcessor, Gemma3nForConditionalGeneration
  from PIL import Image
  import requests
  import torch
  model_id = "google/gemma-3n-e4b-it"
  model = Gemma3nForConditionalGeneration.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16,).eval()
  processor = AutoProcessor.from_pretrained(model_id)
  messages = [
      {
          "role": "system",
          "content": [{"type": "text", "text": "You are a helpful assistant."}]
      },
      {
          "role": "user",
          "content": [
              {"type": "image", "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
              {"type": "text", "text": "Describe this image in detail."}
          ]
      }
  ]
  inputs = processor.apply_chat_template(
      messages,
      add_generation_prompt=True,
      tokenize=True,
      return_dict=True,
      return_tensors="pt",
  ).to(model.device)
  input_len = inputs["input_ids"].shape[-1]
  with torch.inference_mode():
      generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
      generation = generation[0][input_len:]
  decoded = processor.decode(generation, skip_special_tokens=True)
  print(decoded)
  # **Overall Impression:** The image is a close-up shot of a vibrant garden scene,
  # focusing on a cluster of pink cosmos flowers and a busy bumblebee.
  # It has a slightly soft, natural feel, likely captured in daylight.