import gradio as gr import tensorflow as tf from PIL import Image import numpy as np model = tf.keras.models.load_model('food_or_not_v2.h5') def prob(x): probs = { "Food": 0.5, "Not Food": 0.5 } if(x<0.5): probs["Food"] = (1-(x*2)) probs["Not Food"] = (x*2) else: probs["Food"] = 1-(1-((1-x)*2)) probs["Not Food"] = (1-((1-x)*2)) return probs def get_pred(img): try: img = Image.fromarray(img.astype('uint8'), 'RGB') img = img.resize((224, 224)) img = np.array(img) img = np.expand_dims(img, axis=0) val = model.predict(img) val = val[0][0] return prob(val) except Exception as e: return "Error" iface = gr.Interface( fn=get_pred, inputs='image', outputs='label', title="Food or Not Food Classifier", description="Upload an image to check if it contains food or not.", examples=["example1.jpg", "example2.png"] # Provide paths to example images ) iface.launch()