Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import StreamingResponse
|
| 3 |
from rembg import remove
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
# Create FastAPI app
|
| 8 |
-
app = FastAPI()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def remove_background(image_bytes):
|
| 12 |
-
try:
|
| 13 |
-
output_bytes = remove(image_bytes)
|
| 14 |
-
return output_bytes
|
| 15 |
-
except Exception as e:
|
| 16 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 17 |
-
|
| 18 |
-
# Gradio interface
|
| 19 |
-
def gradio_interface(input_image):
|
| 20 |
-
if input_image is None:
|
| 21 |
-
return None
|
| 22 |
-
output_bytes = remove_background(input_image)
|
| 23 |
-
return output_bytes
|
| 24 |
-
|
| 25 |
-
# Create Gradio app
|
| 26 |
gradio_app = gr.Interface(
|
| 27 |
-
fn=
|
| 28 |
-
inputs=gr.Image(type="
|
| 29 |
-
outputs=gr.Image(type="numpy"
|
| 30 |
title="Background Remover",
|
| 31 |
description="Upload an image to remove the background"
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
@app.post("/api/remove-bg")
|
| 36 |
-
async def api_remove_bg(file:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
input_image = await file.read()
|
| 41 |
-
output_bytes = remove_background(input_image)
|
| 42 |
-
|
| 43 |
-
return StreamingResponse(BytesIO(output_bytes), media_type="image/png")
|
| 44 |
|
| 45 |
-
# Mount Gradio
|
| 46 |
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from rembg import remove
|
| 3 |
+
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
+
from fastapi import FastAPI, Response
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Create both Gradio and FastAPI apps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
gradio_app = gr.Interface(
|
| 9 |
+
fn=lambda img: remove(img),
|
| 10 |
+
inputs=gr.Image(type="numpy"),
|
| 11 |
+
outputs=gr.Image(type="numpy"),
|
| 12 |
title="Background Remover",
|
| 13 |
description="Upload an image to remove the background"
|
| 14 |
)
|
| 15 |
|
| 16 |
+
app = FastAPI()
|
| 17 |
+
|
| 18 |
@app.post("/api/remove-bg")
|
| 19 |
+
async def api_remove_bg(file: bytes = File(...)):
|
| 20 |
+
output_bytes = remove(file)
|
| 21 |
+
return Response(content=output_bytes, media_type="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Mount Gradio to the root path
|
| 24 |
app = gr.mount_gradio_app(app, gradio_app, path="/")
|