Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Backward-compatible API
Browse files
app.py
CHANGED
|
@@ -177,10 +177,10 @@ gradio_iface = gr.Interface(
|
|
| 177 |
|
| 178 |
app = FastAPI()
|
| 179 |
|
| 180 |
-
# Mount Gradio
|
| 181 |
-
app = gr.mount_gradio_app(app, gradio_iface, path="/")
|
| 182 |
|
| 183 |
-
# Custom
|
| 184 |
@app.post("/predict-saree")
|
| 185 |
async def predict_saree(request: Request):
|
| 186 |
try:
|
|
@@ -191,7 +191,6 @@ async def predict_saree(request: Request):
|
|
| 191 |
|
| 192 |
result_img = process_saree(body["data"])
|
| 193 |
|
| 194 |
-
# Convert output image to base64 PNG
|
| 195 |
buf = BytesIO()
|
| 196 |
result_img.save(buf, format="PNG")
|
| 197 |
base64_img = base64.b64encode(buf.getvalue()).decode("utf-8")
|
|
@@ -200,7 +199,6 @@ async def predict_saree(request: Request):
|
|
| 200 |
|
| 201 |
except HTTPException as e:
|
| 202 |
return JSONResponse(status_code=e.status_code, content={"error": "Input Error", "details": e.detail})
|
| 203 |
-
|
| 204 |
except Exception as e:
|
| 205 |
tb = traceback.format_exc()
|
| 206 |
return JSONResponse(
|
|
@@ -250,7 +248,6 @@ async def unhandled_exception_handler(request: Request, exc: Exception):
|
|
| 250 |
}
|
| 251 |
)
|
| 252 |
|
| 253 |
-
# Run (Hugging Face will call uvicorn automatically)
|
| 254 |
if __name__ == "__main__":
|
| 255 |
import uvicorn
|
| 256 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 177 |
|
| 178 |
app = FastAPI()
|
| 179 |
|
| 180 |
+
# Mount Gradio at /gradio
|
| 181 |
+
app = gr.mount_gradio_app(app, gradio_iface, path="/gradio")
|
| 182 |
|
| 183 |
+
# Custom API endpoint
|
| 184 |
@app.post("/predict-saree")
|
| 185 |
async def predict_saree(request: Request):
|
| 186 |
try:
|
|
|
|
| 191 |
|
| 192 |
result_img = process_saree(body["data"])
|
| 193 |
|
|
|
|
| 194 |
buf = BytesIO()
|
| 195 |
result_img.save(buf, format="PNG")
|
| 196 |
base64_img = base64.b64encode(buf.getvalue()).decode("utf-8")
|
|
|
|
| 199 |
|
| 200 |
except HTTPException as e:
|
| 201 |
return JSONResponse(status_code=e.status_code, content={"error": "Input Error", "details": e.detail})
|
|
|
|
| 202 |
except Exception as e:
|
| 203 |
tb = traceback.format_exc()
|
| 204 |
return JSONResponse(
|
|
|
|
| 248 |
}
|
| 249 |
)
|
| 250 |
|
|
|
|
| 251 |
if __name__ == "__main__":
|
| 252 |
import uvicorn
|
| 253 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|