Spaces:
Sleeping
Sleeping
Download app.py from closirr/image-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/closirr/image-classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/closirr/image-classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/closirr/image-classifier/resolve/main/app.py
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() | |