Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from fastapi.
|
| 5 |
import os
|
| 6 |
|
| 7 |
# Create FastAPI app
|
|
@@ -32,48 +32,10 @@ app.mount("/", gradio_app)
|
|
| 32 |
|
| 33 |
# API endpoint
|
| 34 |
@app.post("/api/remove-bg")
|
| 35 |
-
async def api_remove_bg(file:
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
except Exception as e:
|
| 40 |
-
raise HTTPException(status_code=500, detail=str(e))import gradio as gr
|
| 41 |
-
from rembg import remove
|
| 42 |
-
from fastapi import FastAPI
|
| 43 |
-
from fastapi.staticfiles import StaticFiles
|
| 44 |
-
import os
|
| 45 |
-
|
| 46 |
-
# Create FastAPI app
|
| 47 |
-
app = FastAPI()
|
| 48 |
-
|
| 49 |
-
# Background removal function
|
| 50 |
-
def remove_bg(image_path):
|
| 51 |
-
with open(image_path, "rb") as f:
|
| 52 |
-
input_image = f.read()
|
| 53 |
-
output_image = remove(input_image)
|
| 54 |
-
output_path = "/tmp/output.png"
|
| 55 |
-
with open(output_path, "wb") as f:
|
| 56 |
-
f.write(output_image)
|
| 57 |
-
return output_path
|
| 58 |
-
|
| 59 |
-
# Create Gradio interface
|
| 60 |
-
gradio_app = gr.Interface(
|
| 61 |
-
fn=remove_bg,
|
| 62 |
-
inputs=gr.Image(type="filepath", label="Input Image"),
|
| 63 |
-
outputs=gr.Image(type="filepath", label="Output Image"),
|
| 64 |
-
title="Background Remover",
|
| 65 |
-
description="Upload an image to remove the background",
|
| 66 |
-
allow_flagging="never"
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# Mount Gradio app
|
| 70 |
-
app.mount("/", gradio_app)
|
| 71 |
-
|
| 72 |
-
# API endpoint
|
| 73 |
-
@app.post("/api/remove-bg")
|
| 74 |
-
async def api_remove_bg(file: bytes = File(...)):
|
| 75 |
-
try:
|
| 76 |
-
output_bytes = remove(file)
|
| 77 |
return Response(content=output_bytes, media_type="image/png")
|
| 78 |
except Exception as e:
|
| 79 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 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 FastAPI app
|
|
|
|
| 32 |
|
| 33 |
# API endpoint
|
| 34 |
@app.post("/api/remove-bg")
|
| 35 |
+
async def api_remove_bg(file: UploadFile = File(...)):
|
| 36 |
try:
|
| 37 |
+
input_image = await file.read()
|
| 38 |
+
output_bytes = remove(input_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return Response(content=output_bytes, media_type="image/png")
|
| 40 |
except Exception as e:
|
| 41 |
raise HTTPException(status_code=500, detail=str(e))
|