import torch from datasets import load_dataset import gradio as gr from transformers import AutoFeatureExtractor, AutoModelForImageClassification # This should be the same as the first line of Python code in this Colab notebook dataset = load_dataset('beans') feature_extractor = AutoFeatureExtractor.from_pretrained("saved_model_files") model = AutoModelForImageClassification.from_pretrained("saved_model_files") labels = dataset['train'].features['labels'].names def classify(im): features = feature_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( title="Leaf Spot Classifier", description="Classify the leaf into one of: angular_leaf_spot, bean_rust, healthy", examples=["examples/healthy_test.15.jpg", "examples/angular_leaf_spot_test.0.jpg", "examples/bean_rust_test.32.jpg"], cache_examples=False, fn=classify, inputs=gr.Image(shape=(224, 224)), outputs=gr.Label(num_top_classes=3), ) interface.launch(debug=True)