File size: 1,547 Bytes
9526c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40ded74
 
 
9526c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()