LungDisease / app.py
ASNVS's picture
up
7d8df0d verified
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Load model
print("Loading model...")
modelo = tf.keras.models.load_model("modelo_multilabel_mejorado3.h5")
print("Model loaded.")
# Model classes
target_classes = ['Atelectasis', 'Effusion', 'Infiltration', 'Nodule', 'Pneumothorax']
UMBRAL_PROBABILIDAD = 0.3 # Minimum 30% to consider a disease relevant
def predecir(img):
img = img.resize((224, 224))
img = np.array(img) / 255.0
img = np.expand_dims(img, axis=0)
pred = modelo.predict(img)[0]
resultados = [(target_classes[i], prob) for i, prob in enumerate(pred)]
resultados = [(enf, p) for enf, p in resultados if p >= UMBRAL_PROBABILIDAD]
resultados.sort(key=lambda x: x[1], reverse=True)
if not resultados:
return "ERROR "
salida = "\n".join([f"{enf}: {p*100:.2f}%" for enf, p in resultados[:2]])
return salida
# Custom CSS to style the background, iframe lines, title, and description
custom_css = """
body {
background-color: #efefef;
}
iframe {
border-top: 4px solid #000;
border-bottom: 4px solid #000;
}
h1, h2, p,.interface-heading, .interface-description {
color: black !important;
}
"""
# Gradio Interface
demo = gr.Interface(
fn=predecir,
inputs=gr.Image(type="pil", label="Upload X ray image"),
outputs="text",
title="Detection of Pulmonary Diseases",
description="Upload a chest X-ray, and the model will identify possible diseases",
theme="default",
css=custom_css
)
# Launch application
demo.launch(server_name="0.0.0.0", share=True)