Commit ยท
dd66cec
1
Parent(s): bb81bd9
Launch Gradio app directly and dynamically register FastAPI routes to support ZeroGPU startup hooks
Browse files361 unmodified lines3623633643653663673683693703713723738 unmodified lines38238338438538638738858 unmodified lines447448449450451452453454361 unmodified lines# Create static directoryos.makedirs("./static/outputs", exist_ok=True)# Define FastAPI applicationapp = FastAPI()# Mount the static files directoryapp.mount("/static", StaticFiles(directory="static"), name="static")# Helper task to clean up old files (>1 hour old) to prevent disk fill-updef cleanup_old_files():now = time.time()8 unmodified linesexcept Exception as e:print(f"Error removing {file_path}: {e}")# Expose REST API endpoint for automated backend workflows (e.g. posting to Instagram)@app.post("/api/tryon")async def api_tryon(request: Request,58 unmodified linestraceback.print_exc()return JSONResponse({"status": "error", "message": str(e)}, status_code=500)# Mount Gradio interface inside FastAPI under the root pathapp = gr.mount_gradio_app(app, image_blocks, path="/")if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)361 unmodified lines3623633643653663678 unmodified lines37637737837938038138238338438538638738838939039139239339439558 unmodified lines454455456457458459361 unmodified lines# Create static directoryos.makedirs("./static/outputs", exist_ok=True)# Helper task to clean up old files (>1 hour old) to prevent disk fill-updef cleanup_old_files():now = time.time()8 unmodified linesexcept Exception as e:print(f"Error removing {file_path}: {e}")# Launch the Gradio app directly on port 7860 (required by Hugging Face ZeroGPU runtime check)image_blocks.launch(server_name="0.0.0.0",server_port=7860,prevent_thread_lock=True)# Now that the Gradio app is launched, customize its internal FastAPI instance!app = image_blocks.app# Mount the static files directoryapp.mount("/static", StaticFiles(directory="static"), name="static")# Expose REST API endpoint on the Gradio-created FastAPI server@app.post("/api/tryon")async def api_tryon(request: Request,58 unmodified linestraceback.print_exc()return JSONResponse({"status": "error", "message": str(e)}, status_code=500)# Keep the main process runningwhile True:time.sleep(1)
app.py+17-12