COCODEDE04 commited on
Commit
95af2b2
·
verified ·
1 Parent(s): 6fb27ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -8
app.py CHANGED
@@ -126,20 +126,41 @@ else:
126
 
127
 
128
  # ---------- DEFINE GRADIO INTERFACE ----------
 
129
  iface = gr.Interface(
130
  fn=predict_from_json,
131
  inputs=gr.JSON(label="ratios JSON (dict of feature -> value)"),
132
  outputs="json",
133
  title="Static Fingerprint Model API",
134
- description="POST your 21 ratios. Returns probabilities + predicted state.",
135
- api_name="predict", # <--- this line is what creates the API
136
  )
137
 
138
- # ⚠️ Use gr.mount_gradio_app to make /gradio_api/ routes appear
139
- from fastapi import FastAPI
 
 
140
  app = FastAPI()
141
- app = gr.mount_gradio_app(app, iface, path="/") # mount Gradio onto FastAPI root
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
- if __name__ == "__main__":
144
- import uvicorn
145
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
126
 
127
 
128
  # ---------- DEFINE GRADIO INTERFACE ----------
129
+ # --- UI (unchanged) ---
130
  iface = gr.Interface(
131
  fn=predict_from_json,
132
  inputs=gr.JSON(label="ratios JSON (dict of feature -> value)"),
133
  outputs="json",
134
  title="Static Fingerprint Model API",
135
+ description="POST your 21 ratios as a JSON dict. Returns probabilities + predicted state."
 
136
  )
137
 
138
+ # --- FastAPI app with a simple REST endpoint ---
139
+ from fastapi import FastAPI, Request
140
+ import gradio as gr
141
+
142
  app = FastAPI()
143
+ # Mount the Gradio UI at the root so your Space page still works
144
+ app = gr.mount_gradio_app(app, iface, path="/")
145
+
146
+ @app.post("/predict")
147
+ async def api_predict(req: Request):
148
+ """
149
+ Accepts either:
150
+ 1) raw dict: {"autosuf_oper":1.2, ...}
151
+ 2) gradio format: {"data":[{...}]}
152
+ """
153
+ body = await req.json()
154
+ if isinstance(body, dict) and "data" in body and isinstance(body["data"], list) and body["data"]:
155
+ payload = body["data"][0] # unwrap Gradio shape
156
+ elif isinstance(body, dict):
157
+ payload = body # raw dict
158
+ else:
159
+ return {"error": "Invalid payload. Send a JSON object of feature->value OR {'data':[that_object]}"}
160
+
161
+ try:
162
+ return predict_from_json(payload) # reuse your existing function
163
+ except Exception as e:
164
+ return {"error": f"{type(e).__name__}: {e}"}
165
 
166
+ # Spaces will auto-run with uvicorn; no need to call launch() here.