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
import gradio as gr
|
| 3 |
+
from rembg import remove
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
# Background remover function with error handling
|
| 8 |
+
def remove_background(image: Image.Image) -> Image.Image:
|
| 9 |
+
try:
|
| 10 |
+
# Convert image to bytes
|
| 11 |
+
with io.BytesIO() as buffered:
|
| 12 |
+
image.save(buffered, format="PNG", optimize=True)
|
| 13 |
+
img_bytes = buffered.getvalue()
|
| 14 |
+
|
| 15 |
+
# Process image with error handling
|
| 16 |
+
output_bytes = remove(
|
| 17 |
+
img_bytes,
|
| 18 |
+
alpha_matting=True,
|
| 19 |
+
alpha_matting_foreground_threshold=240,
|
| 20 |
+
alpha_matting_background_threshold=10,
|
| 21 |
+
alpha_matting_erode_size=10
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
output_image = Image.open(io.BytesIO(output_bytes)).convert("RGBA")
|
| 25 |
+
return output_image
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Error processing image: {str(e)}")
|
| 29 |
+
raise gr.Error("Failed to process image. Please try another image.")
|
| 30 |
+
|
| 31 |
+
# Example Images
|
| 32 |
+
examples = [
|
| 33 |
+
["https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Cat_August_2010-4.jpg/480px-Cat_August_2010-4.jpg"],
|
| 34 |
+
["https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cat_November_2010-1a.jpg/480px-Cat_November_2010-1a.jpg"],
|
| 35 |
+
["https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Internet1.jpg/640px-Internet1.jpg"]
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
# Custom CSS for professional look
|
| 39 |
+
custom_css = """
|
| 40 |
+
:root {
|
| 41 |
+
--primary: #4f46e5;
|
| 42 |
+
--secondary: #f9fafb;
|
| 43 |
+
}
|
| 44 |
+
body {
|
| 45 |
+
font-family: 'Inter', sans-serif;
|
| 46 |
+
}
|
| 47 |
+
.gradio-container {
|
| 48 |
+
max-width: 900px !important;
|
| 49 |
+
margin: auto !important;
|
| 50 |
+
}
|
| 51 |
+
.dark {
|
| 52 |
+
--primary: #6366f1;
|
| 53 |
+
--secondary: #1e293b;
|
| 54 |
+
}
|
| 55 |
+
footer {
|
| 56 |
+
display: none !important;
|
| 57 |
+
}
|
| 58 |
+
.processing-message {
|
| 59 |
+
color: var(--primary);
|
| 60 |
+
font-weight: bold;
|
| 61 |
+
}
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
# Social Media & Credits
|
| 65 |
+
socials = """
|
| 66 |
+
<div class="social-container">
|
| 67 |
+
<h3>About AI ClearCut</h3>
|
| 68 |
+
<p>Professional background removal tool powered by AI computer vision.</p>
|
| 69 |
+
|
| 70 |
+
<div class="credits">
|
| 71 |
+
<h4>Technical Details</h4>
|
| 72 |
+
<p>Powered by <a href="https://github.com/danielgatis/rembg" target="_blank">Rembg</a> • UI by Gradio</p>
|
| 73 |
+
<p>Model: u2net • Processing: CPU optimized</p>
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
<div class="developer">
|
| 77 |
+
<h4>Developer</h4>
|
| 78 |
+
<div class="links">
|
| 79 |
+
<a href="https://github.com/prokaiiddo" target="_blank">GitHub</a> •
|
| 80 |
+
<a href="https://linkedin.com/in/kaiiddo" target="_blank">LinkedIn</a> •
|
| 81 |
+
<a href="https://twitter.com/kaiiddo" target="_blank">Twitter</a>
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
</div>
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
# Gradio Interface
|
| 88 |
+
with gr.Blocks(title="AI ClearCut - Professional Background Remover", css=custom_css) as interface:
|
| 89 |
+
gr.Markdown("""
|
| 90 |
+
# <center>AI ClearCut</center>
|
| 91 |
+
### <center>Professional Background Removal Tool</center>
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
input_image = gr.Image(label="Upload Image", type="pil", elem_id="input-image")
|
| 97 |
+
with gr.Row():
|
| 98 |
+
submit_btn = gr.Button("Remove Background", variant="primary")
|
| 99 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 100 |
+
gr.Examples(
|
| 101 |
+
examples=examples,
|
| 102 |
+
inputs=[input_image],
|
| 103 |
+
label="Example Images",
|
| 104 |
+
examples_per_page=3
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
with gr.Column():
|
| 108 |
+
output_image = gr.Image(label="Result with Transparent Background", type="pil", elem_id="output-image")
|
| 109 |
+
with gr.Row():
|
| 110 |
+
download_btn = gr.Button("Download PNG")
|
| 111 |
+
download_jpg = gr.Button("Download JPG (with white bg)")
|
| 112 |
+
|
| 113 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 114 |
+
with gr.Row():
|
| 115 |
+
alpha_matting = gr.Checkbox(label="Enable Alpha Matting (better for hair/fur)", value=True)
|
| 116 |
+
with gr.Column(visible=False) as adv_options:
|
| 117 |
+
foreground_thresh = gr.Slider(0, 255, value=240, label="Foreground Threshold")
|
| 118 |
+
background_thresh = gr.Slider(0, 255, value=10, label="Background Threshold")
|
| 119 |
+
erode_size = gr.Slider(0, 40, value=10, label="Erode Size")
|
| 120 |
+
|
| 121 |
+
with gr.Accordion("About & Credits", open=False):
|
| 122 |
+
gr.Markdown(socials)
|
| 123 |
+
|
| 124 |
+
# Show/hide advanced options
|
| 125 |
+
alpha_matting.change(
|
| 126 |
+
fn=lambda x: gr.Column(visible=x),
|
| 127 |
+
inputs=[alpha_matting],
|
| 128 |
+
outputs=[adv_options]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Process image
|
| 132 |
+
submit_btn.click(
|
| 133 |
+
fn=remove_background,
|
| 134 |
+
inputs=[input_image],
|
| 135 |
+
outputs=[output_image],
|
| 136 |
+
api_name="remove_bg"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Download handlers
|
| 140 |
+
download_btn.click(
|
| 141 |
+
fn=lambda x: x,
|
| 142 |
+
inputs=[output_image],
|
| 143 |
+
outputs=[gr.File(label="Downloading...")],
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
download_jpg.click(
|
| 147 |
+
fn=lambda x: x.convert("RGB") if x else None,
|
| 148 |
+
inputs=[output_image],
|
| 149 |
+
outputs=[gr.File(label="Downloading JPG...")],
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# Clear button
|
| 153 |
+
clear_btn.click(
|
| 154 |
+
fn=lambda: [None, None],
|
| 155 |
+
outputs=[input_image, output_image]
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Simplified launch for Hugging Face Spaces
|
| 159 |
+
interface.launch()
|