""" app.py — Gradio Space para HuggingFace. Solo UI: llama via HTTP al api_server.py que corre en el droplet AMD. """ import os import base64 import io import requests import gradio as gr from PIL import Image API_URL = os.environ.get("API_URL", "http://localhost:7860") VARIABLES = [ "Optical_Depth_Land_And_Ocean", "Image_Optical_Depth_Land_And_Ocean", "Angstrom_Exponent_1_Ocean", "Mass_Concentration_Land", ] def run_download(concept_id, bbox_w, bbox_s, bbox_e, bbox_n, date_start, date_end, n_granules, hdf_dir): try: r = requests.post(f"{API_URL}/download", json={ "concept_id": concept_id, "bbox_west": bbox_w, "bbox_south": bbox_s, "bbox_east": bbox_e, "bbox_north": bbox_n, "date_start": date_start, "date_end": date_end, "n_granules": int(n_granules), "hdf_dir": hdf_dir, }, timeout=300) return r.json().get("log", "Sin respuesta") except Exception as e: return f"Error: {e}" def run_pipeline(hdf_dir, output_dir, plots_dir, db_url, variable): try: r = requests.post(f"{API_URL}/pipeline", json={ "hdf_dir": hdf_dir, "output_dir": output_dir, "plots_dir": plots_dir, "db_url": db_url, "variable": variable, }, timeout=600) data = r.json() images = [] for img in data.get("images", []): raw = base64.b64decode(img["b64"]) images.append(Image.open(io.BytesIO(raw))) status = ( f"Pipeline completado\n" f"Filas cargadas: {data.get('rows_loaded', 0):,}\n" f"Graficas: {data.get('plots_count', 0)}" ) return status, images except Exception as e: return f"Error: {e}", [] def run_full(concept_id, bbox_w, bbox_s, bbox_e, bbox_n, date_start, date_end, n_granules, hdf_dir, output_dir, plots_dir, db_url, variable): dl_log = run_download(concept_id, bbox_w, bbox_s, bbox_e, bbox_n, date_start, date_end, n_granules, hdf_dir) status, images = run_pipeline(hdf_dir, output_dir, plots_dir, db_url, variable) return dl_log + "\n\n" + status, images with gr.Blocks(title="AMD MI300X Aerosol Pipeline") as demo: gr.Markdown("# AMD MI300X Aerosol Pipeline Demo\nDescarga datos MODIS y genera visualizaciones de aerosoles con agentes LangGraph en AMD MI300X.") with gr.Tabs(): with gr.TabItem("1 Descarga"): with gr.Row(): ci1 = gr.Textbox(label="Concept ID", value="C1443528505-LAADS") ng1 = gr.Slider(1, 20, value=5, step=1, label="N granules") with gr.Row(): bw1 = gr.Number(label="Lon W", value=-10) bs1 = gr.Number(label="Lat S", value=20) be1 = gr.Number(label="Lon E", value=10) bn1 = gr.Number(label="Lat N", value=50) with gr.Row(): ds1 = gr.Textbox(label="Fecha inicio", value="2003-07-04") de1 = gr.Textbox(label="Fecha fin", value="2003-07-05") hd1 = gr.Textbox(label="HDF dir", value="/tmp/earthdata") btn1 = gr.Button("Descargar", variant="primary") out1 = gr.Textbox(label="Log", lines=10) btn1.click(run_download, [ci1,bw1,bs1,be1,bn1,ds1,de1,ng1,hd1], out1) with gr.TabItem("2 Pipeline"): hd2 = gr.Textbox(label="HDF dir", value="/tmp/earthdata") od2 = gr.Textbox(label="CSV dir", value="/tmp/aerosol_csv") pd2 = gr.Textbox(label="Plots dir", value="/tmp/aerosol_plots") db2 = gr.Textbox(label="DB URL", value="", type="password") va2 = gr.Dropdown(VARIABLES, label="Variable", value=VARIABLES[0]) btn2 = gr.Button("Ejecutar pipeline", variant="primary") st2 = gr.Textbox(label="Estado", lines=5) gl2 = gr.Gallery(label="Graficas", columns=3, height=500) btn2.click(run_pipeline, [hd2,od2,pd2,db2,va2], [st2,gl2]) with gr.TabItem("3 Flujo completo"): with gr.Row(): ci3 = gr.Textbox(label="Concept ID", value="C1443528505-LAADS") ng3 = gr.Slider(1, 20, value=5, step=1, label="N granules") with gr.Row(): bw3 = gr.Number(label="Lon W", value=-10) bs3 = gr.Number(label="Lat S", value=20) be3 = gr.Number(label="Lon E", value=10) bn3 = gr.Number(label="Lat N", value=50) with gr.Row(): ds3 = gr.Textbox(label="Fecha inicio", value="2003-07-04") de3 = gr.Textbox(label="Fecha fin", value="2003-07-05") hd3 = gr.Textbox(label="HDF dir", value="/tmp/earthdata") od3 = gr.Textbox(label="CSV dir", value="/tmp/aerosol_csv") pd3 = gr.Textbox(label="Plots dir", value="/tmp/aerosol_plots") db3 = gr.Textbox(label="DB URL", value="", type="password") va3 = gr.Dropdown(VARIABLES, label="Variable", value=VARIABLES[0]) btn3 = gr.Button("Ejecutar todo", variant="primary") lg3 = gr.Textbox(label="Log", lines=12) im3 = gr.Gallery(label="Graficas", columns=3, height=500) btn3.click(run_full, [ci3,bw3,bs3,be3,bn3,ds3,de3,ng3,hd3,od3,pd3,db3,va3], [lg3,im3]) gr.Markdown("---\nPowered by AMD MI300X | vLLM | LangGraph | NASA EarthData | MODIS MYD04_3K") if __name__ == "__main__": demo.launch()