leaf_classifier / app.py
shatrunjai's picture
Update app.py
2e922d4
raw
history blame
1.2 kB
import datasets
from datasets import load_dataset
import gradio as gr
import torch
from transformers import Trainer, TrainingArguments
from transformers import AutoModelForImageClassification
import numpy as np
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
dataset = load_dataset('beans') # This should be the same as the first line of Python code in this Colab notebook
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
labels = dataset['train'].features['labels'].names
def classify(im):
features = image_processor(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
# Run the Gradio interface for the app
interface = gr.Interface(
fn=classify,
inputs=["image"],
outputs=["label"],
title="Leaf disaease classifier",
description="A pre-trained vit model for classifying leaf diseases"
)
interface.launch(debug=True)