Janeka commited on
Commit
053dcb6
·
verified ·
1 Parent(s): 5409b5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -4,38 +4,38 @@ from fastapi import FastAPI, UploadFile, File, HTTPException
4
  from fastapi.responses import Response
5
  import os
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # Create FastAPI app
8
  app = FastAPI()
9
 
10
- # Background removal function
11
- def remove_bg(image_path):
12
- with open(image_path, "rb") as f:
13
- input_image = f.read()
14
- output_image = remove(input_image)
15
- output_path = "/tmp/output.png"
16
- with open(output_path, "wb") as f:
17
- f.write(output_image)
18
- return output_path
19
-
20
- # Create Gradio interface
21
  gradio_app = gr.Interface(
22
- fn=remove_bg,
23
- inputs=gr.Image(type="filepath", label="Input Image"),
24
- outputs=gr.Image(type="filepath", label="Output Image"),
25
  title="Background Remover",
26
  description="Upload an image to remove the background",
27
  allow_flagging="never"
28
  )
29
 
30
- # Mount Gradio app
31
- app.mount("/", gradio_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))
 
4
  from fastapi.responses import Response
5
  import os
6
 
7
+ # Create background removal function
8
+ def remove_background(image_bytes):
9
+ try:
10
+ return remove(image_bytes)
11
+ except Exception as e:
12
+ raise HTTPException(status_code=500, detail=str(e))
13
+
14
+ # Create Gradio interface
15
+ def gradio_interface(input_image):
16
+ if input_image is None:
17
+ return None
18
+ return remove_background(input_image)
19
+
20
  # Create FastAPI app
21
  app = FastAPI()
22
 
23
+ # Create and mount Gradio app
 
 
 
 
 
 
 
 
 
 
24
  gradio_app = gr.Interface(
25
+ fn=gradio_interface,
26
+ inputs=gr.Image(type="filepath"),
27
+ outputs=gr.Image(type="filepath"),
28
  title="Background Remover",
29
  description="Upload an image to remove the background",
30
  allow_flagging="never"
31
  )
32
 
 
 
 
33
  # API endpoint
34
  @app.post("/api/remove-bg")
35
  async def api_remove_bg(file: UploadFile = File(...)):
36
+ image_bytes = await file.read()
37
+ output_bytes = remove_background(image_bytes)
38
+ return Response(content=output_bytes, media_type="image/png")
39
+
40
+ # Mount Gradio app
41
+ app = gr.mount_gradio_app(app, gradio_app, path="/")