Janeka commited on
Commit
7c0a7f8
·
verified ·
1 Parent(s): 89ec0dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -1,24 +1,29 @@
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="/")
 
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="/")