CXR / app.py
fecia's picture
Update app.py
54b9d20 verified
raw
history blame
47.2 kB
import gradio as gr
import os
import io
import png
import tensorflow as tf
import tensorflow_text as tf_text
import tensorflow_hub as tf_hub
import numpy as np
from PIL import Image
from huggingface_hub import snapshot_download, HfFolder
from sklearn.metrics.pairwise import cosine_similarity
import traceback
import time
import pandas as pd # Para formatear la salida en tabla
# --- Configuración ---
MODEL_REPO_ID = "google/cxr-foundation"
MODEL_DOWNLOAD_DIR = './hf_cxr_foundation_space' # Directorio dentro del contenedor del Space
# Umbrales
SIMILARITY_DIFFERENCE_THRESHOLD = 0.1
POSITIVE_SIMILARITY_THRESHOLD = 0.1
print(f"Usando umbrales: Comp Δ={SIMILARITY_DIFFERENCE_THRESHOLD}, Simp τ={POSITIVE_SIMILARITY_THRESHOLD}")
# --- Prompts ---
criteria_list_positive = [
"optimal centering", "optimal inspiration", "optimal penetration",
"complete field of view", "scapulae retracted", "sharp image", "artifact free"
]
criteria_list_negative = [
"poorly centered", "poor inspiration", "non-diagnostic exposure",
"cropped image", "scapulae overlying lungs", "blurred image", "obscuring artifact"
]
# --- Funciones Auxiliares (Integradas o adaptadas) ---
# @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)]) # Puede ayudar rendimiento
def preprocess_text(text):
"""Función interna del preprocesador BERT."""
return bert_preprocessor_global(text) # Asume que bert_preprocessor_global está cargado
def bert_tokenize(text, preprocessor):
"""Tokeniza texto usando el preprocesador BERT cargado globalmente."""
if preprocessor is None:
raise ValueError("BERT preprocessor no está cargado.")
if not isinstance(text, str): text = str(text)
# Ejecutar el preprocesador
out¡ = preprocessor(tf.constant([text.lower()]))
# Extraer y procesar IDs y máscaras
ids = out['input_word_ids'].numpy().astype(np.int32)
masks =Por supuesto! Aquí está el código completo del archivo `app.py` para out['input_mask'].numpy().astype(np.float32)
paddings = 1.0 - masks
# Reemplazar token [SEP] (102) por 0 y marcar Gradio con la corrección del tema oscuro (eliminando `text_color_subdued`).
como padding
end_token_idx = (ids == 10```python
import gradio as gr
import os
import io
import png
import tensorflow as tf2)
ids[end_token_idx] = 0
import tensorflow_text as tf_text
import tensorflow_hub as tf paddings[end_token_idx] = 1.0_hub
import numpy as np
from PIL import Image
from huggingface_hub import snapshot_download,
# Asegurar las dimensiones (B, T, S) -> ( HfFolder
from sklearn.metrics.pairwise import cosine_similarity
import1, 1, 128)
# El preprocesador puede devolver (1, 128), necesitamos (1, 1, 12 traceback
import time
import pandas as pd # Para formatear la salida en tabla
# --- Configuración ---8)
if ids.ndim == 2: ids = np.expand_dims(ids, axis=1)
if paddings.
MODEL_REPO_ID = "google/cxr-foundation"
ndim == 2: paddings = np.expand_dims(paddMODEL_DOWNLOAD_DIR = './hf_cxr_foundation_space'ings, axis=1)
# Verificar formas finales
expected_shape = (1 # Directorio dentro del contenedor del Space
SIMILARITY_DIFFERENCE_THRESHOLD = , 1, 128)
if ids.shape != expected_shape:
# Intentar reajustar si es necesario (puede0.1
POSITIVE_SIMILARITY_THRESHOLD = 0.1 pasar con algunas versiones)
if ids.shape == (1,1
print(f"Usando umbrales: Comp Δ={SIMILAR28): ids = np.expand_dims(ids, axis=1ITY_DIFFERENCE_THRESHOLD}, Simp τ={POSITIVE_SIMILARITY_THRESHOLD}")
# --- Prompts ---
criteria_list_positive)
else: raise ValueError(f"Shape incorrecta para ids: = [
"optimal centering", "optimal inspiration", "optimal penetration",
"complete field of view {ids.shape}, esperado {expected_shape}")
if paddings", "scapulae retracted", "sharp image", "artifact free"
].shape != expected_shape:
if paddings.shape == (
criteria_list_negative = [
"poorly centered", "1,128): paddings = np.expand_dims(paddings, axis=1)poor inspiration", "non-diagnostic exposure",
"cropped image", "scapulae overlying lungs
else: raise ValueError(f"Shape incorrecta para paddings: {paddings.shape}, esperado {expected_shape}")
return ids, paddings
", "blurred image", "obscuring artifact"
]
# --- Funciones Auxiliadef png_to_tfexample(image_array: np.ndarray) -> tf.train.Example:
"""Crea tf.train.Example desde NumPy array (res (Integradas o adaptadas) ---
def bert_tokenize(text, preprocessor):
escala de grises)."""
if image_array.ndim == """Tokeniza texto usando el preprocesador BERT cargado globalmente."""
if 3 and image_array.shape[2] == 1:
preprocessor is None: raise ValueError("BERT preprocessor no está cargado.") image_array = np.squeeze(image_array, axis=2) # Asegurar 2D
elif image_array.ndim != 2:
raise ValueError(f'Array debe ser 2-D (
if not isinstance(text, str): text = str(text)escala de grises). Dimensiones actuales: {image_array.ndim
out = preprocessor(tf.constant([text.lower()]))}')
image = image_array.astype(np.float32)
min
ids = out['input_word_ids'].numpy().astype(_val = image.min()
max_val = image.max()
np.int32)
masks = out['input_mask'].# Evitar división por cero si la imagen es constante
if max_val <= min_val:numpy().astype(np.float32)
paddings =
# Si es constante, tratar como uint8 si el rango original lo permitía,
1.0 - masks
end_token_idx = (ids == 102)
# o simplemente ponerla a 0 si es float.
if image_array. ids[end_token_idx] = 0
paddings[end_token_idx] = 1.0
if ids.ndim == 2dtype == np.uint8 or (min_val >= 0 and max: ids = np.expand_dims(ids, axis=1)
if paddings.ndim == 2: paddings = np.expand_val <= 255):
pixel_array = image._dims(paddings, axis=1)
expected_shape = (1,astype(np.uint8)
bitdepth = 8
1, 128)
if ids.shape != expectedelse: # Caso flotante constante o fuera de rango uint8
pixel__shape:
if ids.shape == (1,128): ids = np.expand_dims(ids, axis=1)
else: raise ValueErrorarray = np.zeros_like(image, dtype=np.uint1(f"Shape incorrecta para ids: {ids.shape}, esperado {6)
bitdepth = 16
else:
expected_shape}")
if paddings.shape != expected_shape:image -= min_val # Mover mínimo a cero
current_max = max_val -
if paddings.shape == (1,128): padd min_val
# Escalar a 16-bit para mayor precisión si noings = np.expand_dims(paddings, axis=1) era uint8 originalmente
if image_array.dtype != np.uint8:
else: raise ValueError(f"Shape incorrecta para paddings:
image *= 65535 / current_max
pixel_array = {paddings.shape}, esperado {expected_shape}")
return ids, paddings
image.astype(np.uint16)
bitdepth = def png_to_tfexample(image_array: np.ndarray)16
else:
# Si era uint8, mantener el rango y tipo
# La resta del min ya la dejó en [0, current_max]
-> tf.train.Example:
"""Crea tf.train.Example desde NumPy array ( # Escalar a 255 si es necesario
image *= 255 / current_escala de grises)."""
if image_array.ndim ==max
pixel_array = image.astype(np.uint8) 3 and image_array.shape[2] == 1:
bitdepth = 8
# Codificar como PNG
output = io.Bytes image_array = np.squeeze(image_array, axis=2IO()
png.Writer(
width=pixel_array.) # Asegurar 2D
elif image_array.ndim != 2shape[1],
height=pixel_array.shape[0],:
raise ValueError(f'Array debe ser 2-D (
greyscale=True,
bitdepth=bitdepth
escala de grises). Dimensiones actuales: {image_array.ndim).write(output, pixel_array.tolist())
png_bytes = output.getvalue()
}')
image = image_array.astype(np.float32)
min_val # Crear tf.train.Example
example = tf.train.Example()
, max_val = image.min(), image.max()
if features = example.features.feature
features['image/encoded']. max_val <= min_val: # Imagen constante
if image_array.dtype == np.uint8 or (min_val >= 0 and max_bytes_list.value.append(png_bytes)
features['image/format'].bytes_list.value.append(b'png')
return example
def generate_image_embedding(img_np,val <= 255):
pixel_array = image.astype(np.uint8); bitdepth = 8
else:
pixel_array = np.zeros_like(image elixrc_infer, qformer_infer):
"""Genera embedding final, dtype=np.uint16); bitdepth = 16
else: # Imagen con rango
image -= min_val
current_max = max_val - min de imagen."""
if elixrc_infer is None or qformer_infer is None:
raise ValueError("Modelos ELIXR-C o Q_val
if image_array.dtype != np.uint8: #Former no cargados.")
try:
# 1. EL Escalar a 16-bit si no era uint8
image *= 6IXR-C
serialized_img_tf_example = png_5535 / current_max
pixel_array = image.to_tfexample(img_np).SerializeToString()
elixrc_output = elixrcastype(np.uint16); bitdepth = 16
_infer(input_example=tf.constant([serialized_img_tf_example]))else: # Mantener rango uint8
image *= 255 / current_max
pixel_array = image.astype(np.uint
elixrc_embedding = elixrc_output['feature_maps_0'].numpy()
8); bitdepth = 8
output = io.BytesIO()
png.Writer(width=pixel_array.shape[1], height=pixel_array.shape print(f" Embedding ELIXR-C shape: {elixrc_embedding.[0], greyscale=True, bitdepth=bitdepth).write(shape}")
# 2. QFormer (Imagen)
qformer_input_output, pixel_array.tolist())
png_bytes = output.getvalue()
example = tf.train.Example()
features = example.features.feature
features['image/encoded'].bytes_list.value.img = {
'image_feature': elixrc_embedding.tolist(),
append(png_bytes)
features['image/format'].bytes_ 'ids': np.zeros((1, 1, 12list.value.append(b'png')
return example
def8), dtype=np.int32).tolist(), # Texto vacío
'paddings': generate_image_embedding(img_np, elixrc_infer, np.ones((1, 1, 128), dtype= qformer_infer):
"""Genera embedding final de imagen."""
if elixnp.float32).tolist(), # Todo padding
}
qformer_output_img = qformer_infer(**qformer_input_imgrc_infer is None or qformer_infer is None: raise ValueError(")
image_embedding = qformer_output_img['all_contrastive_imgModelos ELIXR-C o QFormer no cargados.")
_emb'].numpy()
# Ajustar dimensiones si es necesario
if image_try:
# 1. ELIXR-C
serialized_embedding.ndim > 2:
print(f" Ajustimg_tf_example = png_to_tfexample(img_npando dimensiones embedding imagen (original: {image_embedding.shape})")
).SerializeToString()
elixrc_output = elixrc_infer( image_embedding = np.mean(
image_embedding,
input_example=tf.constant([serialized_img_tf_example])) axis=tuple(range(1, image_embedding.ndim -
elixrc_embedding = elixrc_output['feature_maps_0'].numpy1))
)
if image_embedding.ndim == 1()
print(f" Embedding ELIXR-C shape: {elixrc_embedding.:
image_embedding = np.expand_dims(image_embedding, axis=0)
elif image_embedding.ndim == 1:
shape}")
# 2. QFormer (Imagen)
qformer_input_ image_embedding = np.expand_dims(image_embedding, axis=0) # Asegurar 2D
print(f" Embedding final imagen shape: {image_embedding.shape}")
if image_embedding.ndimimg = {
'image_feature': elixrc_embedding.tolist(),
!= 2:
raise ValueError(f"Embedding final de imagen no tiene 2 dimensiones: { 'ids': np.zeros((1, 1, 12image_embedding.shape}")
return image_embedding
except Exception8), dtype=np.int32).tolist(), # Texto vacío
'paddings as e:
print(f"Error generando embedding de imagen: {e}")
': np.ones((1, 1, 128), dtype=np.floattraceback.print_exc()
raise # Re-lanzar32).tolist(), # Todo padding
}
qformer_output_img = qformer_ la excepción para que Gradio la maneje
def calculate_similarities_and_classify(infer(**qformer_input_img)
image_embedding = qformer_output_image_embedding, bert_preprocessor, qformer_infer):
img['all_contrastive_img_emb'].numpy()
# Ajustar dimensiones
if"""Calcula similitudes y clasifica."""
if image_embedding is None: raise ValueError("Embedding image_embedding.ndim > 2:
print(f" Ajustando de imagen es None.")
if bert_preprocessor is None: raise ValueError("Preprocesador BERT es dimensiones embedding imagen (original: {image_embedding.shape})")
image_embedding = np.mean(image_embedding, axis=tuple( None.")
if qformer_infer is None: raise ValueError("Qrange(1, image_embedding.ndim - 1)))
if image_embedding.ndim == Former es None.")
detailed_results = {}
print("\n--- Calculando similitudes y clasific1: image_embedding = np.expand_dims(image_embedding,ando ---")
for i in range(len(criteria_list_positive)):
axis=0) # Asegurar 2D
print(f" Embedding final imagen shapepositive_text = criteria_list_positive[i]
negative_: {image_embedding.shape}")
if image_embedding.ndimtext = criteria_list_negative[i]
criterion_name = != 2: raise ValueError(f"Embedding final imagen no tiene 2 dims positive_text # Usar prompt positivo como clave
print(f": {image_embedding.shape}")
return image_embedding
except Exception as e:
Procesando criterio: \"{criterion_name}\"")
similarity_positive, similarity print(f"Error generando embedding imagen: {e}"); traceback.print_exc(); raise
def calculate_similarities_and_classify(image_embedding, bert_preprocessor_negative, difference = None, None, None
classification_comp, classification_simp = "ERROR", "ERROR"
try:
#, qformer_infer):
"""Calcula similitudes y clasifica."""
if image_embedding is None: raise ValueError("Embedding imagen es None.")
if bert_ 1. Embedding Texto Positivo
tokens_pos, paddings_pos = bert_tokenize(preprocessor is None: raise ValueError("Preprocesador BERT es None.")
if qformer_positive_text, bert_preprocessor)
qformer_input_infer is None: raise ValueError("QFormer es None.")
detailed_results = {}
print("\n--- Calculando similitudes y clasificando ---")
for i intext_pos = {
'image_feature': np.zeros([ range(len(criteria_list_positive)):
positive_text,1, 8, 8, 1376], dtype= negative_text = criteria_list_positive[i], criteria_list_np.float32).tolist(), # Dummy
'ids': tokensnegative[i]
criterion_name = positive_text # Usar prompt positivo_pos.tolist(), 'paddings': paddings_pos.tolist(),
}
text como clave
print(f"Procesando criterio: \"{criterion_name}\"_embedding_pos = qformer_infer(**qformer_input_text")
similarity_positive, similarity_negative, difference = None, None, None
classification__pos)['contrastive_txt_emb'].numpy()
if text_embedding_poscomp, classification_simp = "ERROR", "ERROR"
try:.ndim == 1: text_embedding_pos = np.expand_
# 1. Embeddings de Texto
tokens_pos, paddings_pos = bert_tokenize(positive_text, bert_preprocessordims(text_embedding_pos, axis=0)
# )
qformer_input_pos = {'image_feature': np2. Embedding Texto Negativo
tokens_neg, paddings_neg.zeros([1, 8, 8, 1376 = bert_tokenize(negative_text, bert_preprocessor)
qformer_input_text], dtype=np.float32).tolist(), 'ids': tokens_pos.tolist(), 'padd_neg = {
'image_feature': np.zeros([1ings': paddings_pos.tolist()}
text_embedding_pos = qformer_infer(**qformer_input_pos)['contrastive_txt_emb'].numpy(), 8, 8, 1376], dtype=np
if text_embedding_pos.ndim == 1: text_embedding_pos = np.expand_dims(text_embedding_pos, axis=0).float32).tolist(), # Dummy
'ids': tokens_neg.tolist(), 'paddings': paddings_neg.tolist(),
tokens_neg, paddings_neg = bert_tokenize(negative_text, bert_preprocessor)
qformer_input_neg
}
text_embedding_neg = qformer_infer(** = {'image_feature': np.zeros([1, 8, qformer_input_text_neg)['contrastive_txt_emb'].numpy()
if text_embedding_neg.ndim == 1: text_embedding_neg8, 1376], dtype=np.float32). = np.expand_dims(text_embedding_neg, axis=0tolist(), 'ids': tokens_neg.tolist(), 'paddings':)
# Verificar compatibilidad de dimensiones para similitud
if image_embedding paddings_neg.tolist()}
text_embedding_neg = qformer_infer(**qformer_input_neg)['contrastive_txt_.shape[1] != text_embedding_pos.shape[1]:emb'].numpy()
if text_embedding_neg.ndim ==
raise ValueError(f"Dimensión incompatible: Imagen ({image_embedding.shape[11: text_embedding_neg = np.expand_dims(text_embedding_neg, axis=0)
# Verificar dimensiones
if image_embedding.shape]}) vs Texto Pos ({text_embedding_pos.shape[1]})")
if[1] != text_embedding_pos.shape[1]: raise ValueError(f"Dim mismatch: Img ({image_embedding.shape[1]}) vs Pos ({text_embedding_pos.shape[1]})")
if image_embedding image_embedding.shape[1] != text_embedding_neg.shape.shape[1] != text_embedding_neg.shape[1]: raise ValueError(f"Dim mismatch: Img ({image_embedding.shape[1]:
raise ValueError(f"Dimensión incompatible: Imagen ({image_embedding.shape[1[1]}) vs Neg ({text_embedding_neg.shape[1]})")
# 2. Calcular Similitudes
similarity_positive = cosine_similarity(image]}) vs Texto Neg ({text_embedding_neg.shape[1]})")_embedding, text_embedding_pos)[0][0]
similarity_negative =
# 3. Calcular Similitudes
similarity_positive = cosine_similarity(image_embedding cosine_similarity(image_embedding, text_embedding_neg)[0][, text_embedding_pos)[0][0]
similarity_negative0]
# 3. Clasificar
difference = similarity_positive - similarity = cosine_similarity(image_embedding, text_embedding_neg)[0_negative
classification_comp = "PASS" if difference > SIMILARITY_DIFFERENCE][0]
print(f" Sim (+)={similarity_positive_THRESHOLD else "FAIL"
classification_simp = "PASS" if:.4f}, Sim (-)={similarity_negative:.4f}")
similarity_positive > POSITIVE_SIMILARITY_THRESHOLD else "FAIL"# 4. Clasificar
difference = similarity_positive - similarity_
print(f" Sim(+)={similarity_positive:.4f},negative
classification_comp = "PASS" if difference > SIMILARITY_DIFFERENCE Sim(-)={similarity_negative:.4f}, Diff={difference:.4f_THRESHOLD else "FAIL"
classification_simp = "PASS" if} -> Comp:{classification_comp}, Simp:{classification_simp}")
except Exception as similarity_positive > POSITIVE_SIMILARITY_THRESHOLD else "FAIL" e:
print(f" ERROR procesando criterio '{criterion_name}': {e}"); traceback.print_exc()
# Mantener clasificaciones como "ERROR
print(f" Diff={difference:.4f} -> Comp: {classification_comp},"
detailed_results[criterion_name] = {
'positive_prompt': Simp: {classification_simp}")
except Exception as e:
print(f" ERROR procesando criterio '{criterion_name}': {e}")
traceback.print_exc()
# Mantener clasificaciones como "ERROR" positive_text, 'negative_prompt': negative_text,
'similarity_positive': float(similarity_positive) if similarity_positive is not None else None,
# Guardar resultados
detailed_results[criterion_name] = {
'similarity_negative': float(similarity_negative) if similarity_negative'positive_prompt': positive_text,
'negative_prompt': is not None else None,
'difference': float(difference) if negative_text,
'similarity_positive': float(similarity_positive difference is not None else None,
'classification_comparative': classification) if similarity_positive is not None else None,
'similarity__comp, 'classification_simplified': classification_simp
}
return detailed_resultsnegative': float(similarity_negative) if similarity_negative is not None else None,
'difference': float(difference) if difference is not None
# --- Carga Global de Modelos ---
print("--- Iniciando carga global de modelos else None,
'classification_comparative': classification_comp,
---")
start_time = time.time()
models_loaded = False
bert_preprocessor_global = None
elixrc_infer 'classification_simplified': classification_simp
}
return detailed_results
# ---_global = None
qformer_infer_global = None
try: Carga Global de Modelos ---
# Se ejecuta UNA VEZ al iniciar la
hf_token = os.environ.get("HF_TOKEN") # Leer aplicación Gradio/Space
print("--- Iniciando carga global de modelos ---")
start_ token desde secretos del Space
if hf_token: print("HFtime = time.time()
models_loaded = False
bert_pre_TOKEN encontrado, usando para autenticación.")
os.makedirs(MODEL_DOWNLOADprocessor_global = None
elixrc_infer_global = None
_DIR, exist_ok=True)
print(f"Descargando/verificando modelos en: {MODEL_DOWNLOAD_DIR}")qformer_infer_global = None
try:
# Añadir token si
snapshot_download(repo_id=MODEL_REPO_ID, local_dir=MODEL_DOWNLOAD_DIR,
allow_patterns=['elixr es necesario (para repos privados o gated)
hf_token = os.environ.get("-c-v2-pooled/*', 'pax-elixr-b-text/*'],
local_dir_use_symlinks=False, token=hf_token) # Pasar token aquí
print("Modelos descargados/verificados.")
HF_TOKEN") # Leer token desde secretos del Space
# if hf_token:
print("Cargando Preprocesador BERT...")
bert_preprocess# print("Usando HF_TOKEN para autenticación.")
# # HfFolder.save_token(hf_token) # Esto no siempre funciona bien en entornos server_handle = "https://tfhub.dev/tensorflow/bert_enless
# Crear directorio si no existe
os.makedirs(MODEL_DOWNLOAD_DIR_uncased_preprocess/3"
bert_preprocessor_global, exist_ok=True)
print(f"Descargando/verificando modelos en = tf_hub.KerasLayer(bert_preprocess_handle)
print("Preprocesador BERT: {MODEL_DOWNLOAD_DIR}")
snapshot_download(repo_id=MODEL cargado.")
print("Cargando ELIXR-C...")_REPO_ID, local_dir=MODEL_DOWNLOAD_DIR,
elixrc_model_path = os.path.join( allow_patterns=['elixr-c-v2-pooled/*', 'pax-elixrMODEL_DOWNLOAD_DIR, 'elixr-c-v2--b-text/*'],
local_dir_use_symlinkspooled')
elixrc_model = tf.saved_model.=False, # Evitar symlinks
token=hf_token) # Pasar tokenload(elixrc_model_path)
elixrc_infer_global = elixrc_model.signatures['serving_default']
print("Modelo aquí
print("Modelos descargados/verificados.")
# C ELIXR-C cargado.")
print("Cargando Qargar Preprocesador BERT desde TF Hub
print("Cargando Preprocesador BERT...")
Former (ELIXR-B Text)...")
qformer_model_path = os.path.join(MODEL_DOWNLOAD_DIR, '# Usar handle explícito puede ser más robusto en algunos entornos
bert_preprocess_pax-elixr-b-text')
qformer_handle = "https://tfhub.dev/tensorflow/bert_en_model = tf.saved_model.load(qformer_model_pathuncased_preprocess/3"
bert_preprocessor_global =)
qformer_infer_global = qformer_model.signatures['serving_default']
tf_hub.KerasLayer(bert_preprocess_handle)
print("Modelo QFormer cargado.")
models_loaded = True
end_print("Preprocesador BERT cargado.")
# Cargar ELIXR-C
print("Cargando ELIXR-C...")
elixrctime = time.time()
print(f"--- Modelos cargados global_model_path = os.path.join(MODEL_DOWNLOAD_DIRmente con éxito en {end_time - start_time:.2f}, 'elixr-c-v2-pooled')
el segundos ---")
except Exception as e:
models_loaded = False
print(ixrc_model = tf.saved_model.load(elixrcf"--- ERROR CRÍTICO DURANTE LA CARGA GLOBAL DE MODELOS_model_path)
elixrc_infer_global = el ---"); print(e); traceback.print_exc()
# --- Función Principal de Procesamiento paraixrc_model.signatures['serving_default']
print("Modelo Gradio ---
def assess_quality_and_update_ui(image ELIXR-C cargado.")
# Cargar QFormer (_pil):
"""Procesa la imagen y devuelve actualizaciones para la UI."""ELIXR-B Text)
print("Cargando QFormer
if not models_loaded:
raise gr.Error("Error: Los (ELIXR-B Text)...")
qformer_model_ modelos no se pudieron cargar. La aplicación no puede procesar imágenes.")
if image_pil is Nonepath = os.path.join(MODEL_DOWNLOAD_DIR, 'p:
# Devuelve valores por defecto/vacíos y controla la visibilidad
return (
ax-elixr-b-text')
qformer_model gr.update(visible=True), # Muestra bienvenida
gr.update(visible= = tf.saved_model.load(qformer_model_path)
qformer_infer_global = qformer_model.signatures['False), # Oculta resultados
None, # Borra imagen de salidaserving_default']
print("Modelo QFormer cargado.")
gr.update(value="N/A"), # Borra etiqueta
pdmodels_loaded = True
end_time = time.time()
.DataFrame(), # Borra dataframe
None # Borra JSON
)
print("\n--- Iniciando evaluación para nueva imagen ---")
start print(f"--- Modelos cargados globalmente con éxito en {end_time_process_time = time.time()
try:
# - start_time:.2f} segundos ---")
except Exception as e:
models_loaded = False
print(f"--- ERROR CRÍTICO DUR 1. Convertir a NumPy
img_np = np.arrayANTE LA CARGA GLOBAL DE MODELOS ---")
print(e)
traceback.print_(image_pil.convert('L'))
print(f"Imagenexc()
# Gradio se iniciará, pero la función de análisis fallará. convertida a NumPy. Shape: {img_np.shape}, Tipo:
# --- Función Principal de Procesamiento para Gradio ---
def assess_quality_and_ {img_np.dtype}")
# 2. Generar Embeddingupdate_ui(image_pil):
"""Procesa la imagen y devuelve actualizaciones
print("Generando embedding de imagen...")
image_embedding = generate_image_embedding(img_np, elixrc_infer_global, q para la UI."""
if not models_loaded:
raise grformer_infer_global)
print("Embedding de imagen generado.")
.Error("Error: Los modelos no se pudieron cargar. La aplicación no puede procesar imágenes.")
# 3. Clasificar
print("Calculando similitudes y clasificando criterios if image_pil is None:
# Devuelve valores por defecto/vacíos...")
detailed_results = calculate_similarities_and_classify(image_embedding, bert_preprocessor_global, qformer_infer_ y controla la visibilidad
return (
gr.update(visible=Trueglobal)
print("Clasificación completada.")
# ), # Muestra bienvenida
gr.update(visible=False), # Oculta resultados
4. Formatear Resultados
output_data, passed_count,None, # Borra imagen de salida
gr.update(value="N/A total_count = [], 0, 0
for criterion, details in detailed_results.items"), # Borra etiqueta
pd.DataFrame(), # Borra dataframe():
total_count += 1
sim_pos = details
None # Borra JSON
)
print("\n--- Iniciando evaluación['similarity_positive']
sim_neg = details['similarity_negative para nueva imagen ---")
start_process_time = time.time']
diff = details['difference']
comp = details['classification_comparative']
simp = details['classification_simplified']
()
try:
# 1. Convertir a NumPy
img_np = np.array(image_pil.convert('Loutput_data.append([ criterion, f"{sim_pos:.4f}"'))
print(f"Imagen convertida a NumPy. Shape: {img_np.shape}, Tipo: {img_np.dtype}")
if sim_pos is not None else "N/A",
f"{sim_neg:. # 2. Generar Embedding de Imagen
print("Generando embedding4f}" if sim_neg is not None else "N/A", de imagen...")
image_embedding = generate_image_embedding(img f"{diff:.4f}" if diff is not None else "N/_np, elixrc_infer_global, qformer_infer_A", comp, simp ])
if comp == "PASS": passed_count += 1
global)
print("Embedding de imagen generado.")
# 3 df_results = pd.DataFrame(output_data, columns=[ "Criterion", "Sim (+)", "Sim (-)", "Difference", "Assessment (Comp)", "Assessment (Simp)" ])
overall_quality = "Error"; pass_. Calcular Similitudes y Clasificar
print("Calculando similitudesrate = 0
if total_count > 0:
y clasificando criterios...")
detailed_results = calculate_similarities_and_classify(pass_rate = passed_count / total_count
if pass_image_embedding, bert_preprocessor_global, qformer_infer_rate >= 0.85: overall_quality = "Excellent"
elif pass_rate >= global)
print("Clasificación completada.")
# 0.70: overall_quality = "Good"
elif pass4. Formatear Resultados para Gradio
output_data = []
passed_count = _rate >= 0.50: overall_quality = "Fair"0
total_count = 0
for criterion, details in detailed_results.items
else: overall_quality = "Poor"
quality_label():
total_count += 1
sim_pos = details['similarity_positive']
sim_neg = details['similarity_negative = f"{overall_quality} ({passed_count}/{total_count}']
diff = details['difference']
comp = details['classification passed)"
end_process_time = time.time()
print(f"--- Evaluación completada en {end_process_time - start_process_time:.2f} seg_comparative']
simp = details['classification_simplified']
---")
# Devolver resultados y actualizar visibilidad
return (
output_data.append([
criterion,
f"{sim_pos:.4f}"gr.update(visible=False), # Oculta bienvenida
gr.update(visible=True), # Muestra resultados
image_pil, # Muestra imagen if sim_pos is not None else "N/A",
f procesada
gr.update(value=quality_label), # Actualiza etiqueta
df_results, # Actualiza dataframe
detailed"{sim_neg:.4f}" if sim_neg is not None else_results # Actualiza JSON
)
except Exception as e "N/A",
f"{diff:.4f}" if diff:
print(f"Error durante procesamiento Gradio: {e}"); is not None else "N/A",
comp,
simp
])
traceback.print_exc()
raise gr.Error(f"Error procesando imagen: {str if comp == "PASS":
passed_count += 1
(e)}")
# --- Función para Resetear la UI ---
def reset_ui # Crear DataFrame
df_results = pd.DataFrame(output_data, columns():
print("Reseteando UI...")
return (
gr.update(visible==[
"Criterion", "Sim (+)", "Sim (-)", "Difference", "Assessment (CompTrue), # Muestra bienvenida
gr.update(visible=False), # Oculta resultados
None, # Borra imagen de)", "Assessment (Simp)"
])
# Calcular etiqueta de calidad general
overall_quality entrada
None, # Borra imagen de salida
gr.update(value="N/A"), # Borra etiqueta
pd = "Error"
pass_rate = 0
if total_count > 0:
.DataFrame(), # Borra dataframe
None # Borra JSON
)
# --- Definir Tema Oscuro Personalizado ---
# Inspirado en los colores del HTML original y pass_rate = passed_count / total_count
if pass Tailwind dark grays/blues
dark_theme = gr.themes.Default_rate >= 0.85: overall_quality = "Excellent"
elif pass_rate >=(
primary_hue=gr.themes.colors.blue, # Azul como color primario
secondary_hue=gr.themes.colors.blue, 0.70: overall_quality = "Good"
elif # Azul secundario
neutral_hue=gr.themes.colors pass_rate >= 0.50: overall_quality = "Fair.gray, # Gris neutro
font=[gr.themes.GoogleFont("Inter"
else: overall_quality = "Poor"
quality_"), "ui-sans-serif", "system-ui", "sans-label = f"{overall_quality} ({passed_count}/{total_countserif"],
font_mono=[gr.themes.GoogleFont("Jet} passed)"
end_process_time = time.time()
print(f"---Brains Mono"), "ui-monospace", "Consolas", "monospace"],
Evaluación completada en {end_process_time - start_process_time:.2f} segundos ---).set(
# Fondos
body_background_fill="#111827", # Fondo principal muy oscuro (gray-900)
background_fill_primary="#1f2937",")
# Devolver resultados y actualizar visibilidad
return (
# Fondo de componentes (gray-800)
background_fill_secondary="#3gr.update(visible=False), # Oculta bienvenida
gr.update(visible=74151", # Fondo secundario (gray-700)
block_background_fill="#1f2937", True), # Muestra resultados
image_pil, # Muestra imagen# Fondo de bloques (gray-800)
# Texto
procesada
gr.update(value=quality_label), # Actualiza etiqueta
df body_text_color="#d1d5db", # Texto_results, # Actualiza dataframe
detailed_results # Actualiza JSON
)
except Exception as e:
print(f"Error durante principal claro (gray-300)
# text_color_subdued="# procesamiento Gradio: {e}")
traceback.print_exc()
9ca3af", # <-- LÍNEA PROBLEMÁTICA EL# Lanzar un gr.Error para mostrarlo en la UI de Gradio
raise gr.Error(f"Error procesando imagen: {str(e)}")
# --- Función para ResetearIMINADA
block_label_text_color="#d1d5db", # Etiquetas de bloque (gray-300)
block_title_text la UI ---
def reset_ui():
print("Reseteando UI...")
return (
gr.update(visible=True), # Muestra bienvenida
_color="#ffffff", # Títulos de bloque (blanco)
gr.update(visible=False), # Oculta resultados
# Bordes
border_color_accent="#374151",None, # Borra imagen de entrada
None, # Bor # Borde (gray-700)
border_colorra imagen de salida
gr.update(value="N/A"), # Borra etiqueta
_primary="#4b5563", # Borde primario (gray-pd.DataFrame(), # Borra dataframe
None # Borra JSON
)
600)
# Botones y Elementos Interactivos
# --- Definir Tema Oscuro Personalizado (CORREGIDO) ---
#button_primary_background_fill="*primary_600", # Usa color primario (azul)
button_primary_text_color="#ffffff",
Inspirado en los colores del HTML original y Tailwind dark grays/blues
dark_button_secondary_background_fill="*neutral_700",
button_secondary_text_color="#ffffff",
input_background_fill="#3theme = gr.themes.Default(
primary_hue=gr.74151", # Fondo de inputs (gray-700)
input_borderthemes.colors.blue, # Azul como color primario
secondary_hue=gr.themes.colors.blue, # Azul secundario
neutral_hue=gr_color="#4b5563", # Borde de inputs (gray-.themes.colors.gray, # Gris neutro
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans600)
input_text_color="#ffffff", # Texto en inputs
# Sombras y Radios
shadow_drop="rgba(0,0,0,0-serif"],
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "ui.2) 0px 2px 4px",
block-monospace", "Consolas", "monospace"],
).set(
_shadow="rgba(0,0,0,0.2) # Fondos
body_background_fill="#111827", 0px 2px 5px",
radius_size="*# Fondo principal muy oscuro (gray-900)
background_fill_primaryradius_lg", # Bordes redondeados
)
# --- Definir la Interfaz Gradio con="#1f2937", # Fondo de componentes (gray-800)
Bloques y Tema ---
with gr.Blocks(theme=dark_theme, title="CXR Quality Assessment") as demo:
# --- Cabecera ---
with gr.Row():
gr.Markdown(
"""
# <span style="color: # background_fill_secondary="#374151", #e5e7eb;">CXR Quality Assessment</span>
<p style Fondo secundario (gray-700)
block_background_="color: #9ca3af;">Evaluate chest X-ray technical quality usingfill="#1f2937", # Fondo de bloques (gray-8 AI (ELIXR family)</p>
""",
elem_id="app-header00)
# Texto
body_text_color="#d1d5db", #"
)
# --- Contenido Principal (Dos Columnas) ---
with gr Texto principal claro (gray-300)
# text_color_subdued.Row(equal_height=False): # Permitir alturas diferentes
# --- Columna Iz="#9ca3af", # <--- ESTA LÍNEA CAUSABA EL ERROR Y FUE ELIMINADA/COMENTADA
block_label_text_color="#d1d5db", # Etiquetas de bloque (gray-300quierda (Carga) ---
with gr.Column(scale=1,)
block_title_text_color="#ffffff", # T min_width=350):
gr.Markdown("### ítulos de bloque (blanco)
# Bordes
border_1. Upload Image", elem_id="upload-title")
inputcolor_accent="#374151", # Borde (gray-70_image = gr.Image(type="pil", label="Upload Chest X-ray", height=300)
border_color_primary="#4b55630) # Altura fija para imagen entrada
with gr.Row():
", # Borde primario (gray-600)
analyze_btn = gr.Button("Analyze Image", variant="primary", scale=2)
reset_btn = gr.Button("Reset", variant="secondary", scale=1)
## Botones y Elementos Interactivos
button_primary_background_fill="*primary_600", # Usa color primario (azul)
button_primary_ Añadir ejemplos si tienes imágenes de ejemplo
# gr.Examples(
text_color="#ffffff",
button_secondary_background_fill="*neutral_700",# examples=[os.path.join("examples", "sample_cx
button_secondary_text_color="#ffffff",
input_background_fill="#3r.png")],
# inputs=input_image, label="Example CXR"
# )
gr.Markdown(
74151", # Fondo de inputs (gray-700)
input_border_color="#4b5563", # Borde de inputs (gray-"<p style='color:#9ca3af; font-size:0600)
input_text_color="#ffffff", #.9em;'>Model loading on startup takes ~1 min. Analysis takes ~15-40 sec.</p>"
)
# --- Columna Derecha (Bienvenida / Resultados) ---
Texto en inputs
# Sombras y Radios
shadow_dropwith gr.Column(scale=2):
# --- Bloque de Bienvenida (Visible Inicialmente="rgba(0,0,0,0.2) 0px) ---
with gr.Column(visible=True, elem_id 2px 4px",
block_shadow="rgba(0,0="welcome-section") as welcome_block:
gr.Markdown(,0,0.2) 0px 2px 5px",
radius_size="*radius_lg", # Bordes redondeados
)
"""
### Welcome!
Upload a chest X-ray image (# --- Definir la Interfaz Gradio con Bloques y Tema ---
with gr.Blocks(themePNG, JPG, etc.) on the left panel and click "Analyze Image".=dark_theme, title="CXR Quality Assessment") as demo:
The system will evaluate its technical quality based on 7 standard criteria using the ELIXR model family. # --- Cabecera ---
with gr.Row():
gr.Markdown
The results will appear here once the analysis is complete.
""",(
"""
# <span style="color: #e5e7eb;">CXR elem_id="welcome-text"
)
# --- Blo Quality Assessment</span>
<p style="color: #9ca3af;">Evaluate chest X-ray technical quality using AI (ELIXR family)</p>
que de Resultados (Oculto Inicialmente) ---
with gr.""", # Usar blanco/gris claro para texto cabecera
elem_id="app-header"
)
# --- Contenido Principal (DosColumn(visible=False, elem_id="results-section") as results Columnas) ---
with gr.Row(equal_height=False): # Permitir alturas diferentes
# --- Columna Izquierda (Carga) ---
with gr.Column(scale=1, min_width=_block:
gr.Markdown("### 2. Quality Assessment Results350):
gr.Markdown("### 1. Upload Image", elem_id="results-title")
with gr.Row(): # Fila para imagen de salida", elem_id="upload-title")
input_image = gr.Image(type y resumen
with gr.Column(scale=1):
output_image = gr.Image(type="pil", label="Analyzed Image", interactive=False)
with gr.Column(scale="pil", label="Upload Chest X-ray", height=300) # Altura fija para imagen entrada
with gr.Row():
analyze_btn = gr=1):
gr.Markdown("#### Summary", elem_id=".Button("Analyze Image", variant="primary", scale=2)
reset_btn = gr.Button("Reset", variant="secondary", scale=1)
#summary-title")
output_label = gr.Label(value="N/A", label="Overall Quality Estimate", elem_id="quality-label")
gr.Markdown Añadir ejemplos si tienes imágenes de ejemplo
# gr.Examples(
("#### Detailed Criteria Evaluation", elem_id="detailed-title")
output # examples=[os.path.join("examples", "sample__dataframe = gr.DataFrame(
headers=["Criterion", "Sim (+cxr.png")],
# inputs=input_image, label)", "Sim (-)", "Difference", "Assessment (Comp)", "Assessment (Simp)"],
label=None, # Quitar etiqueta redundante
wrap=True,
max="Example CXR"
# )
gr.Markdown(
"<p style='color:#9ca3af; font-size:0.9_rows=10, # Limitar filas visibles con scroll
overflow_row_behaviour="show_ends", # Muestra inicio/fin al hacer scroll
em;'>Model loading on startup takes ~1 min. Analysis takes ~15-4interactive=False, # No editable
elem_id="results-dataframe"
)
0 sec.</p>"
)
# --- Columna Derecha (Bienvenida / Resultados) ---
with gr.Column(scale=2): with gr.Accordion("Raw JSON Output (for debugging)", open=False
# --- Bloque de Bienvenida (Visible Inicialmente) ---
with gr.Column(visible=True, elem_id="welcome-section") as welcome_block:
gr.Markdown):
output_json = gr.JSON(label=None)
gr.Markdown(
f"""
#### Technical Notes
* **Criterion:** Quality(
"""
### Welcome!
Upload a chest X-ray image ( aspect evaluated.
* **Sim (+/-):** Cosine similarity with positive/negative prompt.
* **Difference:** Sim (+) - Sim (-).
*PNG, JPG, etc.) on the left panel and click "Analyze Image". **Assessment (Comp):** PASS if Difference > {SIMILARITY_DI
The system will evaluate its technical quality based on 7 standard criteria using the ELIXR model family.FFERENCE_THRESHOLD}. (Main Result)
* **Assessment (
The results will appear here once the analysis is complete.
""",Simp):** PASS if Sim (+) > {POSITIVE_SIMILARITY_THRESHOLD}.
""", elem_id="notes-text"
)
# --- Pie de página ---
gr.Markdown(
"""
elem_id="welcome-text"
)
# Podrías añadir un icono o----
<p style='text-align:center; color:#9 imagen aquí si quieres
# gr.Image("path/to/welcome_icon.pngca3af; font-size:0.8em;'>
C", interactive=False, show_label=False, show_download_button=FalseXR Quality Assessment Tool | Model: google/cxr-foundation | Interface: Gradio
</p>
""", elem_id="app-footer"
))
# --- Bloque de Resultados (Oculto Inicialmente) ---
with gr.
# --- Conexiones de Eventos ---
analyze_btn.click(
fnColumn(visible=False, elem_id="results-section") as results=assess_quality_and_update_ui,
inputs=[input_block:
gr.Markdown("### 2. Quality Assessment Results", elem_id="results_image],
outputs=[
welcome_block, # ->-title")
with gr.Row(): # Fila para imagen de salida actualiza visibilidad bienvenida
results_block, # -> actualiza visibilidad resultados
y resumen
with gr.Column(scale=1):
outputoutput_image, # -> muestra imagen analizada
output_label, # -> actualiza etiqueta resumen
output_dataframe, # -> actualiza tabla
output_image = gr.Image(type="pil", label="Analyzed Image_json # -> actualiza JSON
]
)
reset_btn.click(
fn=reset_ui,
inputs=None,", interactive=False)
with gr.Column(scale=1):
gr.Markdown("#### # No necesita inputs
outputs=[
welcome_block,
Summary", elem_id="summary-title")
output_label = gr.Label(valueresults_block,
input_image, # -> limpia imagen entrada="N/A", label="Overall Quality Estimate", elem_id="quality
output_image,
output_label,
output_dataframe,
output_json
]
)
# ----label")
# Podríamos añadir más texto de resumen aquí si quisiéramos
Iniciar la Aplicación Gradio ---
if __name__ == "__main__":
gr.Markdown("#### Detailed Criteria Evaluation", elem_id="detailed-title # server_name="0.0.0.0" para accesibilidad en red local
# server_port=7860 es el puerto estándar de HF")
output_dataframe = gr.DataFrame(
headers=["Criterion", "Sim (+)", "Sim (-)", "Difference", "Assessment (Comp)", "Assessment (Simp)"],
label=None, # Quitar etiqueta redundante
wrap=True,
# La altura ahora se maneja mejor automáticamente o con CSS
# row_count=(7, "dynamic Spaces
demo.launch(server_name="0.0.0") # Mostrar 7 filas, permitir scroll si hay más
max_rows=10, # Lim.0", server_port=7860)