import gradio as gr import tensorflow as tf import numpy as np from PIL import Image # Load model model = tf.keras.models.load_model("cervical_newmodel.h5") # Define threshold THRESHOLD = 0.4657 # Preprocessing function (adjust image size based on your model input) def preprocess_image(image): image = image.convert("RGB") image = image.resize((224, 224)) # change to your model's input shape img_array = np.array(image) / 255.0 img_array = np.expand_dims(img_array, axis=0) return img_array # Prediction function def predict_cervical_image(image): if image is None: print("⚠️ Skipped empty call (no image uploaded yet)") return "Please upload a cervical image first." img_array = preprocess_image(image) prediction = model.predict(img_array)[0][0] # assuming output is single sigmoid neuron if prediction > THRESHOLD: label = "🧬 Abnormal (Possible Cancerous Condition)" else: label = "✅ Normal" result = { "Prediction Probability": float(prediction), "Classification": label } return label # Gradio Interface interface = gr.Interface( fn=predict_cervical_image, inputs=gr.Image(type="pil", label="Upload Cervical Image"), outputs=gr.Textbox(label="Result"), title="Cervical Image Classifier", description="Upload a cervical image to classify it as Normal or Abnormal (cancerous). \ This model uses a threshold of 0.4657 for classification." ) # Launch app if __name__ == "__main__": interface.launch()