| from transformers import pipeline | |
| from PIL import Image | |
| import os | |
| import gradio as gr | |
| from timeit import default_timer as timer | |
| from typing import Tuple, Dict | |
| def predict(img) -> Tuple[Dict, float]: | |
| start_time = timer() | |
| classifier = pipeline("image-classification", model="bazyl/gtsrb-model") | |
| result = classifier(img, top_k=5) | |
| response = {result[i]["label"]: result[i]["score"] for i in range(len(result))} | |
| pred_time = round(timer() - start_time, 3) | |
| return response, pred_time | |
| title = "GTSRB - German Traffic Sign Recognition by Bazyl Horsey" | |
| description = "CNN created for the GTSRB Dataset, achieved 99.93% test accuracy" | |
| # Create examples list from "examples/" directory | |
| example_list = [["examples/" + example] for example in os.listdir("examples")] | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=predict, | |
| 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, | |
| ) | |
| # Launch the app! | |
| demo.launch() |