File size: 1,116 Bytes
21889ee
 
 
 
 
376039f
21889ee
 
d098db7
21889ee
 
 
 
 
 
 
 
376039f
 
 
 
21889ee
376039f
 
 
 
 
 
 
 
 
21889ee
 
376039f
 
 
21889ee
 
 
 
 
 
 
 
 
 
 
 
376039f
21889ee
 
 
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
53
import tensorflow as tf
import gradio as gr
import numpy as np
import json
from PIL import Image
from tensorflow.keras.applications.efficientnet import preprocess_input

# Load model
model = tf.keras.models.load_model("polyp_efficientnet_model.h5")

# Load class names
with open("class_names.json") as f:
    class_names = json.load(f)

IMG_SIZE = (224, 224)

def predict(image):
    if image is None:
        return None

    # Resize
    image = image.resize(IMG_SIZE)

    # Convert to numpy
    image = np.array(image)

    # Ensure RGB
    if image.shape[-1] == 4:
        image = image[..., :3]

    # Batch dimension
    image = np.expand_dims(image, axis=0)

    # ✅ CORRECT preprocessing
    image = preprocess_input(image)

    preds = model.predict(image)[0]

    return {
        class_names[i]: float(preds[i])
        for i in range(len(class_names))
    }

interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=4),
    title="Polyp Disease Classification",
    description="EfficientNet-based medical image classifier"
)

interface.launch()