Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,44 +58,44 @@ async def root():
|
|
| 58 |
|
| 59 |
# FIXED: Accept both UploadFile and raw bytes
|
| 60 |
@app.post("/remove-bg")
|
| 61 |
-
async def remove_background(
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
if not body:
|
| 68 |
-
raise HTTPException(400, detail="No image data received")
|
| 69 |
-
image = Image.open(io.BytesIO(body))
|
| 70 |
-
else:
|
| 71 |
contents = await file.read()
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
raise HTTPException(400, detail="Invalid image format")
|
| 79 |
|
| 80 |
-
# Process
|
| 81 |
result = remove_bg(image)
|
| 82 |
|
| 83 |
-
# Return PNG
|
| 84 |
img_byte_arr = io.BytesIO()
|
| 85 |
result.save(img_byte_arr, format="PNG")
|
| 86 |
img_byte_arr.seek(0)
|
| 87 |
|
|
|
|
| 88 |
return StreamingResponse(
|
| 89 |
img_byte_arr,
|
| 90 |
media_type="image/png",
|
| 91 |
-
headers={
|
| 92 |
-
"Content-Disposition": "inline; filename=removed-bg.png",
|
| 93 |
-
"Cache-Control": "public, max-age=3600"
|
| 94 |
-
}
|
| 95 |
)
|
| 96 |
except Exception as e:
|
| 97 |
-
print(f"❌
|
| 98 |
-
raise HTTPException(
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 58 |
|
| 59 |
# FIXED: Accept both UploadFile and raw bytes
|
| 60 |
@app.post("/remove-bg")
|
| 61 |
+
async def remove_background(
|
| 62 |
+
file: UploadFile = File(None),
|
| 63 |
+
request: Request = None
|
| 64 |
+
):
|
| 65 |
try:
|
| 66 |
+
contents = None
|
| 67 |
+
|
| 68 |
+
# Try UploadFile first
|
| 69 |
+
if file:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
contents = await file.read()
|
| 71 |
+
print(f"✅ File received: {file.filename}, {len(contents)} bytes")
|
| 72 |
+
else:
|
| 73 |
+
# Fallback: check raw body
|
| 74 |
+
body = await request.body()
|
| 75 |
+
contents = body
|
| 76 |
+
print(f"✅ Raw body received: {len(contents)} bytes")
|
| 77 |
+
|
| 78 |
+
if not contents or len(contents) == 0:
|
| 79 |
+
raise HTTPException(400, detail="No file uploaded")
|
| 80 |
|
| 81 |
+
image = Image.open(io.BytesIO(contents))
|
| 82 |
+
print(f"✅ Image loaded: {image.size}, format: {image.format}")
|
|
|
|
| 83 |
|
|
|
|
| 84 |
result = remove_bg(image)
|
| 85 |
|
|
|
|
| 86 |
img_byte_arr = io.BytesIO()
|
| 87 |
result.save(img_byte_arr, format="PNG")
|
| 88 |
img_byte_arr.seek(0)
|
| 89 |
|
| 90 |
+
print("✅ Processing complete")
|
| 91 |
return StreamingResponse(
|
| 92 |
img_byte_arr,
|
| 93 |
media_type="image/png",
|
| 94 |
+
headers={"Content-Disposition": "inline; filename=removed-bg.png"}
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f"❌ Detailed error: {str(e)}")
|
| 98 |
+
raise HTTPException(400, detail=f"Processing failed: {str(e)}")
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|