Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/examples/idefics/inference.py
Views: 2542
# this is a demo of inference of IDEFICS-9B which needs about 20GB of GPU memory12import torch3from transformers import IdeficsForVisionText2Text, AutoProcessor45device = "cuda" if torch.cuda.is_available() else "cpu"67checkpoint = "HuggingFaceM4/idefics-9b"8#checkpoint = "HuggingFaceM4/tiny-random-idefics"910model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device)11processor = AutoProcessor.from_pretrained(checkpoint)1213url = "https://hips.hearstapps.com/hmg-prod/images/cute-photos-of-cats-in-grass-1593184777.jpg"14image = processor.image_processor.fetch_images(url)1516prompts = [17[18"User:",19image,20"Describe this image.\nAssistant: An image of two kittens in grass.\n",21"User:",22"https://hips.hearstapps.com/hmg-prod/images/dog-puns-1581708208.jpg",23"Describe this image.\nAssistant:",24],25[26"User:",27"https://hips.hearstapps.com/hmg-prod/images/dog-puns-1581708208.jpg",28"Describe this image.\nAssistant: An image of a dog wearing funny glasses.\n",29"User:",30"https://hips.hearstapps.com/hmg-prod/images/cute-photos-of-cats-in-grass-1593184777.jpg",31"Describe this image.\nAssistant:",32],33[34"User:",35image,36"Describe this image.\nAssistant: An image of two kittens in grass.\n",37"User:",38"https://huggingface.co/datasets/hf-internal-testing/fixtures_nlvr2/raw/main/image1.jpeg",39"Describe this image.\nAssistant:",40],41[42"User:",43"https://huggingface.co/datasets/hf-internal-testing/fixtures_nlvr2/raw/main/image2.jpeg",44"Describe this image.\nAssistant: An image of a dog.\n",45"User:",46"https://huggingface.co/datasets/hf-internal-testing/fixtures_nlvr2/raw/main/image1.jpeg",47"Describe this image.\nAssistant:",48],49]5051# batched mode52inputs = processor(prompts, return_tensors="pt").to(device)53# single sample mode54#inputs = processor(prompts[0], return_tensors="pt").to(device)5556generated_ids = model.generate(**inputs, max_length=128)57generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)58for i,t in enumerate(generated_text):59print(f"{i}:\n{t}\n")606162