import gradio as gr import os import torch from model import create_vit from timeit import default_timer as timer from typing import Tuple, Dict # Setup class names with open("class_names.txt", "r") as f: class_names = [name.strip() for name in f.readlines()] ### Model and transforms preparation ### # Create model and transforms model, _, _, transforms = create_vit(output_shape=len(class_names), classes=class_names) # Load saved weights model.load_state_dict( torch.load(f="vit.pth", map_location=torch.device("cpu")) # load to CPU ) ### Predict function ### def predict(img) -> Tuple[Dict, float]: # Start a timer start_time = timer() # Transform the input image for use with the model img = transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index # Put model into eval mode, make prediction model.eval() with torch.inference_mode(): # Pass transformed image through the model and turn the prediction logits into probaiblities pred_probs = torch.softmax(model(img).logits, dim=1) # Create a prediction label and prediction probability dictionary pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))} # Calculate pred time end_time = timer() pred_time = round(end_time - start_time, 4) # Return pred dict and pred time return pred_labels_and_probs, pred_time ### 4. Gradio app ### # Create title, description and article title = "A ViT cifar10 Classifier" description = "An [ViT feature extractor](https://huggingface.co./google/vit-base-patch16-224) computer vision model to classify images on the [10 classes of the cifar10 dataset](https://huggingface.co./datasets/cifar10). [Source Code Found Here](https://colab.research.google.com/drive/1j4NbiMpCqmXN1xw9e2_r77gMdr3WpMnO?usp=drive_link)" article = "Built with [Gradio](https://github.com/gradio-app/gradio) and [PyTorch](https://pytorch.org/). [Source Code Found Here](https://colab.research.google.com/drive/1j4NbiMpCqmXN1xw9e2_r77gMdr3WpMnO?usp=drive_link)" # Create example list example_list = [["examples/" + example] for example in os.listdir("examples")] # Create the Gradio demo demo = gr.Interface(fn=predict, # maps inputs to outputs inputs=gr.Image(type="pil"), outputs=[gr.Label(num_top_classes=5, label="Predictions"), gr.Number(label="Prediction time (s)")], examples=example_list, title=title, description=description, article=article) # Launch the demo demo.launch()