File size: 1,350 Bytes
663913f
 
 
 
 
abc6dd9
663913f
 
 
 
 
abc6dd9
663913f
 
 
 
 
 
07b69f6
663913f
 
 
 
 
 
 
 
 
abc6dd9
 
720168f
abc6dd9
5bb415f
9b5f0ab
e31cc72
 
9b5f0ab
 
 
e8da7ff
 
 
720168f
9b5f0ab
abc6dd9
 
 
663913f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
import torch
import torchvision.transforms.functional as TF
from model import NeuralNetwork
import json
import os

device = "cuda" if torch.cuda.is_available() else "cpu"

def pokemon_classifier(inp):
    model = NeuralNetwork()
    model.load_state_dict(torch.load("model_best.pt", map_location=torch.device(device)))
    model.eval()

    with open('labels.json') as f:
        labels = json.load(f)

    x = TF.to_tensor(inp)
    x = TF.resize(x, (64, 64), antialias=True)
    x = x.to(device)
    x = x.unsqueeze(0)
    
    with torch.no_grad():
        y_pred = model(x)
    pokemon = torch.argmax(y_pred, dim=1).item()
    
    return labels[str(pokemon)]

with gr.Blocks() as demo:
    gr.Markdown("# Gen 1 Pokemon classifier")
    with gr.Column(scale=4):
        inp = gr.Image(type="pil")
        out = gr.Textbox(label='Pokemon')
    gr.Examples(
        examples=[
            os.path.join(os.path.dirname(__file__), "images/Aerodactyl.jpg"),
            os.path.join(os.path.dirname(__file__), "images/Bulbasaur.jpg"),
            os.path.join(os.path.dirname(__file__), "images/Charizard.jpg")
        ],
        inputs=inp,
        outputs=out,
        fn=pokemon_classifier,
        cache_examples=False
    )
    btn = gr.Button("Run")
    btn.click(fn=pokemon_classifier, inputs=inp, outputs=out)

demo.launch()