Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.responses import Response | |
| from rembg import remove, new_session | |
| app = FastAPI() | |
| # Create ONE session (cached in memory) | |
| SESSION = new_session( | |
| "u2net", | |
| alpha_matting=True, | |
| alpha_matting_foreground_threshold=240, | |
| alpha_matting_background_threshold=10, | |
| alpha_matting_erode_size=10, | |
| ) | |
| def health(): | |
| return {"status": "ok"} | |
| async def remove_bg(file: UploadFile = File(...)): | |
| input_bytes = await file.read() | |
| output_bytes = remove( | |
| input_bytes, | |
| session=SESSION | |
| ) | |
| return Response( | |
| content=output_bytes, | |
| media_type="image/png" | |
| ) | |