closirr's picture
1
18a7c0b
Raw History Blame Contribute Delete
1.05 kB
import gradio as gr
from transformers import AutoFeatureExtractor, ResNetForImageClassification
from PIL import Image
# Load the model and feature extractor
model_name = "microsoft/resnet-50"
model = ResNetForImageClassification.from_pretrained(model_name)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
# Prediction function
def predict(image):
# Preprocess the image
inputs = feature_extractor(images=image, return_tensors="pt")
# Perform inference
outputs = model(**inputs)
# Get the predicted class index
predicted_class_idx = outputs.logits.argmax(-1).item()
# Map the index to the corresponding label
return model.config.id2label[predicted_class_idx]
# Gradio interface
image_input = gr.Image()
label_output = gr.Textbox(label="Predicted Class")
interface = gr.Interface(
fn=predict,
inputs=image_input,
outputs=label_output,
title="Image Classifier",
description="Upload an image to receive a class prediction."
)
if __name__ == "__main__":
interface.launch()