import os import subprocess from fastapi import FastAPI, UploadFile, File from fastapi.responses import FileResponse from fastapi.middleware.cors import CORSMiddleware app = FastAPI() # Set up CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], # Allows all origins allow_credentials=True, allow_methods=["*"], # Allows all methods allow_headers=["*"], # Allows all headers ) @app.post("/experiments/remove-background/") async def remove_background(file: UploadFile = File(...)): input_path = f"temp/{file.filename}" output_path = f"temp/no-bg-{file.filename}" # Ensure the temp folder exists os.makedirs(os.path.dirname(input_path), exist_ok=True) # Save the uploaded file with open(input_path, "wb") as buffer: buffer.write(await file.read()) # Run rembg command subprocess.run(["rembg", "i", input_path, output_path], check=True) # Return the processed file return FileResponse(output_path) # import subprocess # subprocess.run(["rembg", "i", input_path, output_path], check=True)