Views
No views yet




.compile() is critical for fast decoding. Our compile implementation also handles warmup, so you can start making requests directly once it returns.1import torch
2from transformers import AutoModelForCausalLM
3
4moondream = AutoModelForCausalLM.from_pretrained(
5 "moondream/moondream3-preview",
6 trust_remote_code=True,
7 dtype=torch.bfloat16,
8 device_map={"": "cuda"},
9)
10moondream.compile()query skill can be used to ask open-ended questions about images.1from PIL import Image
2
3# Simple VQA
4image = Image.open("photo.jpg")
5result = moondream.query(image=image, question="What's in this image?")
6print(result["answer"])query runs in reasoning mode, allowing the model to "think" about the question before generating an answer. This is helpful for more complicated tasks, but sometimes the task you're running is simple and doesn't benefit from reasoning. To save on inference cost when this is the case, you can disable reasoning:1# Without reasoning for simple questions
2result = moondream.query(
3 image=image,
4 question="What color is the sky?",
5 reasoning=False
6)
7print(result["answer"])stream=True. You can control the temperature, top-p, and maximum number of tokens generated by passing in optional settings.1# Streaming with custom settings
2settings = {
3 "temperature": 0.7,
4 "top_p": 0.95,
5 "max_tokens": 512
6}
7
8result = moondream.query(
9 image=image,
10 question="Describe what's happening in detail",
11 stream=True,
12 settings=settings
13)
14
15# Stream the answer
16for chunk in result["answer"]:
17 print(chunk, end="", flush=True)1# Text-only example (no image)
2result = moondream.query(
3 question="Explain the concept of machine learning in simple terms"
4)
5print(result["answer"])caption skill has you covered.1# Different caption lengths
2image = Image.open("landscape.jpg")
3
4# Short caption
5short = moondream.caption(image, length="short")
6print(f"Short: {short['caption']}")
7
8# Normal caption (default)
9normal = moondream.caption(image, length="normal")
10print(f"Normal: {normal['caption']}")
11
12# Long caption
13long = moondream.caption(image, length="long")
14print(f"Long: {long['caption']}")query skill.1# Streaming caption with custom settings
2result = moondream.caption(
3 image,
4 length="long",
5 stream=True,
6 settings={"temperature": 0.3}
7)
8
9for chunk in result["caption"]:
10 print(chunk, end="", flush=True)point skill identifies specific points (x, y coordinates) for objects in an image.1# Find points for specific objects
2image = Image.open("crowd.jpg")
3result = moondream.point(image, "person wearing a red shirt")
4
5# Points are normalized coordinates (0-1)
6for i, point in enumerate(result["points"]):
7 print(f"Point {i+1}: x={point['x']:.3f}, y={point['y']:.3f}")detect skill provides bounding boxes for objects in an image.1# Detect objects with bounding boxes
2image = Image.open("street_scene.jpg")
3result = moondream.detect(image, "car")
4
5# Bounding boxes are normalized coordinates (0-1)
6for i, obj in enumerate(result["objects"]):
7 print(f"Object {i+1}: "
8 f"x_min={obj['x_min']:.3f}, y_min={obj['y_min']:.3f}, "
9 f"x_max={obj['x_max']:.3f}, y_max={obj['y_max']:.3f}")
10
11# Control maximum number of objects
12settings = {"max_objects": 10}
13result = moondream.detect(image, "person", settings=settings)1# Encode image once
2image = Image.open("complex_scene.jpg")
3encoded = moondream.encode_image(image)
4
5# Reuse the encoding for multiple queries
6questions = [
7 "How many people are in this image?",
8 "What time of day was this taken?",
9 "What's the weather like?"
10]
11
12for q in questions:
13 result = moondream.query(image=encoded, question=q, reasoning=False)
14 print(f"Q: {q}")
15 print(f"A: {result['answer']}\n")
16
17# Also works with other skills
18caption = moondream.caption(encoded, length="normal")
19objects = moondream.detect(encoded, "vehicle")