File size: 1,233 Bytes
bcac5b4
 
 
 
3bb06e0
32b54e8
bcac5b4
32b54e8
bcac5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoTokenizer, AutoModelForVision2Seq
from PIL import Image
import gradio as gr

# Load the model and tokenizer
model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForVision2Seq.from_pretrained(model_name)

# Function to classify the image and return the description
def classify_image(image):
    # Ensure the image is in RGB mode
    image = image.convert("RGB")

    # Tokenize the image
    inputs = tokenizer(image, return_tensors="pt", padding="max_length", truncation=True)

    # Generate model output
    with torch.no_grad():
        outputs = model.generate(**inputs)
    
    # Decode and return the result (description)
    description = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return description

# Gradio Interface
interface = gr.Interface(
    fn=classify_image, 
    inputs=gr.Image(type="pil", label="Upload an Image"),  # Upload image dynamically
    outputs=gr.Textbox(label="Description"),
    title="Image Classification with Llama-3.2-11B-Vision-Instruct",
    description="Upload an image and the model will describe what's in it."
)

# Launch the interface
interface.launch()