Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import torch
|
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
-
import base64
|
8 |
|
9 |
def remove_background(input_image):
|
10 |
try:
|
@@ -16,151 +15,115 @@ def remove_background(input_image):
|
|
16 |
|
17 |
# Process the image
|
18 |
result = segmentor(input_image)
|
19 |
-
|
20 |
-
# Convert the output image to PNG format
|
21 |
-
if isinstance(result['output_image'], Image.Image):
|
22 |
-
output = result['output_image']
|
23 |
-
else:
|
24 |
-
output = Image.fromarray(result['output_image'])
|
25 |
-
|
26 |
-
return output, output
|
27 |
-
|
28 |
except Exception as e:
|
29 |
raise gr.Error(f"Error processing image: {str(e)}")
|
30 |
|
31 |
-
|
32 |
-
if image is None:
|
33 |
-
return None
|
34 |
-
try:
|
35 |
-
# Save image to bytes
|
36 |
-
buffer = io.BytesIO()
|
37 |
-
image.save(buffer, format="PNG")
|
38 |
-
return buffer.getvalue()
|
39 |
-
except Exception as e:
|
40 |
-
raise gr.Error(f"Error saving image: {str(e)}")
|
41 |
-
|
42 |
css = """
|
43 |
.gradio-container {
|
44 |
font-family: 'Segoe UI', sans-serif;
|
45 |
-
|
|
|
46 |
}
|
47 |
-
.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
}
|
52 |
-
.
|
53 |
-
|
|
|
|
|
54 |
}
|
55 |
.gr-button {
|
56 |
background: linear-gradient(45deg, #FFD700, #FFA500);
|
57 |
-
border: none;
|
58 |
-
color: black;
|
59 |
-
padding:
|
60 |
-
border-radius: 8px;
|
61 |
-
font-weight: bold;
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
transform: translateY(-2px);
|
66 |
-
box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3);
|
67 |
}
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
margin: 1rem 0;
|
76 |
-
padding: 0.5rem;
|
77 |
-
border-radius: 8px;
|
78 |
-
}
|
79 |
-
.success {
|
80 |
-
background: rgba(0, 255, 0, 0.1);
|
81 |
-
color: #00ff00;
|
82 |
-
}
|
83 |
-
.error {
|
84 |
-
background: rgba(255, 0, 0, 0.1);
|
85 |
-
color: #ff0000;
|
86 |
}
|
87 |
"""
|
88 |
|
|
|
89 |
with gr.Blocks(css=css) as demo:
|
90 |
gr.HTML(
|
91 |
"""
|
92 |
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
|
93 |
-
<h1 style="font-size:
|
94 |
AI Background Remover
|
95 |
</h1>
|
96 |
-
<p style="color: #
|
97 |
-
Powered by RMBG V1.4 model
|
98 |
</p>
|
99 |
</div>
|
100 |
"""
|
101 |
)
|
102 |
|
103 |
-
with gr.Row():
|
104 |
with gr.Column():
|
|
|
105 |
input_image = gr.Image(
|
106 |
label="Upload Image",
|
107 |
type="pil",
|
108 |
sources=["upload", "clipboard"],
|
109 |
-
|
110 |
)
|
111 |
|
112 |
with gr.Column():
|
113 |
output_image = gr.Image(
|
114 |
label="Result",
|
115 |
-
type="pil"
|
|
|
116 |
)
|
117 |
-
download_btn = gr.Button("Download Result", visible=False)
|
118 |
|
119 |
with gr.Row():
|
120 |
-
clear_btn = gr.Button("Clear")
|
121 |
process_btn = gr.Button("Remove Background", variant="primary")
|
122 |
-
|
123 |
-
|
|
|
124 |
|
125 |
# Event handlers
|
126 |
-
def
|
127 |
if image is None:
|
128 |
-
|
129 |
try:
|
130 |
-
|
131 |
-
|
132 |
-
status.visible = True
|
133 |
-
status.value = "✅ Background removed successfully!"
|
134 |
-
return output, download, True, status
|
135 |
except Exception as e:
|
136 |
-
|
137 |
-
status.value = f"❌ Error: {str(e)}"
|
138 |
-
return None, None, False, status
|
139 |
-
|
140 |
-
def clear_all():
|
141 |
-
return {
|
142 |
-
input_image: None,
|
143 |
-
output_image: None,
|
144 |
-
download_btn: gr.Button(visible=False),
|
145 |
-
status: gr.Textbox(visible=False)
|
146 |
-
}
|
147 |
|
148 |
process_btn.click(
|
149 |
-
fn=
|
150 |
inputs=[input_image],
|
151 |
-
outputs=[output_image,
|
|
|
152 |
)
|
153 |
|
154 |
clear_btn.click(
|
155 |
-
fn=
|
156 |
-
outputs=[input_image, output_image,
|
157 |
-
|
158 |
-
|
159 |
-
# Download functionality
|
160 |
-
download_btn.click(
|
161 |
-
fn=save_image,
|
162 |
-
inputs=[output_image],
|
163 |
-
outputs=[gr.File(label="Download")]
|
164 |
)
|
165 |
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
import io
|
|
|
7 |
|
8 |
def remove_background(input_image):
|
9 |
try:
|
|
|
15 |
|
16 |
# Process the image
|
17 |
result = segmentor(input_image)
|
18 |
+
return result['output_image']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
except Exception as e:
|
20 |
raise gr.Error(f"Error processing image: {str(e)}")
|
21 |
|
22 |
+
# Custom CSS for mobile-friendly design
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
css = """
|
24 |
.gradio-container {
|
25 |
font-family: 'Segoe UI', sans-serif;
|
26 |
+
max-width: 100% !important;
|
27 |
+
padding: 10px !important;
|
28 |
}
|
29 |
+
.container {
|
30 |
+
display: flex;
|
31 |
+
flex-direction: column;
|
32 |
+
gap: 20px;
|
33 |
}
|
34 |
+
.image-container {
|
35 |
+
width: 100%;
|
36 |
+
max-width: 500px;
|
37 |
+
margin: 0 auto;
|
38 |
}
|
39 |
.gr-button {
|
40 |
background: linear-gradient(45deg, #FFD700, #FFA500);
|
41 |
+
border: none !important;
|
42 |
+
color: black !important;
|
43 |
+
padding: 12px 20px !important;
|
44 |
+
border-radius: 8px !important;
|
45 |
+
font-weight: bold !important;
|
46 |
+
margin: 10px 0 !important;
|
47 |
+
width: 100% !important;
|
48 |
+
max-width: 300px !important;
|
|
|
|
|
49 |
}
|
50 |
+
@media (max-width: 768px) {
|
51 |
+
.gradio-container {
|
52 |
+
padding: 5px !important;
|
53 |
+
}
|
54 |
+
.gr-button {
|
55 |
+
padding: 10px 15px !important;
|
56 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
58 |
"""
|
59 |
|
60 |
+
# Create Gradio interface
|
61 |
with gr.Blocks(css=css) as demo:
|
62 |
gr.HTML(
|
63 |
"""
|
64 |
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
|
65 |
+
<h1 style="font-size: 2rem; margin-bottom: 1rem; background: linear-gradient(45deg, #FFD700, #FFA500); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
|
66 |
AI Background Remover
|
67 |
</h1>
|
68 |
+
<p style="color: #666; font-size: 1rem; margin-bottom: 2rem;">
|
69 |
+
Powered by RMBG V1.4 model
|
70 |
</p>
|
71 |
</div>
|
72 |
"""
|
73 |
)
|
74 |
|
75 |
+
with gr.Row(equal_height=True):
|
76 |
with gr.Column():
|
77 |
+
# Removed 'tool' parameter and added mobile-friendly settings
|
78 |
input_image = gr.Image(
|
79 |
label="Upload Image",
|
80 |
type="pil",
|
81 |
sources=["upload", "clipboard"],
|
82 |
+
interactive=True
|
83 |
)
|
84 |
|
85 |
with gr.Column():
|
86 |
output_image = gr.Image(
|
87 |
label="Result",
|
88 |
+
type="pil",
|
89 |
+
interactive=False
|
90 |
)
|
|
|
91 |
|
92 |
with gr.Row():
|
93 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
94 |
process_btn = gr.Button("Remove Background", variant="primary")
|
95 |
+
|
96 |
+
# Status message for feedback
|
97 |
+
status_msg = gr.Textbox(label="Status", interactive=False, visible=False)
|
98 |
|
99 |
# Event handlers
|
100 |
+
def process_and_update(image):
|
101 |
if image is None:
|
102 |
+
return None, "Please upload an image first"
|
103 |
try:
|
104 |
+
result = remove_background(image)
|
105 |
+
return result, "Background removed successfully!"
|
|
|
|
|
|
|
106 |
except Exception as e:
|
107 |
+
return None, f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
process_btn.click(
|
110 |
+
fn=process_and_update,
|
111 |
inputs=[input_image],
|
112 |
+
outputs=[output_image, status_msg],
|
113 |
+
api_name="remove_background"
|
114 |
)
|
115 |
|
116 |
clear_btn.click(
|
117 |
+
fn=lambda: (None, None, ""),
|
118 |
+
outputs=[input_image, output_image, status_msg],
|
119 |
+
api_name="clear"
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
)
|
121 |
|
122 |
+
# Launch the app
|
123 |
+
demo.launch(
|
124 |
+
share=True,
|
125 |
+
enable_queue=True,
|
126 |
+
show_error=True,
|
127 |
+
server_port=7860,
|
128 |
+
server_name="0.0.0.0"
|
129 |
+
)
|