deman539's picture
Update app.py
62cccf5
raw
history blame
923 Bytes
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(input_image):
features = extractor(input_image, 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', title='Leaf classification on beans dataset',
description='Sample fine-tuning a ViT for bean dataset classification')
interface.launch()