leaf-classifier / app.py
schrilax's picture
add implementation/interface
267841d
import datasets
import gradio as gr
import torch
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
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=gr.Image(shape=(200, 200)), outputs=gr.outputs.Label(num_top_classes=3),
examples=['leaf1.png', 'leaf2.png', 'leaf3.jpg', 'leaf4.jpg'], title='Leaf Classification App', description='Check if the leaves of your plant are healthy!', flagging_dir='flagged_examples/')
interface.launch(debug=True)