Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from PIL import Image
3
4model = AutoModelForCausalLM.from_pretrained(
5 "vikhyatk/moondream2",
6 revision="2025-06-21",
7 trust_remote_code=True,
8 device_map={"": "cuda"} # ...or 'mps', on Apple Silicon
9)
10
11# Captioning
12print("Short caption:")
13print(model.caption(image, length="short")["caption"])
14
15print("\nNormal caption:")
16for t in model.caption(image, length="normal", stream=True)["caption"]:
17 # Streaming generation example, supported for caption() and detect()
18 print(t, end="", flush=True)
19print(model.caption(image, length="normal"))
20
21# Visual Querying
22print("\nVisual query: 'How many people are in the image?'")
23print(model.query(image, "How many people are in the image?")["answer"])
24
25# Object Detection
26print("\nObject detection: 'face'")
27objects = model.detect(image, "face")["objects"]
28print(f"Found {len(objects)} face(s)")
29
30# Pointing
31print("\nPointing: 'person'")
32points = model.point(image, "person")["points"]
33print(f"Found {len(points)} person(s)")reasoning=True in the query skill to trade off speed vs. accuracy.compile() now supported in HF Transformers implementation