import gradio as gr from PIL import Image import spaces from typing import Dict import torch from transformers import ViTImageProcessor, AutoFeatureExtractor, AutoModelForImageClassification image_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") extractor = AutoFeatureExtractor.from_pretrained("saved_model_files") model = AutoModelForImageClassification.from_pretrained("saved_model_files") labels = ['angular_leaf_spot', 'bean_rust', 'healthy'] @spaces.GPU(duration=240) def classify(image: Image.Image) -> Dict[str, float]: """ Classify an image of a bean plant leaf into one of several health categories. Args: image (Image.Image): The input image of the bean leaf to be classified. Returns: Dict[str, float]: A dictionary where the keys are the health labels (e.g., 'angular_leaf_spot', 'bean_rust', 'healthy') and the values are the confidence scores for each label. """ features = image_processor(image, return_tensors='pt') logits = model(features["pixel_values"])[-1] probability = torch.nn.functional.softmax(logits, dim=-1) probs = probability[0].detach().numpy() confidences = {label: float(probs[i]) for i, label in enumerate(labels)} return confidences ####### GRADIO APP ####### title = """