ihabooe commited on
Commit
579622d
·
verified ·
1 Parent(s): 5ddafad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -88
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, 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, filepath, gr.HTML.update(value=auto_download_html, visible=True)
92
 
93
  except Exception as e:
94
  print(f"Error processing image: {str(e)}")
95
- return None, None, None
96
 
97
- # Define the interface
98
- with gr.Blocks(css="""
99
- @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
100
-
101
- .title-text {
102
- color: #ff00de;
103
- font-family: 'Orbitron', sans-serif;
104
- font-size: 2.5em;
105
- text-align: center;
106
- margin: 20px 0;
107
- text-shadow: 0 0 10px rgba(255, 0, 222, 0.7);
108
- animation: glow 2s ease-in-out infinite alternate;
109
- }
110
-
111
- .subtitle-text {
112
- color: #00ffff;
113
- text-align: center;
114
- margin-bottom: 30px;
115
- font-size: 1.2em;
116
- text-shadow: 0 0 8px rgba(0, 255, 255, 0.7);
117
- }
118
-
119
- .image-container {
120
- background: rgba(10, 10, 30, 0.3);
121
- border-radius: 15px;
122
- padding: 20px;
123
- margin: 10px 0;
124
- border: 2px solid #00ffff;
125
- box-shadow: 0 0 15px rgba(0, 255, 255, 0.2);
126
- transition: all 0.3s ease;
127
- }
128
-
129
- .image-container img {
130
- max-width: 100%;
131
- height: auto;
132
- display: block;
133
- margin: 0 auto;
134
- }
135
-
136
- .download-btn {
137
- background: linear-gradient(45deg, #00ffff, #ff00de);
138
- border: none;
139
- padding: 12px 25px;
140
- border-radius: 8px;
141
- color: white;
142
- font-family: 'Orbitron', sans-serif;
143
- cursor: pointer;
144
- transition: all 0.3s ease;
145
- margin-top: 10px;
146
- text-align: center;
147
- text-transform: uppercase;
148
- letter-spacing: 1px;
149
- }
150
-
151
- @keyframes glow {
152
- from { text-shadow: 0 0 5px #ff00de, 0 0 10px #ff00de; }
153
- to { text-shadow: 0 0 10px #ff00de, 0 0 20px #ff00de; }
154
- }
155
-
156
- @media (max-width: 768px) {
157
- .title-text { font-size: 1.8em; }
158
- .subtitle-text { font-size: 1em; }
159
- .image-container { padding: 10px; }
160
- }
161
- """) 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,17 +180,39 @@ with gr.Blocks(css="""
180
  )
181
 
182
  download_button = gr.File(
183
- label="Download Result",
184
  visible=True,
185
- elem_classes="download-btn"
 
186
  )
187
-
188
- auto_download = gr.HTML(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
  input_image.change(
191
  fn=process,
192
  inputs=input_image,
193
- outputs=[output_image, download_button, auto_download]
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__":