Spaces:
Sleeping
Sleeping
Download server.py from Sarvesh3529/Matrix-backend-python: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/Sarvesh3529/Matrix-backend-python/resolve/main/server.py
- Command line
-
hf download hf://spaces/Sarvesh3529/Matrix-backend-python/server.py
-
curl -L -o server.py https://huggingface.co/spaces/Sarvesh3529/Matrix-backend-python/resolve/main/server.py
1.18 kB
| from fastapi import FastAPI, UploadFile, File, Response | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from PIL import Image | |
| from rembg import remove | |
| import io | |
| import os | |
| import uvicorn | |
| app = FastAPI() | |
| # Enable CORS configurations to allow the internal node proxy to make pipeline hops | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def remove_background(file: UploadFile = File(...)): | |
| """ | |
| Removes the background from an uploaded image file. | |
| Expects a multipart/form-data upload with an image file. | |
| Returns a PNG image with a transparent background. | |
| """ | |
| contents = await file.read() | |
| input_image = Image.open(io.BytesIO(contents)).convert("RGBA") | |
| output_image = remove(input_image) | |
| output_buffer = io.BytesIO() | |
| output_image.save(output_buffer, format="PNG") | |
| return Response(content=output_buffer.getvalue(), media_type="image/png") | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 8080)) | |
| uvicorn.run("server:app", host="0.0.0.0", port=port) |