cupkake14's picture
create app.py
d12f7bd
raw
history blame
1.04 kB
import gradio as gr
import datasets
import transformers
import torch
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(im):
features = feature_extractor(im, return_tensors='pt')
with torch.no_grad():
logits = model(**features).logits
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
gr.Interface(fn = classify,
inputs = "image",
outputs = "label",
examples='/examples',
title='Leaf classification on beans dataset',
description='Fine-tuning a ViT for bean plant health classification'
).launch(debug=True)