Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import torch | |
| from model import load_model | |
| from timeit import default_timer as timer | |
| from typing import Tuple, Dict | |
| # class names | |
| class_names = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck'] | |
| model, transform = load_model() | |
| # predict function | |
| def predict(img): | |
| start_time = timer() | |
| img = transform(img).unsqueeze(0) | |
| model.eval() | |
| with torch.inference_mode(): | |
| pred_probs = torch.softmax(model(img), dim=1) | |
| pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))} | |
| end_time = timer() | |
| pred_time = round(end_time - start_time, 4) | |
| return pred_labels_and_probs, pred_time | |
| title = "Noel's Cifar10 - Efficinet Computer Vision Model (PyTorch)" | |
| description = "An EfficientNetB0 feature extractor computer vision model to classify Cifar10 dataset" | |
| article = "Created in SageMaker Studio" | |
| example_list = [["examples/" + example] for example in os.listdir("examples")] | |
| # Gradio app | |
| demo = gr.Interface(fn=predict, | |
| inputs=gr.Image(type="pil"), | |
| outputs=[gr.Label(num_top_classes=10, label="Predictions"), | |
| gr.Number(label="Prediction time (s)")], | |
| examples=example_list, | |
| title=title, | |
| description=description, | |
| article=article) | |
| demo.launch() | |