salaz055's picture
typo
5a896ac
raw
history blame
1.23 kB
import datasets
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
import gradio as gr
import torch
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
interface = gr.Interface(
fn = classify,
inputs= "image",
outputs= "label",
title = 'Leaf Classication',
description = "Many farmers are turning machine learning to monitor their crops automatically with great accuracy. This application can be used to detect Angular Leaf Spot and Bean Rust!",
examples = [["bean_rust.jpeg"] , ["healthy.jpeg"] , ["angular_leaf_spot.jpeg"]]
)
interface.launch()