Spaces:
Sleeping
Sleeping
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() | |