### 1. Imports and class names setup ### import gradio as gr import os import torch from model import create_model from timeit import default_timer as timer from typing import Tuple, Dict class_names = ['appaloosa_leopard', 'dutch_warmblood', 'thoroughbred_chestnut'] model_ft, model_transforms = create_model(num_classes=len(class_names)) model_ft.load_state_dict( torch.load("horses_swin_t.pt", map_location=torch.device("cpu")) ) def predict(img) -> Tuple[Dict, float]: start_time = timer() img = model_transforms(img).unsqueeze(0) model_ft.eval() with torch.inference_mode(): pred_probs = torch.softmax(model_ft(img), dim=1) pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))} pred_time = round(timer() - start_time, 5) return pred_labels_and_probs, pred_time title = "HorseVision Mini 🐎" description = "A feature extractor computer vision model to classify images of horses." #article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)." example_list = [["examples/" + example] for example in os.listdir("examples")] demo = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs=[gr.Label(num_top_classes=3, label="Predictions"), gr.Number(label="Prediction time (s)")], examples=example_list, title=title, description=description, #article=article ) demo.launch()