File size: 2,297 Bytes
4abf419
357257b
82abee9
357257b
82abee9
 
357257b
4abf419
 
357257b
4abf419
 
357257b
 
73ded6e
 
 
 
ab9a86f
6f2708e
48e41f4
 
6f2708e
73ded6e
 
 
362c3f8
6f2708e
 
73ded6e
 
 
5996683
d28f32d
7bed125
d28f32d
7bed125
73ded6e
d5bc046
73ded6e
5495328
73ded6e
3dac242
73ded6e
dcb5199
7ca27ea
 
73ded6e
 
b739ca5
0cae73c
b739ca5
 
556be06
6e38247
73ded6e
6be91ad
b739ca5
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
58
# # # Copyright ©️ 2022 Syed Salman Habeeb Quadri 
  
# # # This file is part of Blatt. 
  

# # # Blatt is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 
  

# # # Blatt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
  

# # # You should have received a copy of the GNU General Public License along with Blatt. If not, see <https://www.gnu.org/licenses/>.


import gradio as gr
import pickle
import os
from fastai.vision.all import load_learner
import logging
#logging.basicConfig(format='%(asctime)s %(message)s',)
 
# Setting the threshold of logger to DEBUG
#logger.setLevel(logging.DEBUG)

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

#logging.warning(f"CATEGORIES: {type(categories)}")
#logging.warning(f"{type(categories[0])}")
model = load_learner("model.pkl")

def predict(img):
    logging.warning(f"{type(img)}")
    try:
        logging.warning(f"Shape: {img.shape}")
    except AttributeError:
        logging.warning(f"Size: {img.size}")
    pred, idx, probs = model.predict(img)
    dict1 = dict(zip(categories, map(float, probs.numpy())))
    dict1 = dict(sorted(dict1.items(), key=lambda item : item[1]))
    output = {key:dict1[key] for key in list(dict1.keys())[-3:]}
    sum = 0
    for i in list(dict1.keys())[-3:]:
        sum += dict1[i]
    output.update({"Other" : 1.0 - sum})
    if output['Other'] < 0:
        output['Other'] = 0
    return output


image = gr.inputs.Image(shape=(256, 256))
label = gr.outputs.Label()
text = "<a color='#00FF00'>Keep the background dark and uniform for best results<a>"
examples = ["Potato_late_blight.jpg", "apple_black_rot.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, description=text)
interface.launch()