Janeka commited on
Commit
2adeb8a
·
verified ·
1 Parent(s): 7ee2dd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -35
app.py CHANGED
@@ -1,46 +1,24 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
- from fastapi.responses import StreamingResponse
3
  from rembg import remove
 
4
  from io import BytesIO
5
- import gradio as gr
6
-
7
- # Create FastAPI app
8
- app = FastAPI()
9
 
10
- # Background removal function
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=gradio_interface,
28
- inputs=gr.Image(type="filepath", label="Input Image"),
29
- outputs=gr.Image(type="numpy", label="Output Image"),
30
  title="Background Remover",
31
  description="Upload an image to remove the background"
32
  )
33
 
34
- # API endpoint
 
35
  @app.post("/api/remove-bg")
36
- async def api_remove_bg(file: UploadFile = File(...)):
37
- if not file.content_type.startswith('image/'):
38
- raise HTTPException(status_code=400, detail="File must be an image")
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 app to FastAPI
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="/")