Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
|
| 7 |
-
# Create
|
|
|
|
| 8 |
gradio_app = gr.Interface(
|
| 9 |
fn=lambda img: remove(img),
|
| 10 |
-
inputs=gr.Image(type="
|
| 11 |
-
outputs=gr.Image(type="
|
| 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:
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# Mount Gradio to the root path
|
| 24 |
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 4 |
+
from fastapi.responses import Response
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Create apps
|
| 8 |
+
app = FastAPI()
|
| 9 |
gradio_app = gr.Interface(
|
| 10 |
fn=lambda img: remove(img),
|
| 11 |
+
inputs=gr.Image(type="filepath"),
|
| 12 |
+
outputs=gr.Image(type="filepath"),
|
| 13 |
title="Background Remover",
|
| 14 |
description="Upload an image to remove the background"
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
@app.post("/api/remove-bg")
|
| 18 |
+
async def api_remove_bg(file: UploadFile = File(...)):
|
| 19 |
+
if not file.filename.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 20 |
+
raise HTTPException(400, "Only PNG/JPG images allowed")
|
| 21 |
+
|
| 22 |
+
image_bytes = await file.read()
|
| 23 |
+
try:
|
| 24 |
+
output = remove(image_bytes)
|
| 25 |
+
return Response(content=output, media_type="image/png")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise HTTPException(500, f"Error processing image: {str(e)}")
|
| 28 |
|
|
|
|
| 29 |
app = gr.mount_gradio_app(app, gradio_app, path="/")
|