abidlabs/pytorch-image-classifier
2
1import torch2 3model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()4 5import requests6from torchvision import transforms7 8# Download human-readable labels for ImageNet.9response = requests.get("https://git.io/JJkYN")10labels = response.text.split("\n")11 12def predict(inp):13 inp = transforms.ToTensor()(inp).unsqueeze(0)14 with torch.no_grad():15 prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)16 confidences = {labels[i]: float(prediction[i]) for i in range(1000)} 17 return confidences18 19import gradio as gr20 21gr.Interface(fn=predict, 22 inputs=gr.inputs.Image(type="pil"),23 outputs=gr.outputs.Label(num_top_classes=3),24 examples=["lion.jpg", "cheetah.jpg"],25 theme="default",26 css=".footer{display:none !important}").launch()27 