Spaces:
Runtime error
Runtime error
import requests | |
import torch | |
from PIL import Image | |
from transformers import pipeline | |
pipe = pipeline("text-generation", model="meta-llama/Llama-3.2-1B") | |
# Load model directly | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B") | |
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B") | |
# Load the processor | |
processor = AutoProcessor.from_pretrained(model_id) | |
# Define an image URL | |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg" | |
# Fetch the image using requests | |
image = Image.open(requests.get(url, stream=True).raw) | |
# Define the messages in a format the model understands (adjust as needed) | |
messages = [ | |
{"role": "user", "content": [ | |
{"type": "image"}, # This indicates that the input contains an image | |
{"type": "text", "text": "Can you please describe this image in one sentence?"} | |
]} | |
] | |
# Generate input text with the processor | |
input_text = processor.apply_chat_template(messages, add_generation_prompt=True) | |
# Process the image and input text, prepare them for the model | |
inputs = processor(image, input_text, return_tensors="pt").to(model.device) | |
# Run the model to generate a response | |
output = model.generate(**inputs, max_new_tokens=70) | |
# Decode and print the output | |
print(processor.decode(output[0][inputs["input_ids"].shape[-1]:])) | |