File size: 1,622 Bytes
5892ebe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
import onnxruntime
from torchvision import transforms
import torch
import torch.nn.functional as F
import gradio as gr

orst_run = onnxruntime.InferenceSession("model.onnx")

idx_to_class = {0: 'chapati', 
                1: 'mukimo', 
                2: 'kukuchoma',
                3: 'kachumbari',
                4: 'ugali',
                5: 'githeri',
                6: 'matoke',
                7: 'pilau',
                8: 'nyamachoma',
                9: 'sukumawiki',
                10: 'bhaji',
                11: 'mandazi',
                12: 'masalachips'}



def predict(image):
    preprocess = transforms.Compose([
        transforms.Resize((256,256)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    input_tensor = preprocess(image)
    input_batch = input_tensor.unsqueeze(0)
    output = orst_run.run(None, {'input': input_batch.numpy()})
    output = torch.from_numpy(output[0])
      
    prediction=F.softmax(output,dim=1)
    predProab,predIndexs = torch.topk(prediction, 3)

    predProab = predProab.numpy()[0]
    predIndexs = predIndexs.numpy()[0]
    confidences = {idx_to_class[predIndexs[i]]: float(predProab[i]) for i in range(3)}

    return confidences

def inference(img):
    return predict(img)




title = 'Kenyan Food Classification'
description = "Kenyan Food Classification"

examples = ['1.jpg','2.jpg','3.jpg','4.jpg']

gr.Interface(inference, gr.Image(type="pil"), "label", server_name="0.0.0.0",title=title,
                                description=description, examples=examples).launch()