import gradio as gr import tensorflow as tf import numpy as np from PIL import Image model = tf.keras.models.load_model("best_model_mobilenetv2_finetune.h5") class_labels = [ 'Ayam Bakar','Ayam Goreng','Bakso','Bakwan', 'Bihun','Capcay','Gado-Gado','Ikan Goreng', 'Kerupuk', 'Martabak Telur','Mie','Nasi Goreng', 'Nasi Putih','Nugget','Opor Ayam','Pempek', 'Rendang','Roti','Sate','Sosis', 'Soto','Tahu','Telur','Tempe', 'Tumis Kangkung','Udang', ] CONFIDENCE_THRESHOLD = 0.5 def process(img): img = img.resize((224,224)) img = np.array(img) / 255.0 return np.expand_dims(img, axis=0) def predict(img, threshold): img = process(img) pred = model.predict(img) class_idx = np.argmax(pred) confidence = pred[0][class_idx] class_name = class_labels[class_idx] if confidence < threshold: return { "label": "Tidak Dikenali", "confidence": float(confidence), "status": "Unknown" } else: return { "label": class_name, "confidence": float(confidence), "status": "OK" } interface = gr.Interface( fn=predict, inputs=[ gr.Image(type="pil"), gr.Slider(0, 1, value=0.5, label="Confidence Threshold") ], outputs=gr.JSON(label="Hasil Prediksi") ) interface.launch()