File size: 1,107 Bytes
d9d5c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import cnn
from torchvision import transforms
from PIL import Image

transform = transforms.Compose([
    transforms.Resize((128, 128)),
    transforms.ToTensor()
])

model = cnn.CNN(2)
model = model.to("cpu")
model.load_state_dict(torch.load("cnn_model.pth", weights_only=True, map_location="cpu"))
model.eval()

label = ["Kucing", "Anjing"]

def inference(image):
    image = transform(image).unsqueeze(0)

    with torch.no_grad():
        output = model(image)
        output = torch.nn.functional.softmax(output, dim=1)

        predicted_class = torch.argmax(output, dim=1).item()
        score = output[0][predicted_class]

    return f'Ini adalah {label[predicted_class]} dengan kecocokan sebesar {score * 100}'

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            inputs = gr.Image(type="pil")
        with gr.Column():
            btn = gr.Button("Cek")
            pred = gr.Text(label="Prediction")

    btn.click(fn=inference, inputs=inputs, outputs=pred)

        

demo.queue().launch()