File size: 2,548 Bytes
c018b6f | 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | """
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()
|