File size: 1,837 Bytes
8d7e015
ae2057d
 
652d09f
ae2057d
ae22df9
652d09f
ae2057d
652d09f
ae2057d
652d09f
 
ae2057d
652d09f
 
ae2057d
c3d5f77
ae22df9
ffb6da3
ae22df9
 
c3d5f77
 
 
 
c17a799
8af09bd
c17a799
 
8f0bb61
c17a799
ffb6da3
 
8f0bb61
ffb6da3
c17a799
 
 
ffb6da3
 
 
c17a799
088edb6
ae2057d
 
 
652d09f
 
 
 
ffb6da3
 
652d09f
 
 
 
 
 
ae22df9
8f0bb61
652d09f
ae2057d
 
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
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
import numpy as np
import cv2
import tensorflow as tf

# Load the model
model = tf.keras.models.load_model('model.h5')

def preprocess_image(image):
    resized_img = cv2.resize(image, (180, 180))
    img_array = np.array(resized_img).reshape((1, 180, 180, 3))
    return img_array

def predict_pneumonia(image):
    img_array = preprocess_image(image)
    prediction = model.predict(img_array)[0][0]

    # Use a more robust threshold for determining whether an image has pneumonia
    threshold = 0.5

    if prediction >= threshold:
        pneumonia_prediction = 1
    else:
        pneumonia_prediction = 0

    # Return the probability of each class
    class_probabilities = np.array(model.predict(img_array))

    # Return the top two possible classifications
    top_classes = np.argsort(class_probabilities[0])[-2:]

    # Return the class names and the confidence scores for each class
    class_names = ["Pneumonia", "Normal"]
    confidence_scores = class_probabilities[0]

    return {
        "Pneumonia": pneumonia_prediction,
        "Class probabilities": class_probabilities,
        "Top classes": top_classes,
        "Class names": class_names,
        "Confidence scores": confidence_scores
    }

inputs = gr.inputs.Image(shape=(180, 180))
outputs = gr.outputs.Label(num_top_classes=2)

gradio_interface = gr.Interface(
    fn=predict_pneumonia,
    inputs=inputs,
    outputs=outputs,
    title="Pneumonia X-Ray Classification API",
    description="This API classifies images of chest X-rays as having pneumonia or being normal.",
    examples=[
        ["person1946_bacteria_4875.jpeg"],
        ["person1952_bacteria_4883.jpeg"],
        ["NORMAL2-IM-1427-0001.jpeg"],
        ["NORMAL2-IM-1431-0001.jpeg"]
    ],
    theme="default",
    allow_flagging=False
)

gradio_interface.launch()