Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345619 unmodified lines2627282930313210 unmodified lines434445464748495051525354555657585960613 unmodified lines6566676869707111 unmodified lines83848586878889909115 unmodified lines107108109110111112113114115116117104 unmodified lines22222322422522622722822923023123223323415 unmodified lines250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282135 unmodified lines4184194204214224234245 unmodified lines430431432433434435436437438439440441442443444# ==============================================================================# SYSTEM: SADIM AI RADAR 77M - ADVANCED TACTICAL EXPERT INTERFACE# VERSION: 4.6.0 (BULLETPROOF: SINGLE-CLICK, FIXED UI, ESA EMERGENCY FALLBACK)# ==============================================================================import streamlit as st19 unmodified lines)# ------------------------------------------------------------------------------# 2. ADVANCED CSS STYLING (FIXED HEIGHT CONTAINER & CENTER LOADER)# ------------------------------------------------------------------------------st.markdown("""<style>10 unmodified lines/* Hide Native UI */[data-testid="stHeader"], footer { display: none !important; }/* THE ACADEMIC SEARCH BAR BLOCK - FIXED HEIGHT TO PREVENT JUMPING */div[data-testid="stHorizontalBlock"] {position: fixed !important;bottom: 30px !important;left: 30px !important;z-index: 100000 !important;background-color: transparent !important;width: 320px !important;height: 40px !important; /* Strict height locks the layout */display: flex !important;flex-direction: row !important;gap: 12px !important;align-items: center !important;}div[data-testid="column"] {3 unmodified linespadding: 0 !important;}/* Input Field - Long Sharp Rectangle */div[data-testid="stTextInput"] {margin-bottom: 0 !important;width: 200px !important;11 unmodified linesheight: 36px !important;outline: none !important;box-shadow: 4px 4px 15px rgba(0,0,0,0.8) !important;}/* Scan Button - Small Sharp Rectangle */div[data-testid="stButton"] {margin-bottom: 0 !important;width: 80px !important;15 unmodified linesdisplay: block !important;cursor: crosshair !important;box-shadow: 4px 4px 15px rgba(0,0,0,0.8) !important;}div[data-testid="stButton"] button:hover { background-color: #bbbbbb !important; }div[data-testid="stButton"] button:active { background-color: #999999 !important; border-color:#000; }/* CENTERED TACTICAL LOADER (Isolated from normal flow) */[data-testid="stSpinner"] {position: fixed !important;top: 50% !important;104 unmodified linesif not token:return {"error": "SYSTEM_FATAL: HF_TOKEN NOT FOUND IN SECRETS"}# 4.0 ESA API FETCH (The Sniping API Call)api_data = {"parallax": None, "parallax_error": None, "ruwe": None, "radial_velocity": None, "parallax_over_error": None}esa_url = "https://gea.esac.esa.int/tap-server/tap/sync"try:adql_query = f"SELECT parallax, parallax_error, ruwe, radial_velocity, parallax_over_error FROM gaiadr3.gaia_source WHERE source_id = {source_id}"payload = {"REQUEST": "doQuery", "LANG": "ADQL", "FORMAT": "json", "QUERY": adql_query}response = requests.get(esa_url, params=payload, timeout=5)if response.status_code == 200:res_json = response.json()if "data" in res_json and len(res_json["data"]) > 0:15 unmodified linesindex_query = f"SELECT filename FROM '{index_path}' WHERE source_id = {source_id} LIMIT 1"index_df = db.query(index_query).df()# 4.1.1 EMERGENCY ESA FALLBACK (If Target Not in HF DB)if index_df.empty:db.close()fb_query = f"SELECT ra, dec FROM gaiadr3.gaia_source WHERE source_id = {source_id}"try:fb_res = requests.get(esa_url, params={"REQUEST": "doQuery", "LANG": "ADQL", "FORMAT": "json", "QUERY": fb_query}, timeout=5)if fb_res.status_code == 200 and len(fb_res.json().get("data", [])) > 0:fb_ra = fb_res.json()["data"][0][0]fb_dec = fb_res.json()["data"][0][1]ruwe_val = round(api_data["ruwe"], 2) if api_data["ruwe"] is not None else "N/A"rv_val = round(api_data["radial_velocity"], 2) if api_data["radial_velocity"] is not None else "N/A"plx_val = round(api_data["parallax"], 3) if api_data["parallax"] is not None else "N/A"return {'id': source_id, 'ra': fb_ra, 'dec': fb_dec,'dist': "N/A", 'plx_display': f"{plx_val} mas" if plx_val != "N/A" else "N/A",'vel': "N/A", 'vel_color': "#ffffff",'ruwe': ruwe_val, 'ruwe_color': "#ffaa00" if isinstance(ruwe_val, float) and ruwe_val > 1.4 else "#ffffff",'rad_vel': rv_val,'mse': "N/A", 'kl': "N/A",'status': "BASIC TELEMETRY (AI OFFLINE / MISSING DATA)", 'color': "#888888",'neighbors_js': "", 'neighbor_count': 0}except Exception:return {"error": "NO SIGNAL: TARGET NOT FOUND IN LOCAL OR ESA DATABASES"}return {"error": "NO SIGNAL: TARGET NOT FOUND IN LOCAL OR ESA DATABASES"}exact_file_path = index_df.iloc[0]['filename']if not exact_file_path.startswith("hf://"):135 unmodified lines# ------------------------------------------------------------------------------# 5. SESSION MANAGEMENT & INTERFACE BUILDER (SINGLE CLICK RERUN FIX)# ------------------------------------------------------------------------------if 'session' not in st.session_state:st.session_state.session = {5 unmodified lineswith search_col_1:target_sid = st.text_input("ID_SEARCH", placeholder="ENTER SOURCE_ID", label_visibility="collapsed")with search_col_2:if st.button("SCAN"):if target_sid and target_sid.isdigit():with st.spinner(""):scan_res = execute_stellar_scan(target_sid)if "error" in scan_res:st.session_state.session.update({'data': None, 'is_active': True, 'has_error': True, 'error_text': scan_res['error']})else:st.session_state.session.update({'data': scan_res, 'ra': scan_res['ra'], 'dec': scan_res['dec'], 'is_active': True, 'has_error': False})st.rerun() # Force immediate UI update to fix the "3 Clicks Bug"popup_html_component = ""if st.session_state.session['is_active']:12345619 unmodified lines2627282930313210 unmodified lines434445464748495051525354555657585960613 unmodified lines6566676869707111 unmodified lines8384858687888990919215 unmodified lines108109110111112113114115116117118119104 unmodified lines22422522622722822923023123223323423523623715 unmodified lines253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297135 unmodified lines4334344354364374384395 unmodified lines445446447448449450451452453454455456457458459460461# ==============================================================================# SYSTEM: SADIM AI RADAR 77M - ADVANCED TACTICAL EXPERT INTERFACE# VERSION: 4.7.0 (LOCKED UI, SINGLE-CLICK FIX, FULL ESA PHYSICS FALLBACK)# ==============================================================================import streamlit as st19 unmodified lines)# ------------------------------------------------------------------------------# 2. ADVANCED CSS STYLING (LOCKED ALIGNMENT & CENTER LOADER)# ------------------------------------------------------------------------------st.markdown("""<style>10 unmodified lines/* Hide Native UI */[data-testid="stHeader"], footer { display: none !important; }/* THE ACADEMIC SEARCH BAR BLOCK - Strict Alignment */div[data-testid="stHorizontalBlock"] {position: fixed !important;bottom: 30px !important;left: 30px !important;z-index: 100000 !important;background-color: transparent !important;width: fit-content !important;height: 40px !important;display: flex !important;flex-direction: row !important;gap: 12px !important;align-items: flex-end !important; /* Force bottom alignment */}div[data-testid="column"] {3 unmodified linespadding: 0 !important;}/* Input Field */div[data-testid="stTextInput"] {margin-bottom: 0 !important;width: 200px !important;11 unmodified linesheight: 36px !important;outline: none !important;box-shadow: 4px 4px 15px rgba(0,0,0,0.8) !important;margin: 0 !important;}/* Scan Button */div[data-testid="stButton"] {margin-bottom: 0 !important;width: 80px !important;15 unmodified linesdisplay: block !important;cursor: crosshair !important;box-shadow: 4px 4px 15px rgba(0,0,0,0.8) !important;margin: 0 !important;}div[data-testid="stButton"] button:hover { background-color: #bbbbbb !important; }div[data-testid="stButton"] button:active { background-color: #999999 !important; border-color:#000; }/* CENTERED TACTICAL LOADER */[data-testid="stSpinner"] {position: fixed !important;top: 50% !important;104 unmodified linesif not token:return {"error": "SYSTEM_FATAL: HF_TOKEN NOT FOUND IN SECRETS"}esa_url = "https://gea.esac.esa.int/tap-server/tap/sync"# 4.0 ESA API FETCH (The Sniping API Call)api_data = {"parallax": None, "parallax_error": None, "ruwe": None, "radial_velocity": None, "parallax_over_error": None}try:adql_query = f"SELECT parallax, parallax_error, ruwe, radial_velocity, parallax_over_error FROM gaiadr3.gaia_source WHERE source_id = {source_id}"payload = {"REQUEST": "doQuery", "LANG": "ADQL", "FORMAT": "json", "QUERY": adql_query}response = requests.get(esa_url, params=payload, timeout=10)if response.status_code == 200:res_json = response.json()if "data" in res_json and len(res_json["data"]) > 0:15 unmodified linesindex_query = f"SELECT filename FROM '{index_path}' WHERE source_id = {source_id} LIMIT 1"index_df = db.query(index_query).df()# ======================================================================# 4.1.1 EMERGENCY ESA FALLBACK (If Target Not in HF Local DB)# ======================================================================if index_df.empty:db.close()fb_query = f"SELECT ra, dec, parallax, parallax_error, ruwe, radial_velocity, pmra, pmdec FROM gaiadr3.gaia_source WHERE source_id = {source_id}"try:# Increased timeout to 15s for heavy ESA queriesfb_res = requests.get(esa_url, params={"REQUEST": "doQuery", "LANG": "ADQL", "FORMAT": "json", "QUERY": fb_query}, timeout=15)if fb_res.status_code == 200 and len(fb_res.json().get("data", [])) > 0:fb_data = fb_res.json()["data"][0]fb_ra, fb_dec, fb_plx, fb_plx_err, fb_ruwe, fb_rv, fb_pmra, fb_pmdec = fb_data# Manual Physics Calculation for ESA Fallbackfb_dist_pc = 1000.0 / fb_plx if fb_plx is not None and fb_plx > 0 else -1.0fb_dist_ly = fb_dist_pc * 3.26156 if fb_dist_pc > 0 else "N/A"fb_pm_total = math.sqrt((fb_pmra or 0)**2 + (fb_pmdec or 0)**2)fb_v_tan = 4.74 * 1e-3 * fb_pm_total * fb_dist_pc if fb_dist_pc > 0 else "N/A"fb_plx_display = f"{round(fb_plx, 3)} ± {round(fb_plx_err, 3)} mas" if fb_plx is not None and fb_plx_err is not None else "N/A"vel_color = "#ff3333" if isinstance(fb_v_tan, float) and fb_v_tan > 600.0 else "#ffffff"ruwe_color = "#ffaa00" if isinstance(fb_ruwe, float) and fb_ruwe > 1.4 else "#ffffff"return {'id': source_id, 'ra': fb_ra, 'dec': fb_dec,'dist': round(fb_dist_ly, 1) if isinstance(fb_dist_ly, float) else "N/A",'plx_display': fb_plx_display,'vel': round(fb_v_tan, 2) if isinstance(fb_v_tan, float) else "N/A", 'vel_color': vel_color,'ruwe': round(fb_ruwe, 2) if fb_ruwe is not None else "N/A", 'ruwe_color': ruwe_color,'rad_vel': round(fb_rv, 2) if fb_rv is not None else "N/A",'mse': "N/A", 'kl': "N/A",'status': "ESA DIRECT LINK (AI OFFLINE - MISSING LOCAL DATA)", 'color': "#888888",'neighbors_js': "", 'neighbor_count': 0}except Exception:return {"error": "NO SIGNAL: TARGET NOT FOUND IN LOCAL OR ESA DATABASES. ESA API MIGHT BE DOWN."}return {"error": "NO SIGNAL: TARGET ID DOES NOT EXIST IN GAIA DR3 ARCHIVES."}exact_file_path = index_df.iloc[0]['filename']if not exact_file_path.startswith("hf://"):135 unmodified lines# ------------------------------------------------------------------------------# 5. SESSION MANAGEMENT & INTERFACE BUILDER (SINGLE CLICK & FIXED UI)# ------------------------------------------------------------------------------if 'session' not in st.session_state:st.session_state.session = {5 unmodified lineswith search_col_1:target_sid = st.text_input("ID_SEARCH", placeholder="ENTER SOURCE_ID", label_visibility="collapsed")with search_col_2:scan_clicked = st.button("SCAN")# Spinner is OUTSIDE the columns to prevent UI jumpingif scan_clicked and target_sid and target_sid.isdigit():with st.spinner(""):scan_res = execute_stellar_scan(target_sid)if "error" in scan_res:st.session_state.session.update({'data': None, 'is_active': True, 'has_error': True, 'error_text': scan_res['error']})else:st.session_state.session.update({'data': scan_res, 'ra': scan_res['ra'], 'dec': scan_res['dec'], 'is_active': True, 'has_error': False})st.rerun() # Force immediate UI update (One-Click Fix)popup_html_component = ""if st.session_state.session['is_active']:
app.py+53-36