Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
# Load the trained model
|
| 8 |
+
model = tf.keras.models.load_model("chest_xray_model.h5")
|
| 9 |
+
class_labels = ["NORMAL", "PNEUMONIA"] # Update if you have more classes
|
| 10 |
+
|
| 11 |
+
# Preprocessing function for uploaded images
|
| 12 |
+
def preprocess_image(img):
|
| 13 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 14 |
+
img = cv2.resize(img, (150, 150)) / 255.0
|
| 15 |
+
img = np.expand_dims(img, axis=0)
|
| 16 |
+
return img
|
| 17 |
+
|
| 18 |
+
# Prediction function
|
| 19 |
+
def predict_chest_xray(img):
|
| 20 |
+
processed_img = preprocess_image(img)
|
| 21 |
+
prediction = model.predict(processed_img)[0]
|
| 22 |
+
predicted_class = class_labels[np.argmax(prediction)]
|
| 23 |
+
confidence = round(100 * np.max(prediction), 2)
|
| 24 |
+
return f"Prediction: {predicted_class} (Confidence: {confidence}%)"
|
| 25 |
+
|
| 26 |
+
# Create Gradio UI
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=predict_chest_xray,
|
| 29 |
+
inputs=gr.Image(type="numpy"),
|
| 30 |
+
outputs="text",
|
| 31 |
+
title="Chest X-Ray Diagnosis",
|
| 32 |
+
description="Upload a chest X-ray image to get a diagnosis prediction."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
interface.launch()
|