Update app.py
Browse files
app.py
CHANGED
|
@@ -8,121 +8,149 @@ import io
|
|
| 8 |
def remove_background(input_image):
|
| 9 |
try:
|
| 10 |
# Initialize the pipeline with trust_remote_code=True
|
| 11 |
-
segmentor = pipeline(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Process the image
|
| 17 |
-
result = segmentor(input_image)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
raise gr.Error(f"Error processing image: {str(e)}")
|
| 21 |
|
| 22 |
-
# Custom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
css = """
|
| 24 |
.gradio-container {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
padding:
|
| 28 |
-
}
|
| 29 |
-
.container {
|
| 30 |
-
display: flex;
|
| 31 |
-
flex-direction: column;
|
| 32 |
-
gap: 20px;
|
| 33 |
}
|
| 34 |
.image-container {
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
.gr-button {
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 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;
|
| 65 |
-
<h1 style="font-size:
|
| 66 |
-
AI Background Remover
|
| 67 |
</h1>
|
| 68 |
-
<p style="color: #
|
| 69 |
-
|
| 70 |
</p>
|
| 71 |
</div>
|
| 72 |
"""
|
| 73 |
)
|
| 74 |
|
| 75 |
-
with gr.Row(
|
| 76 |
with gr.Column():
|
| 77 |
input_image = gr.Image(
|
| 78 |
-
label="Upload Image",
|
| 79 |
type="pil",
|
| 80 |
-
|
| 81 |
-
interactive=True
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with gr.Column():
|
| 85 |
output_image = gr.Image(
|
| 86 |
label="Result",
|
| 87 |
type="pil",
|
| 88 |
-
|
| 89 |
)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
# Event handlers
|
| 99 |
def process_and_update(image):
|
| 100 |
if image is None:
|
| 101 |
-
return None, "Please upload an image first"
|
| 102 |
try:
|
| 103 |
result = remove_background(image)
|
| 104 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
-
return None, f"Error: {str(e)}"
|
| 107 |
|
| 108 |
process_btn.click(
|
| 109 |
fn=process_and_update,
|
| 110 |
inputs=[input_image],
|
| 111 |
-
outputs=[output_image, status_msg],
|
| 112 |
-
concurrency_limit=1, # Limit concurrent processing
|
| 113 |
-
show_progress=True # Show processing progress
|
| 114 |
)
|
| 115 |
|
| 116 |
clear_btn.click(
|
| 117 |
-
fn=lambda: (None, None, ""),
|
| 118 |
-
outputs=[input_image, output_image, status_msg],
|
| 119 |
-
queue=False # No need for queue in clear operation
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def remove_background(input_image):
|
| 9 |
try:
|
| 10 |
# Initialize the pipeline with trust_remote_code=True
|
| 11 |
+
segmentor = pipeline(
|
| 12 |
+
"image-segmentation",
|
| 13 |
+
model="briaai/RMBG-1.4",
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
device="cpu"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
# Process the image and get result
|
| 19 |
+
result = segmentor(input_image, return_mask=True)
|
| 20 |
+
|
| 21 |
+
# Convert result to RGBA
|
| 22 |
+
if isinstance(result, Image.Image):
|
| 23 |
+
# Create transparent background
|
| 24 |
+
output = Image.new('RGBA', result.size, (0, 0, 0, 0))
|
| 25 |
+
output.paste(input_image, mask=result)
|
| 26 |
+
else:
|
| 27 |
+
output = result['output_image']
|
| 28 |
+
|
| 29 |
+
return output
|
| 30 |
+
|
| 31 |
except Exception as e:
|
| 32 |
raise gr.Error(f"Error processing image: {str(e)}")
|
| 33 |
|
| 34 |
+
# Custom theme and styling
|
| 35 |
+
theme = gr.themes.Soft(
|
| 36 |
+
primary_hue="gold",
|
| 37 |
+
secondary_hue="orange",
|
| 38 |
+
).set(
|
| 39 |
+
body_background_fill="linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%)",
|
| 40 |
+
body_text_color="#ffffff",
|
| 41 |
+
button_primary_background_fill="linear-gradient(45deg, #FFD700, #FFA500)",
|
| 42 |
+
button_primary_text_color="#000000",
|
| 43 |
+
border_color_primary="#FFD700"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
css = """
|
| 47 |
.gradio-container {
|
| 48 |
+
max-width: 1200px !important;
|
| 49 |
+
margin: 0 auto !important;
|
| 50 |
+
padding: 20px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
.image-container {
|
| 53 |
+
border-radius: 15px !important;
|
| 54 |
+
border: 2px solid rgba(255, 215, 0, 0.3) !important;
|
| 55 |
+
padding: 10px !important;
|
| 56 |
+
background: rgba(255, 255, 255, 0.1) !important;
|
| 57 |
+
transition: transform 0.3s ease !important;
|
| 58 |
+
}
|
| 59 |
+
.image-container:hover {
|
| 60 |
+
transform: translateY(-5px) !important;
|
| 61 |
}
|
| 62 |
.gr-button {
|
| 63 |
+
min-width: 200px !important;
|
| 64 |
+
height: 45px !important;
|
| 65 |
+
font-size: 16px !important;
|
| 66 |
+
margin: 10px !important;
|
| 67 |
+
transition: all 0.3s ease !important;
|
| 68 |
+
}
|
| 69 |
+
.gr-button:hover {
|
| 70 |
+
transform: translateY(-2px) !important;
|
| 71 |
+
box-shadow: 0 5px 15px rgba(255, 215, 0, 0.3) !important;
|
| 72 |
}
|
| 73 |
+
.footer {
|
| 74 |
+
text-align: center;
|
| 75 |
+
margin-top: 20px;
|
| 76 |
+
color: #666;
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
"""
|
| 79 |
|
| 80 |
# Create Gradio interface
|
| 81 |
+
with gr.Blocks(theme=theme, css=css) as demo:
|
| 82 |
gr.HTML(
|
| 83 |
"""
|
| 84 |
+
<div style="text-align: center; margin-bottom: 2rem;">
|
| 85 |
+
<h1 style="font-size: 3rem; margin-bottom: 1rem; background: linear-gradient(45deg, #FFD700, #FFA500); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
|
| 86 |
+
AI Background Remover Pro
|
| 87 |
</h1>
|
| 88 |
+
<p style="color: #cccccc; font-size: 1.2rem;">
|
| 89 |
+
Remove backgrounds instantly using advanced AI technology
|
| 90 |
</p>
|
| 91 |
</div>
|
| 92 |
"""
|
| 93 |
)
|
| 94 |
|
| 95 |
+
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
input_image = gr.Image(
|
| 98 |
+
label="Upload Your Image",
|
| 99 |
type="pil",
|
| 100 |
+
elem_classes="image-container"
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
+
with gr.Row():
|
| 104 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 105 |
+
process_btn = gr.Button("Remove Background", variant="primary")
|
| 106 |
+
download_btn = gr.Button("Download", variant="primary", visible=False)
|
| 107 |
+
|
| 108 |
with gr.Column():
|
| 109 |
output_image = gr.Image(
|
| 110 |
label="Result",
|
| 111 |
type="pil",
|
| 112 |
+
elem_classes="image-container"
|
| 113 |
)
|
| 114 |
+
|
| 115 |
+
# Status message
|
| 116 |
+
status_msg = gr.Textbox(
|
| 117 |
+
label="Status",
|
| 118 |
+
placeholder="Ready to process your image...",
|
| 119 |
+
interactive=False
|
| 120 |
+
)
|
| 121 |
|
| 122 |
+
# Event handlers
|
| 123 |
def process_and_update(image):
|
| 124 |
if image is None:
|
| 125 |
+
return None, "Please upload an image first", gr.Button.update(visible=False)
|
| 126 |
try:
|
| 127 |
result = remove_background(image)
|
| 128 |
+
return (
|
| 129 |
+
result,
|
| 130 |
+
"✨ Background removed successfully!",
|
| 131 |
+
gr.Button.update(visible=True)
|
| 132 |
+
)
|
| 133 |
except Exception as e:
|
| 134 |
+
return None, f"❌ Error: {str(e)}", gr.Button.update(visible=False)
|
| 135 |
|
| 136 |
process_btn.click(
|
| 137 |
fn=process_and_update,
|
| 138 |
inputs=[input_image],
|
| 139 |
+
outputs=[output_image, status_msg, download_btn],
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
clear_btn.click(
|
| 143 |
+
fn=lambda: (None, None, "Ready to process your image...", gr.Button.update(visible=False)),
|
| 144 |
+
outputs=[input_image, output_image, status_msg, download_btn],
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
+
gr.HTML(
|
| 148 |
+
"""
|
| 149 |
+
<div class="footer">
|
| 150 |
+
<p>Powered by BRIA AI's RMBG V1.4 Model</p>
|
| 151 |
+
</div>
|
| 152 |
+
"""
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Launch the app
|
| 156 |
+
demo.launch()
|