Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,31 +86,60 @@ def predict_from_json(payload):
|
|
| 86 |
return predict_core(payload)
|
| 87 |
|
| 88 |
# ------------------ FastAPI + Gradio ------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
app = FastAPI()
|
| 90 |
app.add_middleware(
|
| 91 |
CORSMiddleware,
|
| 92 |
allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],
|
| 93 |
)
|
| 94 |
|
| 95 |
-
# Plain
|
| 96 |
-
|
| 97 |
-
async def api_predict(req: Request):
|
| 98 |
body = await req.json()
|
|
|
|
| 99 |
if isinstance(body, dict) and "data" in body and isinstance(body["data"], list) and body["data"]:
|
| 100 |
-
body = body["data"][0]
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
# Optional health check
|
| 104 |
@app.get("/health")
|
| 105 |
def health():
|
| 106 |
return {"ok": True}
|
| 107 |
|
| 108 |
-
# Mount UI at root
|
| 109 |
ui = gr.Interface(
|
| 110 |
fn=predict_from_json,
|
| 111 |
inputs=gr.JSON(label="ratios JSON (dict of feature -> value)"),
|
| 112 |
outputs="json",
|
| 113 |
title="Static Fingerprint Model API",
|
| 114 |
-
description="POST your 21 ratios as a JSON dict. Returns probabilities + predicted state."
|
| 115 |
)
|
| 116 |
-
app = gr.mount_gradio_app(app, ui, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
return predict_core(payload)
|
| 87 |
|
| 88 |
# ------------------ FastAPI + Gradio ------------------
|
| 89 |
+
# ------------------ FastAPI + Gradio ------------------
|
| 90 |
+
from fastapi import FastAPI, Request
|
| 91 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 92 |
+
import gradio as gr
|
| 93 |
+
|
| 94 |
app = FastAPI()
|
| 95 |
app.add_middleware(
|
| 96 |
CORSMiddleware,
|
| 97 |
allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],
|
| 98 |
)
|
| 99 |
|
| 100 |
+
# Plain handler we’ll reuse
|
| 101 |
+
async def _handle_predict(req: Request):
|
|
|
|
| 102 |
body = await req.json()
|
| 103 |
+
# accept either raw dict or {"data":[{...}]}
|
| 104 |
if isinstance(body, dict) and "data" in body and isinstance(body["data"], list) and body["data"]:
|
| 105 |
+
body = body["data"][0]
|
| 106 |
+
if not isinstance(body, dict):
|
| 107 |
+
return {"error": "Invalid payload. Send a JSON object of feature->value or {'data':[that_object]}."}
|
| 108 |
+
try:
|
| 109 |
+
return predict_from_json(body)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return {"error": f"{type(e).__name__}: {e}"}
|
| 112 |
+
|
| 113 |
+
@app.post("/predict")
|
| 114 |
+
async def predict_main(req: Request):
|
| 115 |
+
return await _handle_predict(req)
|
| 116 |
+
|
| 117 |
+
# Be generous: also accept your older paths
|
| 118 |
+
@app.post("/run/predict")
|
| 119 |
+
async def predict_compat1(req: Request):
|
| 120 |
+
return await _handle_predict(req)
|
| 121 |
+
|
| 122 |
+
@app.post("/gradio_api/call/predict")
|
| 123 |
+
async def predict_compat2(req: Request):
|
| 124 |
+
return await _handle_predict(req)
|
| 125 |
|
|
|
|
| 126 |
@app.get("/health")
|
| 127 |
def health():
|
| 128 |
return {"ok": True}
|
| 129 |
|
| 130 |
+
# Mount the Gradio UI at root
|
| 131 |
ui = gr.Interface(
|
| 132 |
fn=predict_from_json,
|
| 133 |
inputs=gr.JSON(label="ratios JSON (dict of feature -> value)"),
|
| 134 |
outputs="json",
|
| 135 |
title="Static Fingerprint Model API",
|
| 136 |
+
description="POST your 21 ratios as a JSON dict. Returns probabilities + predicted state.",
|
| 137 |
)
|
| 138 |
+
app = gr.mount_gradio_app(app, ui, path="/")
|
| 139 |
+
|
| 140 |
+
# DEBUG: print available routes so we can see them in the logs
|
| 141 |
+
for r in app.router.routes:
|
| 142 |
+
try:
|
| 143 |
+
print("ROUTE:", r.path)
|
| 144 |
+
except Exception:
|
| 145 |
+
pass
|