bean / app.py
jafdxc's picture
Upload app.py
5d96762
raw
history blame
909 Bytes
import datasets
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)