Janeka commited on
Commit
317e696
·
verified ·
1 Parent(s): 115f928

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -16
app.py CHANGED
@@ -1,29 +1,79 @@
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
 
6
- # Create apps
7
  app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
8
  gradio_app = gr.Interface(
9
- fn=lambda img: remove(img),
10
- inputs=gr.Image(type="filepath"),
11
- outputs=gr.Image(type="filepath"),
12
  title="Background Remover",
13
  description="Upload an image to remove the background",
14
- allow_flagging="never" # Disable flagging feature
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="/")
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from rembg import remove
3
+ from fastapi import FastAPI
4
+ from fastapi.staticfiles import StaticFiles
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: bytes = File(...)):
 
 
 
 
36
  try:
37
+ output_bytes = remove(file)
38
+ return Response(content=output_bytes, media_type="image/png")
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))