import datasets from transformers import AutoFeatureExtractor, AutoModelForImageClassification import numpy as np import gradio as gr import torch dataset = datasets.load_dataset("beans") # This should be the same as the first line of Python code in this Colab notebook extractor = AutoFeatureExtractor.from_pretrained("andresgtn/vit-base-bean-health-classifier") model = AutoModelForImageClassification.from_pretrained("andresgtn/vit-base-bean-health-classifier") # add to cuda? #model.eval() #model.to(device) labels = dataset['train'].features['labels'].names def classify(im): features = extractor(im, return_tensors='pt') #features.to(device) # move to gpu as model, if available with torch.no_grad(): logits = model(**features).logits probability = torch.nn.functional.softmax(logits, dim=-1) #probs = probability[0].to('cpu').detach().numpy() probs = probability[0].detach().numpy() confidences = {label: float(probs[i]) for i, label in enumerate(labels)} return confidences interface = gr.Interface(classify, gr.Image(shape=(200, 200)), 'text') #demo.launch() interface.launch(debug=False)