File size: 1,664 Bytes
a6b95e7
91687c0
 
 
 
dc2240a
a6b95e7
 
 
 
 
 
 
 
91687c0
 
a6b95e7
dc2240a
a6b95e7
 
91687c0
a6b95e7
91687c0
a6b95e7
 
dc2240a
3d100de
91687c0
 
a6b95e7
91687c0
a6b95e7
 
 
 
 
 
 
 
 
91687c0
a6b95e7
91687c0
 
a6b95e7
91687c0
 
a6b95e7
91687c0
 
a6b95e7
91687c0
a6b95e7
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
import os
import gradio as gr
import tensorflow as tf
import numpy as np
import cv2

# Define the model path
MODEL_PATH = "chest_xray_model.h5"

# Check if the model file exists
if not os.path.exists(MODEL_PATH):
    raise FileNotFoundError(
        f"Model file '{MODEL_PATH}' not found. Please upload it to your Hugging Face Space."
    )

# Load the trained model
model = tf.keras.models.load_model(MODEL_PATH)

# Get class labels from the trained model
class_labels = ["COVID-19", "NORMAL", "PNEUMONIA"]  # Update if needed

# Function to preprocess the input image
def preprocess_image(img):
    """Prepares the image for model prediction."""
    img = cv2.resize(img, (150, 150))  # Resize to match model input shape
    img = img.astype(np.float32) / 255.0  # Normalize pixel values
    img = np.expand_dims(img, axis=0)  # Add batch dimension
    return img

# Function to make predictions
def predict_chest_xray(img):
    """Runs inference on an uploaded X-ray image."""
    try:
        processed_img = preprocess_image(img)
        prediction = model.predict(processed_img)[0]
        predicted_class = class_labels[np.argmax(prediction)]
        confidence = round(100 * np.max(prediction), 2)
        return f"Prediction: {predicted_class} (Confidence: {confidence}%)"
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio interface
interface = gr.Interface(
    fn=predict_chest_xray,
    inputs=gr.Image(type="numpy"),
    outputs="text",
    title="Chest X-Ray Diagnosis",
    description="Upload a chest X-ray image to get a diagnosis prediction.",
)

# Run the Gradio app
if __name__ == "__main__":
    interface.launch()