# # # 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 . import gradio as gr import pickle import os from fastai.vision.all import load_learner import logging logging.basicConfig(format='%(asctime)s %(message)s',) # Creating an object logger = logging.getLogger() # Setting the threshold of logger to DEBUG logger.setLevel(logging.DEBUG) with open("list.dat", 'rb') as f: categories = pickle.load(f) categories = [x.item() for x in categories] logging.debug(f"CATEGORIES: {type(categories)}") logging.debug(f"{type(categories[0])}") model = load_learner("model.pkl") def predict(img): logging.debug(f"{type(img)}") pred, idx, probs = model.predict(img) logging.debug(f"{type(probs)}") logging.debug(f"{type(probs[0])}") 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}) return output image = gr.inputs.Image(shape=(200, 200)) label = gr.outputs.Label() text = "Keep the background dark and uniform for best results" 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, description=text) interface.launch()