import numpy as np import torch import datasets from transformers import AutoFeatureExtractor, AutoModelForImageClassification dataset = datasets.load_dataset('beans') 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 import gradio as gr interface = gr.Interface(fn=classify, inputs="image", outputs="label", examples=[ ["https://images.unsplash.com/photo-1550147760-44c9966d6bc7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8bGVhZnxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800&q=60"], ["https://images.unsplash.com/photo-1525498128493-380d1990a112?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTd8fGxlYWZ8ZW58MHx8MHx8&auto=format&fit=crop&w=800&q=60"], ["https://apps.lucidcentral.org/pppw_v10/images/entities/bean_angular_leaf_spot_216/angularspot1.jpg"], ["https://extension.umn.edu/sites/extension.umn.edu/files/beans-viral-diseases-2.jpg"], ["http://1.bp.blogspot.com/-CcMICF_A1CI/UHKSvTV2k2I/AAAAAAAAHI0/TlFMGU8RpYQ/s1600/DSCF9698.JPG"], ["https://www.garden.eco/wp-content/uploads/2017/12/bean-leaves.jpg"], ["https://apps.lucidcentral.org/pppw_v10/images/entities/bean_angular_leaf_spot_216/angularspot1.jpg"] ], title="🍃 Bean Leaf Image Classification", description="Based on a leaf image, the goal is to predict the disease type (Angular Leaf Spot and Bean Rust), if any.",) interface.launch(debug=True)