import torch import datasets from transformers import AutoFeatureExtractor, AutoModelForImageClassification import gradio as gr dataset = datasets.load_dataset('beans', 'full_size') extractor = AutoFeatureExtractor.from_pretrained("saved_model_files") model = AutoModelForImageClassification.from_pretrained("saved_model_files") labels = dataset['train'].features['labels'].names def classify(im): features = extractor(im, 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 interface = gr.Interface( fn=classify, inputs="image", outputs="label", examples=[ 'https://datasets-server.huggingface.co/assets/beans/--/default/train/5/image/image.jpg', 'https://datasets-server.huggingface.co/assets/beans/--/default/train/10/image/image.jpg', 'https://datasets-server.huggingface.co/assets/beans/--/default/train/15/image/image.jpg' ], title="bean leaf classification", description="Input an image of a bean leaf to predict whether it is healthy or diseased.", ) interface.launch()