Spaces:
Sleeping
Sleeping
File size: 1,050 Bytes
f5119fe 18a7c0b f5119fe 18a7c0b f5119fe 18a7c0b f5119fe 18a7c0b f5119fe 18a7c0b f5119fe 18a7c0b f5119fe 18a7c0b 449e8a4 f5119fe 18a7c0b f5119fe | 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 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()
|