caption / example.py
Rauki's picture
Upload 28 files
c018b6f verified
Raw
History Blame Contribute Delete
2.55 kB
"""
Simple example script to test Moondream2 model.
Based on the README usage example.
"""
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image
import torch
import os
def main():
print("Loading Moondream2 model...")
print(f"CUDA available: {torch.cuda.is_available()}")
# Load model from HuggingFace
# Note: The model uses trust_remote_code=True to load custom model classes
model_id = "vikhyatk/moondream2"
print(f"Loading model from HuggingFace: {model_id}...")
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
model = AutoModelForCausalLM.from_pretrained(
model_id,
trust_remote_code=True,
device_map=device if device == "cuda" else None
)
if device == "cpu":
model = model.to(device)
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
print("Model loaded successfully!")
# Check if we have a test image
# You can provide an image path as command line argument
import sys
if len(sys.argv) > 1:
image_path = sys.argv[1]
image = Image.open(image_path).convert("RGB")
print(f"Loaded image from: {image_path}")
else:
print("\nNo image provided. Creating a simple test image...")
# Create a simple test image
from PIL import ImageDraw
img = Image.new('RGB', (400, 300), color='white')
draw = ImageDraw.Draw(img)
draw.rectangle([50, 50, 350, 250], fill='lightblue', outline='black', width=3)
draw.text((150, 140), "Test Image", fill='black')
image = img
print("Created test image with a blue rectangle and text.")
print("\n" + "="*50)
print("Testing Captioning (Short)")
print("="*50)
try:
result = model.caption(image, length="short")
print(f"Short caption: {result['caption']}")
except Exception as e:
print(f"Error during captioning: {e}")
print("\n" + "="*50)
print("Testing Visual Query")
print("="*50)
try:
question = "What is in this image?"
result = model.query(image, question)
print(f"Question: {question}")
print(f"Answer: {result['answer']}")
except Exception as e:
print(f"Error during query: {e}")
print("\n" + "="*50)
print("Testing complete!")
print("="*50)
if __name__ == "__main__":
main()