Rezuwan commited on
Commit
23a73fd
·
verified ·
1 Parent(s): 6385993

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -34
app.py CHANGED
@@ -1,22 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from rembg import remove
3
  from PIL import Image
4
- import io
5
- import os
6
 
7
- OUTPUT_FILE = "output.png"
 
 
8
 
9
- # Background removal function
10
  def remove_background(image):
11
- input_bytes = io.BytesIO()
12
- image.save(input_bytes, format='PNG')
13
- input_bytes = input_bytes.getvalue()
14
 
15
- output_bytes = remove(input_bytes)
16
- output_img = Image.open(io.BytesIO(output_bytes))
17
- output_img.save(OUTPUT_FILE, format="PNG")
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- return OUTPUT_FILE, "Background removed successfully!"
20
 
21
  # Example images
22
  example_images = [
@@ -25,15 +47,17 @@ example_images = [
25
  "examples/3.jpg",
26
  "examples/4.jpg",
27
  "examples/5.jpg",
28
- "examples/6.png"
29
  ]
30
 
31
  with gr.Blocks(title="Background Remover") as demo:
32
  gr.Markdown("# Background Remover")
33
- gr.Markdown("""
34
- Upload an image or use an example to remove its background.
35
- PS: Not the best image background remover out there but good enough for a free tool.
36
- """)
 
 
37
 
38
  with gr.Row():
39
  with gr.Column():
@@ -43,7 +67,7 @@ with gr.Blocks(title="Background Remover") as demo:
43
  with gr.Column():
44
  output_image = gr.Image(label="Output", type="filepath")
45
  output_text = gr.Textbox(label="Status", lines=1)
46
- download_html = gr.HTML(visible=False)
47
 
48
  with gr.Row():
49
  gr.Markdown("### Example Images")
@@ -57,22 +81,18 @@ with gr.Blocks(title="Background Remover") as demo:
57
  # Logic
58
  def process(img):
59
  output_path, msg = remove_background(img)
60
- download_button = f"""
61
- <a href="file/{output_path}" download style="
62
- display:inline-block;
63
- padding: 10px 20px;
64
- background-color: #4CAF50;
65
- color: white;
66
- text-align: center;
67
- text-decoration: none;
68
- border-radius: 5px;
69
- font-weight: bold;
70
- margin-top: 10px;
71
- ">⬇ Download Output</a>
72
- """
73
- return output_path, msg, gr.update(visible=True, value=download_button)
74
 
75
- submit_btn.click(fn=process, inputs=image_input, outputs=[output_image, output_text, download_html])
76
- clear_btn.click(fn=lambda: (None, "", gr.update(visible=False, value="")), outputs=[image_input, output_text, download_html])
 
 
 
77
 
78
- demo.launch()
 
 
1
+ import os
2
+ import io
3
+ import uuid
4
+ import tempfile
5
+
6
+ # rembg downloads its model (~176MB) to this directory on first use.
7
+ # Force it to a directory we know is writable inside the container,
8
+ # instead of relying on the default ~/.u2net (which can fail silently
9
+ # or hang retrying if $HOME isn't writable in this environment).
10
+ MODEL_CACHE_DIR = "/tmp/.u2net"
11
+ os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
12
+ os.environ.setdefault("U2NET_HOME", MODEL_CACHE_DIR)
13
+
14
  import gradio as gr
15
  from rembg import remove
16
  from PIL import Image
 
 
17
 
18
+ OUTPUT_DIR = "/tmp/bg_remove_outputs"
19
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
20
+
21
 
 
22
  def remove_background(image):
23
+ if image is None:
24
+ return None, "Please upload or select an image first."
 
25
 
26
+ try:
27
+ input_bytes = io.BytesIO()
28
+ image.save(input_bytes, format="PNG")
29
+ input_bytes = input_bytes.getvalue()
30
+
31
+ output_bytes = remove(input_bytes)
32
+ output_img = Image.open(io.BytesIO(output_bytes))
33
+
34
+ # unique filename per request so concurrent users never collide
35
+ output_path = os.path.join(OUTPUT_DIR, f"{uuid.uuid4().hex}.png")
36
+ output_img.save(output_path, format="PNG")
37
+
38
+ return output_path, "Background removed successfully!"
39
+ except Exception as e:
40
+ return None, f"Something went wrong while removing the background: {e}"
41
 
 
42
 
43
  # Example images
44
  example_images = [
 
47
  "examples/3.jpg",
48
  "examples/4.jpg",
49
  "examples/5.jpg",
50
+ "examples/6.png",
51
  ]
52
 
53
  with gr.Blocks(title="Background Remover") as demo:
54
  gr.Markdown("# Background Remover")
55
+ gr.Markdown(
56
+ """
57
+ Upload an image or use an example to remove its background.
58
+ PS: Not the best image background remover out there but good enough for a free tool.
59
+ """
60
+ )
61
 
62
  with gr.Row():
63
  with gr.Column():
 
67
  with gr.Column():
68
  output_image = gr.Image(label="Output", type="filepath")
69
  output_text = gr.Textbox(label="Status", lines=1)
70
+ download_file = gr.File(label="Download Output", visible=False)
71
 
72
  with gr.Row():
73
  gr.Markdown("### Example Images")
 
81
  # Logic
82
  def process(img):
83
  output_path, msg = remove_background(img)
84
+ has_output = output_path is not None
85
+ return (
86
+ output_path,
87
+ msg,
88
+ gr.update(visible=has_output, value=output_path if has_output else None),
89
+ )
 
 
 
 
 
 
 
 
90
 
91
+ submit_btn.click(fn=process, inputs=image_input, outputs=[output_image, output_text, download_file])
92
+ clear_btn.click(
93
+ fn=lambda: (None, "", gr.update(visible=False, value=None)),
94
+ outputs=[output_image, output_text, download_file],
95
+ )
96
 
97
+ if __name__ == "__main__":
98
+ demo.launch()