OKF-Screener / app.py
sammoftah's picture
Upload folder using huggingface_hub
bb8c32c verified
Raw
History Blame Contribute Delete
1.81 kB
"""OKF Screener — Gradio Server entrypoint.
Serves a custom HTML frontend and exposes a single streaming API endpoint
that runs three compliance-checking lanes (RAG, CSV, OKF) concurrently.
"""
import json
import os
import sys
from dotenv import load_dotenv
load_dotenv()
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
_ROOT = os.path.dirname(os.path.abspath(__file__))
if _ROOT not in sys.path:
sys.path.insert(0, _ROOT)
import gradio as gr
from fastapi import Request
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from backend.race import race_events
app = gr.Server()
_DIST = os.path.join(_ROOT, "frontend", "dist")
@app.post("/race/stream")
async def race_stream(request: Request):
"""Stream NDJSON events from the three-lane compliance race."""
try:
body = await request.json()
except Exception:
body = {}
query = body.get("query", "").strip()
model_id = body.get("model_id", "meta-llama/Llama-3.3-70B-Instruct").strip()
def generate():
try:
for event in race_events(query, model_id):
yield json.dumps(event, ensure_ascii=True) + "\n"
except Exception as exc:
yield json.dumps({"type": "error", "message": str(exc)}) + "\n"
return StreamingResponse(
generate(),
media_type="application/x-ndjson",
headers={"Cache-Control": "no-store"},
)
@app.get("/")
def index():
path = os.path.join(_DIST, "index.html")
if os.path.isfile(path):
return FileResponse(path)
return JSONResponse({"error": "Frontend not built."}, status_code=503)
if __name__ == "__main__":
app.launch(
server_name="0.0.0.0",
server_port=int(os.environ.get("PORT", "7860")),
show_error=True,
)