File size: 881 Bytes
73ded6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pickle
import os
from fastai.vision.all import load_learner

with open("list.dat", 'rb') as f:
    categories = pickle.load(f)
    
model = load_learner("model.pkl")

def predict(img):
    pred, idx, probs = model.predict(img)
    dict1 = dict(zip(categories, map(float, preds)))
    dict1 = dict(sorted(dict1.items(), key=lambda item : item[1]))
    output = {key:dict1[key] for key in dict1.keys()[-3:]}
    sum = 0
    for i in dict1.keys()[-3:]:
        sum += dict1[i]
    output.update("Other", 1.0 - sum)
    return output

image = gr.inputs.Image(shape=(264, 264))
label = gr.outputs.Label()
examples = ["cherry_leaf.jpg", "frogeye_spots_apple_leaf.jpg", "apple_leaf.jpg"]
examples = [os.path.join("images", example) for example in examples]

interface = gr.Interface(fn=predict, inputs=image, outputs=label, examples=examples)
interface.launch()