Spaces:
Running on Zero
Running on Zero
| import os | |
| import gradio as gr | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from gradio.routes import App | |
| from sqlalchemy import func | |
| from app.database import SessionLocal | |
| from app.main import lifespan | |
| from app.models import Call, Job | |
| from app.routers import artifacts, calls, sentiment | |
| from app.storage import is_configured | |
| def runtime_snapshot(): | |
| snapshot = { | |
| "service": "call-qa-processing", | |
| "database_configured": bool(os.environ.get("DATABASE_URL")), | |
| "storage_configured": is_configured(), | |
| "acoustic_enabled": os.environ.get("ENABLE_ACOUSTIC", "0") == "1", | |
| } | |
| db = SessionLocal() | |
| try: | |
| snapshot["calls"] = db.query(func.count(Call.call_id)).scalar() or 0 | |
| snapshot["jobs"] = { | |
| status: db.query(func.count(Job.job_id)).filter(Job.status == status).scalar() or 0 | |
| for status in ("queued", "processing", "succeeded", "failed") | |
| } | |
| except Exception as exc: | |
| snapshot["database_error"] = str(exc)[:240] | |
| finally: | |
| db.close() | |
| return snapshot | |
| def build_space_app(gpu_snapshot): | |
| with gr.Blocks(title="Call QA Processing") as demo: | |
| gr.Markdown("# Call QA Processing") | |
| state = gr.JSON(label="Runtime status") | |
| refresh = gr.Button("Refresh", variant="primary") | |
| refresh.click(runtime_snapshot, outputs=state, api_name="runtime_status") | |
| demo.load(runtime_snapshot, outputs=state) | |
| gpu_state = gr.JSON(label="ZeroGPU status") | |
| gpu_check = gr.Button("Check ZeroGPU") | |
| gpu_check.click(gpu_snapshot, outputs=gpu_state, api_name="gpu_status") | |
| api = App() | |
| origins = os.environ.get("FRONTEND_ORIGIN", "*") | |
| wildcard = origins.strip() == "*" | |
| api.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"] if wildcard else [item.strip() for item in origins.split(",")], | |
| allow_credentials=not wildcard, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| api.include_router(calls.router) | |
| api.include_router(sentiment.router) | |
| api.include_router(artifacts.router) | |
| def health_check(): | |
| return {"status": "ok", "service": "capstone_api"} | |
| return demo, api | |