Spaces:
Sleeping

kriyatrack / app.py
budijuarto's picture
Serve the Svelte app through Gradio Server routes and polish the architecture view
d3cac26 verified
Raw History Blame Contribute Delete
2.89 kB
"""Run the KriyaTrack studio; order and model processes fail independently."""
import os
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent
os.chdir(ROOT)
os.environ.setdefault("KERAS_BACKEND", "torch")
os.environ.setdefault("KERAS_HOME", str(ROOT / "runtime" / "keras"))
os.environ.setdefault("KRIYA_DATA", str(Path("/data/kriyatrack") if Path("/data").is_dir() and os.access("/data", os.W_OK) else ROOT / "runtime"))
def serve_model():
import uvicorn
from server.inference import app, load_model
try:
load_model()
except Exception as exc:
print(f"Model warmup: {type(exc).__name__}: {exc}", flush=True)
uvicorn.run(app, host="127.0.0.1", port=int(os.environ.get("KRIYA_MODEL_PORT", "7872")), access_log=False)
def hosted_lab():
import spaces
import torch
import gradio as gr
from server.studio_proxy import attach_studio
studio = gr.Server(title="KriyaTrack Studio", docs_url=None, redoc_url=None)
attach_studio(studio, port=7870)
@studio.api(name='verify_gpu', description='Verifikasi model KriyaTrack pada GPU untuk riwayat omzet sintetis.')
@spaces.GPU(duration=30)
def verify_on_gpu(series: list[dict]) -> dict:
import keras
from server.inference import Series, features, load_model
parsed = Series.model_validate({"history": series})
row, month = features(parsed.history)
model = load_model().to("cuda")
with torch.no_grad():
result = model(torch.as_tensor(row, device="cuda"), training=False)
value = keras.ops.convert_to_numpy(result).reshape(-1)[0]
return {"month": month, "value": max(0, round(float(value))), "model": "neural"}
return studio
def run():
hosted = bool(os.environ.get("SPACE_ID"))
frontend_port = os.environ.get("PORT", "7860")
if hosted:
studio = hosted_lab()
children = []
try:
child_env = {**os.environ, "PORT": "7870" if hosted else frontend_port}
for args in ([sys.executable, "-m", "uvicorn", "server.orders:app", "--host", "127.0.0.1", "--port", os.environ.get("KRIYA_ORDERS_PORT", "7871"), "--no-access-log"],
[sys.executable, "app.py", "model"], ["node", "server/gateway.mjs"]):
children.append(subprocess.Popen(args, env=child_env))
if hosted:
studio.launch(server_name="0.0.0.0", server_port=int(frontend_port), ssr_mode=False, show_error=True)
else:
while children[0].poll() is None and children[2].poll() is None:
time.sleep(1)
finally:
for child in children:
if child.poll() is None:
child.terminate()
if __name__ == "__main__":
serve_model() if sys.argv[1:] == ["model"] else run()