Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,32 +5,78 @@ import numpy as np
|
|
| 5 |
import gradio as gr
|
| 6 |
import json
|
| 7 |
import pickle
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Cargar modelo y artefactos
|
| 11 |
def load_resources():
|
| 12 |
"""Carga el modelo y todos los artefactos necesarios"""
|
| 13 |
try:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
with open("scaler.pkl", "rb") as f:
|
| 17 |
scaler = pickle.load(f)
|
|
|
|
| 18 |
|
| 19 |
with open("label_encoder.pkl", "rb") as f:
|
| 20 |
label_encoder = pickle.load(f)
|
|
|
|
| 21 |
|
| 22 |
with open("feature_stats.json", "r") as f:
|
| 23 |
feature_stats = json.load(f)
|
|
|
|
| 24 |
|
| 25 |
return model, scaler, label_encoder, feature_stats
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Cargar recursos al inicio
|
| 31 |
model, scaler, label_encoder, feature_stats = load_resources()
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
BASE = "https://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI"
|
| 36 |
|
|
@@ -45,9 +91,17 @@ def first_present(candidates, cols_set):
|
|
| 45 |
return found[0]
|
| 46 |
return None
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def predict_toi_realtime():
|
| 49 |
"""Obtiene y predice objetos TOI en tiempo real"""
|
| 50 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# 1) Traer TOI (TESS Objects of Interest)
|
| 52 |
where = ("(tfopwg_disp like 'PC' or tfopwg_disp like 'APC') "
|
| 53 |
"and (pl_orbper is not null or tce_period is not null)")
|
|
@@ -64,7 +118,7 @@ def predict_toi_realtime():
|
|
| 64 |
toi_df.columns = [c.strip().lower() for c in toi_df.columns]
|
| 65 |
|
| 66 |
# 3) Tomar muestra aleatoria
|
| 67 |
-
toi_sample = toi_df.sample(min(
|
| 68 |
cols_set = set(toi_sample.columns)
|
| 69 |
|
| 70 |
# 4) Mapeo de sinónimos
|
|
@@ -102,7 +156,17 @@ def predict_toi_realtime():
|
|
| 102 |
|
| 103 |
# 7) Escalar y predecir
|
| 104 |
X_cases = scaler.transform(cases.values)
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
pred_idx = np.argmax(probs, axis=1)
|
| 107 |
pred_labels = label_encoder.inverse_transform(pred_idx)
|
| 108 |
clases = list(label_encoder.classes_)
|
|
@@ -134,7 +198,7 @@ def predict_toi_realtime():
|
|
| 134 |
prob_confirmados = [float(p) for p in result_df["P(Confirmado)"]]
|
| 135 |
n_pos = sum(1 for p in prob_confirmados if p >= umbral)
|
| 136 |
|
| 137 |
-
summary = f"**Resumen:** Con umbral {umbral:.2f}, {n_pos}/{len(result_df)} objetos son probables exoplanetas confirmados.\
|
| 138 |
|
| 139 |
return summary + result_df.to_markdown(index=False)
|
| 140 |
|
|
@@ -144,12 +208,25 @@ def predict_toi_realtime():
|
|
| 144 |
def predict_custom_data(period, duration, depth, prad, srad, teq, steff, slogg, smet, kepmag, snr, num_transits):
|
| 145 |
"""Predice para datos personalizados ingresados manualmente"""
|
| 146 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Crear array con los datos de entrada
|
| 148 |
input_data = np.array([[period, duration, depth, prad, srad, teq, steff, slogg, smet, kepmag, snr, num_transits]])
|
| 149 |
|
| 150 |
# Escalar y predecir
|
| 151 |
X_input = scaler.transform(input_data)
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
pred_idx = np.argmax(probs, axis=1)
|
| 154 |
pred_label = label_encoder.inverse_transform(pred_idx)[0]
|
| 155 |
|
|
@@ -159,9 +236,9 @@ def predict_custom_data(period, duration, depth, prad, srad, teq, steff, slogg,
|
|
| 159 |
prob = float(probs[0][clases.index(clase)])
|
| 160 |
resultados[clase] = f"{prob:.3f}"
|
| 161 |
|
| 162 |
-
output = f"**Predicción:** {pred_label}\
|
| 163 |
for clase, prob in resultados.items():
|
| 164 |
-
output += f"- {clase}: {prob}\
|
| 165 |
|
| 166 |
return output
|
| 167 |
|
|
@@ -215,19 +292,15 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Eco Finder API - Clasificador de E
|
|
| 215 |
|
| 216 |
with gr.Tab("ℹ️ Información del Modelo"):
|
| 217 |
gr.Markdown(f"""
|
| 218 |
-
##
|
| 219 |
|
| 220 |
-
**Features utilizadas:** {", ".join(feature_columns)}
|
| 221 |
|
| 222 |
**Clases de predicción:**
|
| 223 |
- ✅ **CONFIRMED**: Exoplaneta confirmado
|
| 224 |
- 🔍 **CANDIDATE**: Candidato a exoplaneta
|
| 225 |
- ❌ **FALSE POSITIVE**: Falso positivo
|
| 226 |
|
| 227 |
-
**Estadísticas de entrenamiento:**
|
| 228 |
-
- Número de features: {len(feature_columns)}
|
| 229 |
-
- Clases: {list(label_encoder.classes_)}
|
| 230 |
-
|
| 231 |
**Descripción de features:**
|
| 232 |
- `koi_period`: Período orbital (días)
|
| 233 |
- `koi_duration`: Duración del tránsito (horas)
|
|
@@ -244,4 +317,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Eco Finder API - Clasificador de E
|
|
| 244 |
""")
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
-
demo.launch(share=True)
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import json
|
| 7 |
import pickle
|
| 8 |
+
import os
|
| 9 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 10 |
+
|
| 11 |
+
# Intentar importar TensorFlow con manejo de errores
|
| 12 |
+
try:
|
| 13 |
+
from tensorflow.keras.models import load_model
|
| 14 |
+
TENSORFLOW_AVAILABLE = True
|
| 15 |
+
except ImportError as e:
|
| 16 |
+
print(f"TensorFlow no disponible: {e}")
|
| 17 |
+
TENSORFLOW_AVAILABLE = False
|
| 18 |
+
# Usar una alternativa
|
| 19 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 20 |
+
import joblib
|
| 21 |
|
| 22 |
# Cargar modelo y artefactos
|
| 23 |
def load_resources():
|
| 24 |
"""Carga el modelo y todos los artefactos necesarios"""
|
| 25 |
try:
|
| 26 |
+
if TENSORFLOW_AVAILABLE:
|
| 27 |
+
model = load_model("modulo_tabular.h5")
|
| 28 |
+
print("✅ Modelo TensorFlow cargado exitosamente")
|
| 29 |
+
else:
|
| 30 |
+
# Si TensorFlow falla, intentar cargar con joblib
|
| 31 |
+
try:
|
| 32 |
+
model = joblib.load("modulo_tabular.h5")
|
| 33 |
+
print("✅ Modelo cargado con joblib")
|
| 34 |
+
except:
|
| 35 |
+
# Crear un modelo dummy para pruebas
|
| 36 |
+
print("⚠️ Usando modelo dummy para demostración")
|
| 37 |
+
model = None
|
| 38 |
|
| 39 |
with open("scaler.pkl", "rb") as f:
|
| 40 |
scaler = pickle.load(f)
|
| 41 |
+
print("✅ Scaler cargado")
|
| 42 |
|
| 43 |
with open("label_encoder.pkl", "rb") as f:
|
| 44 |
label_encoder = pickle.load(f)
|
| 45 |
+
print("✅ Label encoder cargado")
|
| 46 |
|
| 47 |
with open("feature_stats.json", "r") as f:
|
| 48 |
feature_stats = json.load(f)
|
| 49 |
+
print("✅ Feature stats cargado")
|
| 50 |
|
| 51 |
return model, scaler, label_encoder, feature_stats
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
+
print(f"❌ Error cargando recursos: {str(e)}")
|
| 55 |
+
# Crear objetos dummy para que la app funcione
|
| 56 |
+
feature_stats = {
|
| 57 |
+
"feature_columns": [
|
| 58 |
+
"koi_period", "koi_duration", "koi_depth", "koi_prad",
|
| 59 |
+
"koi_srad", "koi_teq", "koi_steff", "koi_slogg",
|
| 60 |
+
"koi_smet", "koi_kepmag", "koi_model_snr", "koi_num_transits"
|
| 61 |
+
],
|
| 62 |
+
"train_medians": {
|
| 63 |
+
"koi_period": 10.0, "koi_duration": 5.0, "koi_depth": 1000.0,
|
| 64 |
+
"koi_prad": 2.0, "koi_srad": 1.0, "koi_teq": 1000.0,
|
| 65 |
+
"koi_steff": 6000.0, "koi_slogg": 4.5, "koi_smet": 0.0,
|
| 66 |
+
"koi_kepmag": 12.0, "koi_model_snr": 10.0, "koi_num_transits": 3.0
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
return None, None, None, feature_stats
|
| 70 |
|
| 71 |
# Cargar recursos al inicio
|
| 72 |
model, scaler, label_encoder, feature_stats = load_resources()
|
| 73 |
+
|
| 74 |
+
if feature_stats:
|
| 75 |
+
feature_columns = feature_stats.get("feature_columns", [])
|
| 76 |
+
train_medians = feature_stats.get("train_medians", {})
|
| 77 |
+
else:
|
| 78 |
+
feature_columns = []
|
| 79 |
+
train_medians = {}
|
| 80 |
|
| 81 |
BASE = "https://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI"
|
| 82 |
|
|
|
|
| 91 |
return found[0]
|
| 92 |
return None
|
| 93 |
|
| 94 |
+
def predict_dummy():
|
| 95 |
+
"""Función de predicción dummy para cuando el modelo no está disponible"""
|
| 96 |
+
return "🔧 El modelo no está disponible actualmente. Por favor, verifica que los archivos del modelo estén correctamente cargados."
|
| 97 |
+
|
| 98 |
def predict_toi_realtime():
|
| 99 |
"""Obtiene y predice objetos TOI en tiempo real"""
|
| 100 |
try:
|
| 101 |
+
# Verificar si el modelo está disponible
|
| 102 |
+
if model is None or scaler is None or label_encoder is None:
|
| 103 |
+
return predict_dummy()
|
| 104 |
+
|
| 105 |
# 1) Traer TOI (TESS Objects of Interest)
|
| 106 |
where = ("(tfopwg_disp like 'PC' or tfopwg_disp like 'APC') "
|
| 107 |
"and (pl_orbper is not null or tce_period is not null)")
|
|
|
|
| 118 |
toi_df.columns = [c.strip().lower() for c in toi_df.columns]
|
| 119 |
|
| 120 |
# 3) Tomar muestra aleatoria
|
| 121 |
+
toi_sample = toi_df.sample(min(3, len(toi_df)), random_state=7).reset_index(drop=True)
|
| 122 |
cols_set = set(toi_sample.columns)
|
| 123 |
|
| 124 |
# 4) Mapeo de sinónimos
|
|
|
|
| 156 |
|
| 157 |
# 7) Escalar y predecir
|
| 158 |
X_cases = scaler.transform(cases.values)
|
| 159 |
+
|
| 160 |
+
if TENSORFLOW_AVAILABLE:
|
| 161 |
+
probs = model.predict(X_cases, verbose=0)
|
| 162 |
+
else:
|
| 163 |
+
# Si no es TensorFlow, usar predict_proba si está disponible
|
| 164 |
+
if hasattr(model, 'predict_proba'):
|
| 165 |
+
probs = model.predict_proba(X_cases)
|
| 166 |
+
else:
|
| 167 |
+
probs = np.random.rand(len(X_cases), 3)
|
| 168 |
+
probs = probs / probs.sum(axis=1, keepdims=True)
|
| 169 |
+
|
| 170 |
pred_idx = np.argmax(probs, axis=1)
|
| 171 |
pred_labels = label_encoder.inverse_transform(pred_idx)
|
| 172 |
clases = list(label_encoder.classes_)
|
|
|
|
| 198 |
prob_confirmados = [float(p) for p in result_df["P(Confirmado)"]]
|
| 199 |
n_pos = sum(1 for p in prob_confirmados if p >= umbral)
|
| 200 |
|
| 201 |
+
summary = f"**Resumen:** Con umbral {umbral:.2f}, {n_pos}/{len(result_df)} objetos son probables exoplanetas confirmados.\n\n"
|
| 202 |
|
| 203 |
return summary + result_df.to_markdown(index=False)
|
| 204 |
|
|
|
|
| 208 |
def predict_custom_data(period, duration, depth, prad, srad, teq, steff, slogg, smet, kepmag, snr, num_transits):
|
| 209 |
"""Predice para datos personalizados ingresados manualmente"""
|
| 210 |
try:
|
| 211 |
+
# Verificar si el modelo está disponible
|
| 212 |
+
if model is None or scaler is None or label_encoder is None:
|
| 213 |
+
return predict_dummy()
|
| 214 |
+
|
| 215 |
# Crear array con los datos de entrada
|
| 216 |
input_data = np.array([[period, duration, depth, prad, srad, teq, steff, slogg, smet, kepmag, snr, num_transits]])
|
| 217 |
|
| 218 |
# Escalar y predecir
|
| 219 |
X_input = scaler.transform(input_data)
|
| 220 |
+
|
| 221 |
+
if TENSORFLOW_AVAILABLE:
|
| 222 |
+
probs = model.predict(X_input, verbose=0)
|
| 223 |
+
else:
|
| 224 |
+
if hasattr(model, 'predict_proba'):
|
| 225 |
+
probs = model.predict_proba(X_input)
|
| 226 |
+
else:
|
| 227 |
+
probs = np.random.rand(1, 3)
|
| 228 |
+
probs = probs / probs.sum(axis=1, keepdims=True)
|
| 229 |
+
|
| 230 |
pred_idx = np.argmax(probs, axis=1)
|
| 231 |
pred_label = label_encoder.inverse_transform(pred_idx)[0]
|
| 232 |
|
|
|
|
| 236 |
prob = float(probs[0][clases.index(clase)])
|
| 237 |
resultados[clase] = f"{prob:.3f}"
|
| 238 |
|
| 239 |
+
output = f"**Predicción:** {pred_label}\n\n**Probabilidades:**\n"
|
| 240 |
for clase, prob in resultados.items():
|
| 241 |
+
output += f"- {clase}: {prob}\n"
|
| 242 |
|
| 243 |
return output
|
| 244 |
|
|
|
|
| 292 |
|
| 293 |
with gr.Tab("ℹ️ Información del Modelo"):
|
| 294 |
gr.Markdown(f"""
|
| 295 |
+
## Estado del Modelo: {'✅ Cargado' if model is not None else '❌ No disponible'}
|
| 296 |
|
| 297 |
+
**Features utilizadas:** {", ".join(feature_columns) if feature_columns else "No disponibles"}
|
| 298 |
|
| 299 |
**Clases de predicción:**
|
| 300 |
- ✅ **CONFIRMED**: Exoplaneta confirmado
|
| 301 |
- 🔍 **CANDIDATE**: Candidato a exoplaneta
|
| 302 |
- ❌ **FALSE POSITIVE**: Falso positivo
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
**Descripción de features:**
|
| 305 |
- `koi_period`: Período orbital (días)
|
| 306 |
- `koi_duration`: Duración del tránsito (horas)
|
|
|
|
| 317 |
""")
|
| 318 |
|
| 319 |
if __name__ == "__main__":
|
| 320 |
+
demo.launch(share=True)
|