File size: 1,133 Bytes
15640ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import torch

# Load the Microsoft Phi-3.5-mini-instruct model
model_name = "microsoft/phi-3.5-mini-instruct"
processor = BlipProcessor.from_pretrained(model_name)
model = BlipForConditionalGeneration.from_pretrained(model_name)

# Define the image classification function
def classify_image(image):
    # Preprocess the image
    inputs = processor(images=image, return_tensors="pt")
    pixel_values = inputs["pixel_values"]

    # Generate the classification
    with torch.no_grad():
        generated_ids = model.generate(pixel_values=pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    
    return generated_text

# Create a Gradio interface
iface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Textbox(label="Image Classification"),
    title="Image Context Classification",
    description="Upload an image to classify its context using Microsoft's Phi-3.5-mini-instruct model."
)

# Launch the interface
iface.launch()