|
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 |
|
|
|
|
|
MODEL_REPO_ID = "google/cxr-foundation" |
|
MODEL_DOWNLOAD_DIR = './hf_cxr_foundation_space' |
|
|
|
SIMILARITY_DIFFERENCE_THRESHOLD = 0.1 |
|
POSITIVE_SIMILARITY_THRESHOLD = 0.1 |
|
print(f"Usando umbrales: Comp Δ={SIMILARITY_DIFFERENCE_THRESHOLD}, Simp τ={POSITIVE_SIMILARITY_THRESHOLD}") |
|
|
|
|
|
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" |
|
] |
|
|
|
|
|
|
|
def preprocess_text(text): |
|
"""Función interna del preprocesador BERT.""" |
|
return bert_preprocessor_global(text) |
|
|
|
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) |
|
|
|
|
|
out¡ = preprocessor(tf.constant([text.lower()])) |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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, |
|
|
|
|
|
from sklearn.metrics.pairwise import cosine_similarity |
|
import1, 1, 128) |
|
|
|
import time |
|
import pandas as pd |
|
|
|
|
|
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) |
|
|
|
|
|
expected_shape = (1 |
|
SIMILARITY_DIFFERENCE_THRESHOLD = , 1, 128) |
|
if ids.shape != expected_shape: |
|
|
|
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}") |
|
|
|
|
|
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" |
|
] |
|
|
|
|
|
"""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) |
|
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']. |
|
if max_val <= min_val:numpy().astype(np.float32) |
|
paddings = |
|
|
|
1.0 - masks |
|
end_token_idx = (ids == 102) |
|
|
|
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: |
|
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 |
|
current_max = max_val - |
|
if paddings.shape == (1,128): padd min_val |
|
|
|
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: |
|
|
|
|
|
-> 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 |
|
|
|
|
|
output = io.Bytes image_array = np.squeeze(image_array, axis=2IO() |
|
png.Writer( |
|
width=pixel_array.) |
|
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 |
|
example = tf.train.Example() |
|
, max_val = image.min(), image.max() |
|
|
|
if features = example.features.feature |
|
features['image/encoded']. max_val <= min_val: |
|
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: |
|
|
|
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: |
|
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}") |
|
|
|
|
|
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(), |
|
'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(), |
|
} |
|
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") |
|
|
|
print("Cargando Preprocesador BERT...") |
|
bert_preprocess |
|
|
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
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.") |
|
|
|
|
|
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() |
|
|
|
|
|
print("Modelo Gradio --- |
|
def assess_quality_and_update_ui(image ELIXR-C cargado.") |
|
|
|
|
|
"""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), |
|
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"), |
|
pdmodels_loaded = True |
|
end_time = time.time() |
|
.DataFrame(), |
|
None |
|
) |
|
|
|
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}") |
|
|
|
"""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.") |
|
|
|
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.") |
|
|
|
gr.update(visible=False), |
|
4. Formatear Resultados |
|
output_data, passed_count,None, |
|
gr.update(value="N/A total_count = [], 0, 0 |
|
for criterion, details in detailed_results.items"), |
|
pd.DataFrame(), |
|
total_count += 1 |
|
sim_pos = details |
|
None |
|
) |
|
|
|
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'] |
|
---") |
|
|
|
return ( |
|
output_data.append([ |
|
criterion, |
|
f"{sim_pos:.4f}"gr.update(visible=False), |
|
gr.update(visible=True), |
|
image_pil, |
|
f procesada |
|
gr.update(value=quality_label), |
|
df_results, |
|
detailed"{sim_neg:.4f}" if sim_neg is not None else_results |
|
) |
|
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)}") |
|
|
|
|
|
def reset_ui |
|
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)" |
|
]) |
|
|
|
|
|
overall_quality entrada |
|
None, |
|
gr.update(value="N/A"), |
|
pd = "Error" |
|
pass_rate = 0 |
|
if total_count > 0: |
|
.DataFrame(), |
|
None |
|
) |
|
|
|
|
|
|
|
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, |
|
secondary_hue=gr.themes.colors.blue, 0.70: overall_quality = "Good" |
|
elif |
|
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=" |
|
background_fill_primary="#1f2937",") |
|
|
|
# Devolver resultados y actualizar visibilidad |
|
return ( |
|
# Fondo de componentes (gray-800) |
|
background_fill_secondary=" |
|
gr.update(visible=74151", # Fondo secundario (gray-700) |
|
block_background_fill=" |
|
image_pil, |
|
|
|
|
|
procesada |
|
gr.update(value=quality_label), |
|
df body_text_color="#d1d5db", |
|
detailed_results |
|
) |
|
except Exception as e: |
|
print(f"Error durante principal claro (gray-300) |
|
# text_color_subdued=" |
|
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=" |
|
block_title_text la UI --- |
|
def reset_ui(): |
|
print("Reseteando UI...") |
|
return ( |
|
gr.update(visible=True), |
|
_color="#ffffff", |
|
|
|
gr.update(visible=False), |
|
|
|
border_color_accent="#374151",None, |
|
None, |
|
border_colorra imagen de salida |
|
gr.update(value="N/A"), |
|
_primary="#4b5563", |
|
None |
|
) |
|
|
|
600) |
|
|
|
|
|
|
|
|
|
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", |
|
input_borderthemes.colors.blue, |
|
secondary_hue=gr.themes.colors.blue, |
|
neutral_hue=gr_color="#4b5563", |
|
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans600) |
|
input_text_color=" |
|
|
|
|
|
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) |
|
body_background_fill="#111827", 0px 2px 5px", |
|
radius_size="* |
|
background_fill_primaryradius_lg", # Bordes redondeados |
|
) |
|
|
|
|
|
# --- Definir la Interfaz Gradio con=" |
|
Bloques y Tema --- |
|
with gr.Blocks(theme=dark_theme, title="CXR Quality Assessment") as demo: |
|
|
|
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=" |
|
) |
|
|
|
|
|
with gr Texto principal claro (gray-300) |
|
|
|
|
|
|
|
block_label_text_color="#d1d5db", |
|
with gr.Column(scale=1,) |
|
block_title_text_color="#ffffff", |
|
gr.Markdown("### ítulos de bloque (blanco) |
|
|
|
# Bordes |
|
border_1. Upload Image", elem_id="upload-title") |
|
inputcolor_accent="#374151", |
|
border_color_primary="#4b55630) # Altura fija para imagen entrada |
|
with gr.Row(): |
|
", |
|
|
|
analyze_btn = gr.Button("Analyze Image", variant="primary", scale=2) |
|
reset_btn = gr.Button("Reset", variant="secondary", scale=1) |
|
|
|
button_primary_background_fill="*primary_600", |
|
button_primary_ Añadir ejemplos si tienes imágenes de ejemplo |
|
|
|
text_color="#ffffff", |
|
button_secondary_background_fill="*neutral_700", |
|
button_secondary_text_color="#ffffff", |
|
input_background_fill="#3r.png")], |
|
|
|
|
|
gr.Markdown( |
|
74151", # Fondo de inputs (gray-700) |
|
input_border_color=" |
|
input_text_color="#ffffff", |
|
) |
|
|
|
|
|
|
|
Texto en inputs |
|
|
|
|
|
shadow_dropwith gr.Column(scale=2): |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
|
|
""" |
|
|
|
Upload a chest X-ray image ( |
|
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. |
|
with gr.Row(): |
|
gr.Markdown |
|
The results will appear here once the analysis is complete. |
|
""",( |
|
""" |
|
|
|
) |
|
|
|
|
|
|
|
<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""" |
|
|
|
* **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" |
|
) |
|
|
|
<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" |
|
)) |
|
|
|
|
|
|
|
with gr. |
|
|
|
|
|
|
|
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(): |
|
results_block, |
|
y resumen |
|
with gr.Column(scale=1): |
|
outputoutput_image, |
|
output_label, |
|
output_dataframe, |
|
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, |
|
output_image, |
|
output_label, |
|
output_dataframe, |
|
output_json |
|
] |
|
) |
|
|
|
|
|
|
|
|
|
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, |
|
wrap=True, |
|
|
|
|
|
demo.launch(server_name="0.0.0") |
|
max_rows=10, |