Spaces:
Sleeping
Sleeping
Abhijeet Mahapatra commited on
Commit Β·
333420f
1
Parent(s): f86ffb5
Fixed gradio loader
Browse files
server.py
CHANGED
|
@@ -34,11 +34,8 @@ import urllib.error
|
|
| 34 |
import urllib.parse
|
| 35 |
import urllib.request
|
| 36 |
from pathlib import Path
|
| 37 |
-
|
| 38 |
from typing import Any, Dict, List, Optional
|
| 39 |
-
from fastapi.responses import HTMLResponse
|
| 40 |
-
from fastapi.staticfiles import StaticFiles
|
| 41 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 42 |
from dotenv import load_dotenv
|
| 43 |
|
| 44 |
load_dotenv()
|
|
@@ -62,13 +59,6 @@ logging.basicConfig(
|
|
| 62 |
)
|
| 63 |
logger = logging.getLogger("dashboard-server")
|
| 64 |
|
| 65 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 66 |
-
# gr.Server
|
| 67 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
-
|
| 69 |
-
FRONTEND_DIR = BASE_DIR / "frontend"
|
| 70 |
-
INDEX_HTML = FRONTEND_DIR / "index.html"
|
| 71 |
-
|
| 72 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
# Backend URL
|
| 74 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -166,60 +156,10 @@ def run_pipeline(
|
|
| 166 |
"links_graph": links_graph,
|
| 167 |
}
|
| 168 |
|
| 169 |
-
server = Server(
|
| 170 |
-
title="Iroha Causal Terminal",
|
| 171 |
-
description=(
|
| 172 |
-
"Iroha Financial Intelligence β real-time causal probability matrix, "
|
| 173 |
-
"HHKD decomposition, DoFlow inference and sector hierarchy over NIFTY50."
|
| 174 |
-
),
|
| 175 |
-
version="2.0.0",
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
-
# ββ CORS (same as main.py) ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 179 |
-
server.add_middleware(
|
| 180 |
-
CORSMiddleware,
|
| 181 |
-
allow_origins=["*"],
|
| 182 |
-
allow_credentials=True,
|
| 183 |
-
allow_methods=["*"],
|
| 184 |
-
allow_headers=["*"],
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
# ββ Static files β mount frontend/ at /static βββββββββββββββββββββββββββββ
|
| 188 |
-
server.mount(
|
| 189 |
-
"/static",
|
| 190 |
-
StaticFiles(directory=str(FRONTEND_DIR)),
|
| 191 |
-
name="static",
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 195 |
-
# HTML route β serves the custom frontend
|
| 196 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 197 |
-
|
| 198 |
-
@server.get("/", response_class=HTMLResponse, include_in_schema=False)
|
| 199 |
-
async def serve_index():
|
| 200 |
-
"""Serve the Iroha Causal Terminal SPA."""
|
| 201 |
-
if not INDEX_HTML.exists():
|
| 202 |
-
return HTMLResponse("<h1>Frontend not found. Run from backend/</h1>", status_code=500)
|
| 203 |
-
return HTMLResponse(INDEX_HTML.read_text(encoding="utf-8"))
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 207 |
-
# Health check
|
| 208 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 209 |
-
|
| 210 |
-
@server.get("/v2/health", tags=["utility"])
|
| 211 |
-
async def health():
|
| 212 |
-
"""Lightweight health-check used by the frontend API banner."""
|
| 213 |
-
return {"status": "ok", "version": "2.0.0"}
|
| 214 |
-
|
| 215 |
-
|
| 216 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 217 |
-
#
|
| 218 |
-
# These are reachable via the Gradio JS Client as well as plain fetch().
|
| 219 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 220 |
|
| 221 |
-
# ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 222 |
-
|
| 223 |
# URL of the noisy_boy_backend β used to fetch the validated causal matrix.
|
| 224 |
# By default, point to ourselves since we now successfully mount the backend routers.
|
| 225 |
# Override via BACKEND_API_URL env var if running a separate backend on 8000.
|
|
@@ -466,16 +406,6 @@ def _abduct_and_predict(
|
|
| 466 |
# 3. Optionally consult pywhyllm guidance from the backend payload.
|
| 467 |
|
| 468 |
|
| 469 |
-
@server.api(
|
| 470 |
-
name="run_inference",
|
| 471 |
-
description=(
|
| 472 |
-
"Run causal inference (association / intervention / counterfactual) "
|
| 473 |
-
"using a validated causal matrix fetched from the backend API. "
|
| 474 |
-
"Layers: 1=Association(DoWhy backdoor), 2=Intervention(SCM propagation), "
|
| 475 |
-
"3=Counterfactual(SCM abduction)."
|
| 476 |
-
),
|
| 477 |
-
concurrency_limit=4,
|
| 478 |
-
)
|
| 479 |
def run_inference(
|
| 480 |
ticker: str = "RELIANCE",
|
| 481 |
mode: str = "assert",
|
|
@@ -898,21 +828,53 @@ def run_inference(
|
|
| 898 |
|
| 899 |
|
| 900 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 901 |
-
# Entry point
|
| 902 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 903 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 904 |
if __name__ == "__main__":
|
| 905 |
port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", "7860")))
|
| 906 |
host = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 907 |
|
| 908 |
logger.info(f"Starting Iroha Causal Terminal on {host}:{port}")
|
| 909 |
-
logger.info(f" β Frontend : http://localhost:{port}/")
|
| 910 |
-
logger.info(f" β API docs : http://localhost:{port}/docs")
|
| 911 |
|
| 912 |
-
|
| 913 |
server_name=host,
|
| 914 |
server_port=port,
|
| 915 |
-
allowed_paths=[str(FRONTEND_DIR)],
|
| 916 |
show_error=True,
|
| 917 |
-
quiet=False,
|
| 918 |
)
|
|
|
|
| 34 |
import urllib.parse
|
| 35 |
import urllib.request
|
| 36 |
from pathlib import Path
|
| 37 |
+
import gradio as gr
|
| 38 |
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
| 39 |
from dotenv import load_dotenv
|
| 40 |
|
| 41 |
load_dotenv()
|
|
|
|
| 59 |
)
|
| 60 |
logger = logging.getLogger("dashboard-server")
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
# Backend URL
|
| 64 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 156 |
"links_graph": links_graph,
|
| 157 |
}
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 160 |
+
# Helpers
|
|
|
|
| 161 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 162 |
|
|
|
|
|
|
|
| 163 |
# URL of the noisy_boy_backend β used to fetch the validated causal matrix.
|
| 164 |
# By default, point to ourselves since we now successfully mount the backend routers.
|
| 165 |
# Override via BACKEND_API_URL env var if running a separate backend on 8000.
|
|
|
|
| 406 |
# 3. Optionally consult pywhyllm guidance from the backend payload.
|
| 407 |
|
| 408 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
def run_inference(
|
| 410 |
ticker: str = "RELIANCE",
|
| 411 |
mode: str = "assert",
|
|
|
|
| 828 |
|
| 829 |
|
| 830 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 831 |
+
# Gradio UI & Entry point
|
| 832 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 833 |
|
| 834 |
+
with gr.Blocks(title="Iroha Causal Terminal") as demo:
|
| 835 |
+
gr.Markdown("# Iroha Causal Terminal")
|
| 836 |
+
gr.Markdown("Iroha Financial Intelligence β real-time causal probability matrix, HHKD decomposition, DoFlow inference and sector hierarchy over NIFTY50.")
|
| 837 |
+
|
| 838 |
+
with gr.Row():
|
| 839 |
+
ticker = gr.Textbox(label="Ticker", value="RELIANCE")
|
| 840 |
+
mode = gr.Dropdown(choices=["assert", "intervene", "counterfactual"], label="Mode", value="assert")
|
| 841 |
+
treatment = gr.Textbox(label="Treatment", value="Revenue")
|
| 842 |
+
outcome = gr.Textbox(label="Outcome", value="NetIncome")
|
| 843 |
+
target = gr.Textbox(label="Target", value="")
|
| 844 |
+
|
| 845 |
+
with gr.Row():
|
| 846 |
+
value = gr.Number(label="Value", value=1.1)
|
| 847 |
+
cf_value = gr.Number(label="CF Value")
|
| 848 |
+
value_type = gr.Dropdown(choices=["absolute", "multiplier", "percent_change"], label="Value Type", value="multiplier")
|
| 849 |
+
horizon = gr.Number(label="Horizon", value=5, precision=0)
|
| 850 |
+
observed_t = gr.Number(label="Observed T", value=-1, precision=0)
|
| 851 |
+
threshold = gr.Number(label="Threshold", value=0.5)
|
| 852 |
+
|
| 853 |
+
with gr.Row():
|
| 854 |
+
use_pywhyllm = gr.Checkbox(label="Use PyWhyLLM", value=False)
|
| 855 |
+
return_assumption_report = gr.Checkbox(label="Return Assumption Report", value=False)
|
| 856 |
+
|
| 857 |
+
btn = gr.Button("Run Inference")
|
| 858 |
+
out = gr.JSON(label="Result")
|
| 859 |
+
|
| 860 |
+
btn.click(
|
| 861 |
+
fn=run_inference,
|
| 862 |
+
inputs=[
|
| 863 |
+
ticker, mode, treatment, outcome, target, value, cf_value, value_type,
|
| 864 |
+
horizon, observed_t, threshold, use_pywhyllm, return_assumption_report
|
| 865 |
+
],
|
| 866 |
+
outputs=out,
|
| 867 |
+
api_name="run_inference"
|
| 868 |
+
)
|
| 869 |
+
|
| 870 |
if __name__ == "__main__":
|
| 871 |
port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", "7860")))
|
| 872 |
host = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 873 |
|
| 874 |
logger.info(f"Starting Iroha Causal Terminal on {host}:{port}")
|
|
|
|
|
|
|
| 875 |
|
| 876 |
+
demo.launch(
|
| 877 |
server_name=host,
|
| 878 |
server_port=port,
|
|
|
|
| 879 |
show_error=True,
|
|
|
|
| 880 |
)
|