andresgtn's picture
create app.py
0eae57d
raw history blame
No virus
1.01 kB
import datasets
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
import numpy as np
import gradio as gr
dataset = load_dataset("beans") # This should be the same as the first line of Python code in this Colab notebook
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
# 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()
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)