beans3 / app.py
jafdxc's picture
Application
4a9c73f
import torch
from datasets import load_dataset
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
# This should be the same as the first line of Python code in this Colab notebook
dataset = 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')
inp = model(**features)
logits = torch.nn.functional.softmax(inp.logits, dim=-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=gr.Image(shape=(224, 224)), outputs="text")
interface.launch(debug=True)