Asish Karthikeya Gogineni commited on
Commit ·
d9af5a7
1
Parent(s): 3e30d53
Fix: Remove infinite recursion bug causing black screen
Browse files
app.py
CHANGED
|
@@ -36,8 +36,63 @@ load_css("style.css")
|
|
| 36 |
# --- Auto-Start Backend Services ---
|
| 37 |
@st.cache_resource
|
| 38 |
def start_background_services():
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Trigger startup (cached, runs once per container)
|
| 43 |
start_background_services()
|
|
@@ -404,4 +459,10 @@ def render_analysis():
|
|
| 404 |
"""
|
| 405 |
alerts_container.markdown(html, unsafe_allow_html=True)
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
render_analysis()
|
|
|
|
| 36 |
# --- Auto-Start Backend Services ---
|
| 37 |
@st.cache_resource
|
| 38 |
def start_background_services():
|
| 39 |
+
"""Checks if backend services are running and starts them if needed."""
|
| 40 |
+
# Check if Gateway is already running
|
| 41 |
+
try:
|
| 42 |
+
with httpx.Client(timeout=1.0) as client:
|
| 43 |
+
response = client.get("http://127.0.0.1:8000/")
|
| 44 |
+
if response.status_code == 200:
|
| 45 |
+
print("✅ Gateway is already running.")
|
| 46 |
+
return
|
| 47 |
+
except:
|
| 48 |
+
print("⚠️ Gateway not found. Initializing backend services...")
|
| 49 |
+
|
| 50 |
+
services = [
|
| 51 |
+
["mcp_gateway.py", "8000"],
|
| 52 |
+
["tavily_mcp.py", "8001"],
|
| 53 |
+
["alphavantage_mcp.py", "8002"],
|
| 54 |
+
["private_mcp.py", "8003"]
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
env = os.environ.copy()
|
| 58 |
+
# Inject secrets
|
| 59 |
+
try:
|
| 60 |
+
def flatten_secrets(secrets, prefix=""):
|
| 61 |
+
for key, value in secrets.items():
|
| 62 |
+
if isinstance(value, dict):
|
| 63 |
+
flatten_secrets(value, f"{prefix}{key}_")
|
| 64 |
+
else:
|
| 65 |
+
env[f"{prefix}{key}"] = str(value)
|
| 66 |
+
|
| 67 |
+
if hasattr(st, "secrets"):
|
| 68 |
+
flatten_secrets(st.secrets)
|
| 69 |
+
print("✅ Secrets injected into subprocess environment.")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"⚠️ Secrets injection warning: {e}")
|
| 72 |
+
|
| 73 |
+
# Start services - NON-BLOCKING, LOG TO STDOUT
|
| 74 |
+
cwd = os.path.dirname(os.path.abspath(__file__))
|
| 75 |
+
for script, port in services:
|
| 76 |
+
print(f"🚀 Launching {script} on port {port}...")
|
| 77 |
+
# Use subprocess.Popen without waiting
|
| 78 |
+
subprocess.Popen(
|
| 79 |
+
[sys.executable, script],
|
| 80 |
+
cwd=cwd,
|
| 81 |
+
env=env,
|
| 82 |
+
# Inherit stdout/stderr so logs appear in Streamlit Cloud console
|
| 83 |
+
# stdout=subprocess.DEVNULL,
|
| 84 |
+
# stderr=subprocess.DEVNULL
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
print("🚀 Launching Monitor...")
|
| 88 |
+
subprocess.Popen(
|
| 89 |
+
[sys.executable, "monitor.py"],
|
| 90 |
+
cwd=cwd,
|
| 91 |
+
env=env
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Do NOT wait. Return immediately to let UI render.
|
| 95 |
+
print("✅ Background services launch triggered.")
|
| 96 |
|
| 97 |
# Trigger startup (cached, runs once per container)
|
| 98 |
start_background_services()
|
|
|
|
| 459 |
"""
|
| 460 |
alerts_container.markdown(html, unsafe_allow_html=True)
|
| 461 |
|
| 462 |
+
# --- Main App Routing ---
|
| 463 |
+
render_sidebar()
|
| 464 |
+
|
| 465 |
+
if st.session_state.page == 'home':
|
| 466 |
+
render_home()
|
| 467 |
+
elif st.session_state.page == 'analysis':
|
| 468 |
render_analysis()
|