Spaces:
Sleeping
Sleeping
| import tensorflow as tf | |
| from tensorflow import keras | |
| from PIL import Image | |
| import numpy as np | |
| import gradio as gr | |
| model_path = 'cnn_neumonia.keras' | |
| IMG_HEIGHT = 64 | |
| IMG_WIDTH = 64 | |
| # 1. Cargar el modelo | |
| try: | |
| model = keras.models.load_model(model_path) | |
| #print("Modelo cargado exitosamente.") | |
| except Exception as e: | |
| print(f"Error al cargar el modelo: {e}") | |
| # Detener la ejecuci贸n si el modelo no se carga | |
| exit() | |
| def preprocess_image_for_prediction(image: Image.Image, target_size=(IMG_HEIGHT, IMG_WIDTH)): | |
| img = image.convert('RGB') | |
| img = img.resize(target_size) | |
| img_array = np.array(img) | |
| img_array = np.expand_dims(img_array, axis=0) # A帽adir dimensi贸n de lote | |
| img_array = img_array / 255.0 # Normalizar a [0, 1] | |
| return img_array | |
| def predict_pneumonia(image_path_or_object): | |
| if isinstance(image_path_or_object, str): | |
| # Si es una ruta, cargar la imagen | |
| image = Image.open(image_path_or_object) | |
| else: | |
| # Si ya es un objeto PIL Image (como en Gradio) | |
| image = image_path_or_object | |
| # Preprocesar la imagen | |
| processed_image = preprocess_image_for_prediction(image, target_size=(IMG_HEIGHT, IMG_WIDTH)) | |
| # Realizar la predicci贸n | |
| prediction = model.predict(processed_image, verbose=0) # verbose=0 para no imprimir el progreso | |
| # Interpretar la predicci贸n | |
| probability = prediction[0][0] | |
| if probability > 0.5: | |
| message = f"隆Tiene Neumon铆a! (Probabilidad: {probability:.2f})" | |
| else: | |
| message = f"No tiene Neumon铆a (Probabilidad: {probability:.2f})" | |
| return message | |
| iface = gr.Interface( | |
| fn=predict_pneumonia, | |
| title="Predicci贸n de Neumon铆a por Radiograf铆a", | |
| description="Carga una imagen de radiograf铆a y predice si contiene neumon铆a.", | |
| inputs=gr.Image(type="pil",label="Sube una imagen de rayos X de t贸rax"), | |
| outputs=gr.Textbox(label="Diagnostico"), | |
| examples=[ | |
| 'NORMAL2-IM-0052-0001.jpeg', | |
| 'ryct.2020200034.fig5-day4.jpeg' | |
| ] | |
| ) | |
| #Launch only when script runs directly | |
| #if __name__ == "__main__": | |
| # iface.launch(debug=True) | |
| iface.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| ssr_mode=False | |
| ) | |