Spaces:
Sleeping
Sleeping
| import torch | |
| from transformers import AutoImageProcessor, AutoModelForImageClassification | |
| from PIL import Image | |
| import gradio as gr | |
| import io | |
| # Load model and feature extractor | |
| def load_model(): | |
| processor = AutoImageProcessor.from_pretrained("therealcyberlord/stanford-car-vit-patch16") | |
| model = AutoModelForImageClassification.from_pretrained("therealcyberlord/stanford-car-vit-patch16") | |
| return processor, model | |
| processor, model = load_model() | |
| # Function to classify image | |
| def classify_image(image): | |
| # Convert image if necessary | |
| if not isinstance(image, Image.Image): | |
| image = Image.open(io.BytesIO(image)).convert("RGB") | |
| inputs = processor(images=image, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| predicted_class_idx = logits.argmax(-1).item() | |
| labels = model.config.id2label | |
| predicted_class = labels[predicted_class_idx] | |
| return predicted_class | |
| # Define Gradio Interface | |
| app = gr.Interface( | |
| fn=classify_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Car Classification", | |
| description="Upload a car image to classify its model." | |
| ) | |
| # Launch the app | |
| app.launch(share=True) | |