Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
| 26 |
|
| 27 |
def process(image, progress=gr.Progress()):
|
| 28 |
if image is None:
|
| 29 |
-
return None, None
|
| 30 |
try:
|
| 31 |
progress(0, desc="Starting processing...")
|
| 32 |
orig_image = Image.fromarray(image)
|
|
@@ -71,94 +71,94 @@ def process(image, progress=gr.Progress()):
|
|
| 71 |
new_im.save(filepath, format='PNG', quality=100)
|
| 72 |
|
| 73 |
# Convert to numpy array for display
|
| 74 |
-
output_array = np.array(new_im)
|
| 75 |
-
|
| 76 |
-
# Create auto-download HTML
|
| 77 |
-
auto_download_html = f"""
|
| 78 |
-
<script>
|
| 79 |
-
setTimeout(function() {{
|
| 80 |
-
const link = document.createElement('a');
|
| 81 |
-
link.href = '/file={filepath}';
|
| 82 |
-
link.download = '{filename}';
|
| 83 |
-
document.body.appendChild(link);
|
| 84 |
-
link.click();
|
| 85 |
-
document.body.removeChild(link);
|
| 86 |
-
}}, 1000);
|
| 87 |
-
</script>
|
| 88 |
-
"""
|
| 89 |
|
| 90 |
progress(1.0, desc="Done!")
|
| 91 |
-
return output_array,
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error processing image: {str(e)}")
|
| 95 |
-
return None, None
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
.
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
gr.Markdown("""
|
| 163 |
<h1 class="title-text">AI Background Removal</h1>
|
| 164 |
<p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
|
|
@@ -180,17 +180,39 @@ with gr.Blocks(css="""
|
|
| 180 |
)
|
| 181 |
|
| 182 |
download_button = gr.File(
|
| 183 |
-
label="Download
|
| 184 |
visible=True,
|
| 185 |
-
elem_classes="download-btn"
|
|
|
|
| 186 |
)
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
input_image.change(
|
| 191 |
fn=process,
|
| 192 |
inputs=input_image,
|
| 193 |
-
outputs=[output_image, download_button
|
| 194 |
)
|
| 195 |
|
| 196 |
if __name__ == "__main__":
|
|
|
|
| 26 |
|
| 27 |
def process(image, progress=gr.Progress()):
|
| 28 |
if image is None:
|
| 29 |
+
return None, None
|
| 30 |
try:
|
| 31 |
progress(0, desc="Starting processing...")
|
| 32 |
orig_image = Image.fromarray(image)
|
|
|
|
| 71 |
new_im.save(filepath, format='PNG', quality=100)
|
| 72 |
|
| 73 |
# Convert to numpy array for display
|
| 74 |
+
output_array = np.array(new_im.convert('RGBA'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
progress(1.0, desc="Done!")
|
| 77 |
+
return output_array, gr.update(value=filepath, visible=True)
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error processing image: {str(e)}")
|
| 81 |
+
return None, None
|
| 82 |
|
| 83 |
+
css = """
|
| 84 |
+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
|
| 85 |
+
|
| 86 |
+
.title-text {
|
| 87 |
+
color: #ff00de;
|
| 88 |
+
font-family: 'Orbitron', sans-serif;
|
| 89 |
+
font-size: 2.5em;
|
| 90 |
+
text-align: center;
|
| 91 |
+
margin: 20px 0;
|
| 92 |
+
text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
|
| 93 |
+
animation: glow 2s ease-in-out infinite alternate;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
.subtitle-text {
|
| 97 |
+
color: #00ffff;
|
| 98 |
+
text-align: center;
|
| 99 |
+
margin-bottom: 30px;
|
| 100 |
+
font-size: 1.2em;
|
| 101 |
+
text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
.image-container {
|
| 105 |
+
background: rgba(10, 10, 30, 0.3);
|
| 106 |
+
border-radius: 15px;
|
| 107 |
+
padding: 20px;
|
| 108 |
+
margin: 10px 0;
|
| 109 |
+
border: 2px solid #00ffff;
|
| 110 |
+
box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
|
| 111 |
+
transition: all 0.3s ease;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.image-container img {
|
| 115 |
+
max-width: 100%;
|
| 116 |
+
height: auto;
|
| 117 |
+
display: block;
|
| 118 |
+
margin: 0 auto;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
.image-container:hover {
|
| 122 |
+
box-shadow: 0 0 20px rgba(0, 255, 255, 0.4);
|
| 123 |
+
transform: translateY(-2px);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
.download-btn {
|
| 127 |
+
background: linear-gradient(45deg, #00ffff, #ff00de);
|
| 128 |
+
border: none;
|
| 129 |
+
padding: 12px 25px;
|
| 130 |
+
border-radius: 8px;
|
| 131 |
+
color: white;
|
| 132 |
+
font-family: 'Orbitron', sans-serif;
|
| 133 |
+
cursor: pointer;
|
| 134 |
+
transition: all 0.3s ease;
|
| 135 |
+
margin-top: 10px;
|
| 136 |
+
text-align: center;
|
| 137 |
+
text-transform: uppercase;
|
| 138 |
+
letter-spacing: 1px;
|
| 139 |
+
width: 100%;
|
| 140 |
+
display: block;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
.download-btn:hover {
|
| 144 |
+
transform: translateY(-2px);
|
| 145 |
+
box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4);
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
@keyframes glow {
|
| 149 |
+
from { text-shadow: 0 0 5px #ff00de, 0 0 10px #ff00de; }
|
| 150 |
+
to { text-shadow: 0 0 10px #ff00de, 0 0 20px #ff00de; }
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
@media (max-width: 768px) {
|
| 154 |
+
.title-text { font-size: 1.8em; }
|
| 155 |
+
.subtitle-text { font-size: 1em; }
|
| 156 |
+
.image-container { padding: 10px; }
|
| 157 |
+
.download-btn { padding: 10px 20px; }
|
| 158 |
+
}
|
| 159 |
+
"""
|
| 160 |
+
|
| 161 |
+
with gr.Blocks(css=css) as demo:
|
| 162 |
gr.Markdown("""
|
| 163 |
<h1 class="title-text">AI Background Removal</h1>
|
| 164 |
<p class="subtitle-text">Remove backgrounds instantly using advanced AI technology</p>
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
download_button = gr.File(
|
| 183 |
+
label="Download Processed Image",
|
| 184 |
visible=True,
|
| 185 |
+
elem_classes="download-btn",
|
| 186 |
+
interactive=True
|
| 187 |
)
|
| 188 |
+
|
| 189 |
+
# Add JavaScript for auto-download
|
| 190 |
+
demo.load(None, None, None, _js="""
|
| 191 |
+
function autoDownload() {
|
| 192 |
+
const downloadButtons = document.querySelectorAll('.download-btn button');
|
| 193 |
+
if (downloadButtons.length > 0) {
|
| 194 |
+
downloadButtons[0].click();
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
const observer = new MutationObserver((mutations) => {
|
| 199 |
+
mutations.forEach((mutation) => {
|
| 200 |
+
if (mutation.type === 'childList') {
|
| 201 |
+
const downloadButtons = document.querySelectorAll('.download-btn button');
|
| 202 |
+
if (downloadButtons.length > 0) {
|
| 203 |
+
setTimeout(autoDownload, 1000);
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
});
|
| 207 |
+
});
|
| 208 |
+
|
| 209 |
+
observer.observe(document.body, { childList: true, subtree: true });
|
| 210 |
+
""")
|
| 211 |
|
| 212 |
input_image.change(
|
| 213 |
fn=process,
|
| 214 |
inputs=input_image,
|
| 215 |
+
outputs=[output_image, download_button]
|
| 216 |
)
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|