ihabooe commited on
Commit
b0764b1
·
verified ·
1 Parent(s): a14ca2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -106
app.py CHANGED
@@ -24,81 +24,18 @@ print(f"Model loaded on {device}")
24
  OUTPUT_DIR = "output_images"
25
  os.makedirs(OUTPUT_DIR, exist_ok=True)
26
 
27
- # Define fixed size for all images
28
- FIXED_SIZE = (225, 225)
29
 
30
- # Resize the input image for model compatibility
31
- def resize_image(image, size=FIXED_SIZE):
32
- image = image.convert('RGB')
33
- image = image.resize(size, Image.LANCZOS)
34
- return image
35
 
36
- # Background removal process
37
- def process(image, progress=gr.Progress()):
38
- if image is None:
39
- return None, gr.update(visible=False)
40
-
41
- progress(0, desc="Starting processing...")
42
-
43
- # Prepare the input
44
- progress(0.1, desc="Preparing image...")
45
- orig_image = Image.fromarray(image)
46
- # Resize input image to fixed size
47
- orig_image = resize_image(orig_image, FIXED_SIZE)
48
- w, h = FIXED_SIZE
49
- image = orig_image
50
- im_np = np.array(image)
51
- im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
52
- im_tensor = torch.unsqueeze(im_tensor, 0)
53
- im_tensor = torch.divide(im_tensor, 255.0)
54
- im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
55
-
56
- progress(0.3, desc="Processing with AI model...")
57
- if torch.cuda.is_available():
58
- im_tensor = im_tensor.cuda()
59
-
60
- # Inference with the model
61
- with torch.no_grad():
62
- result = net(im_tensor)
63
-
64
- progress(0.6, desc="Post-processing...")
65
- # Post-process the result
66
- result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
67
- ma = torch.max(result)
68
- mi = torch.min(result)
69
- result = (result - mi) / (ma - mi)
70
-
71
- # Convert the result to an image
72
- result_array = (result * 255).cpu().data.numpy().astype(np.uint8)
73
- pil_mask = Image.fromarray(np.squeeze(result_array))
74
-
75
- # Add the mask as alpha channel to the original image
76
- new_im = orig_image.copy()
77
- new_im.putalpha(pil_mask)
78
-
79
- progress(0.8, desc="Preparing download...")
80
- # Generate a unique filename
81
- unique_id = str(uuid.uuid4())[:8]
82
- filename = f"background_removed_{unique_id}.png"
83
- filepath = os.path.join(OUTPUT_DIR, filename)
84
-
85
- # Save the processed image
86
- new_im.save(filepath, format='PNG')
87
-
88
- # Convert to numpy array for display
89
- output_array = np.array(new_im.convert("RGBA"))
90
-
91
- progress(1.0, desc="Done!")
92
-
93
- return output_array, gr.update(visible=True, value=filepath, interactive=True)
94
-
95
- # Gradio interface setup
96
- title = "Background Removal Tool"
97
  description = """
98
  <style>
99
  .custom-container {
100
  text-align: center;
101
- max-width: 800px;
102
  margin: 0 auto;
103
  padding: 20px;
104
  background: rgba(10, 10, 30, 0.6);
@@ -108,7 +45,7 @@ description = """
108
  .title-text {
109
  color: #ff00de;
110
  font-family: 'Orbitron', sans-serif;
111
- font-size: 2.5em;
112
  margin: 20px 0;
113
  text-shadow: 0 0 10px rgba(255, 0, 222, 0.5);
114
  animation: title-pulse 2s infinite alternate;
@@ -116,7 +53,7 @@ description = """
116
  .subtitle-text {
117
  color: #00ffff;
118
  font-family: 'Roboto Mono', monospace;
119
- font-size: 1.2em;
120
  margin-top: 10px;
121
  line-height: 1.5;
122
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
@@ -125,6 +62,16 @@ description = """
125
  0% { text-shadow: 0 0 5px rgba(255, 0, 222, 0.5); }
126
  100% { text-shadow: 0 0 15px rgba(255, 0, 222, 0.8), 0 0 25px rgba(255, 0, 222, 0.5); }
127
  }
 
 
 
 
 
 
 
 
 
 
128
  </style>
129
  <div class="custom-container">
130
  <h1 class="title-text">AI Background Removal</h1>
@@ -134,7 +81,7 @@ description = """
134
  </div>
135
  """
136
 
137
- # Create the Gradio interface
138
  with gr.Blocks(css="""
139
  /* Import fonts */
140
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
@@ -156,26 +103,27 @@ with gr.Blocks(css="""
156
  min-height: 100vh;
157
  }
158
 
159
- /* Container styling */
160
  .container {
161
- background: rgba(0, 0, 0, 0.6);
162
- backdrop-filter: blur(10px);
163
- border-radius: 15px;
164
- padding: 20px;
165
- box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
166
- margin: 20px;
167
  }
168
 
169
- /* Input/Output areas */
170
  .input-image, .output-image {
171
- width: 512px !important;
172
- height: 512px !important;
 
 
173
  object-fit: contain !important;
174
  background: rgba(18, 18, 56, 0.7) !important;
175
  border: 2px solid var(--neon-cyan) !important;
176
  border-radius: 12px !important;
177
  transition: all 0.3s ease !important;
178
  overflow: hidden !important;
 
179
  }
180
 
181
  .input-image img, .output-image img {
@@ -184,9 +132,12 @@ with gr.Blocks(css="""
184
  object-fit: contain !important;
185
  }
186
 
187
- .input-image:hover, .output-image:hover {
188
- box-shadow: 0 0 20px rgba(0, 255, 255, 0.4) !important;
189
- transform: translateY(-2px) !important;
 
 
 
190
  }
191
 
192
  /* Download button styling */
@@ -204,11 +155,8 @@ with gr.Blocks(css="""
204
  cursor: pointer !important;
205
  transition: all 0.3s ease !important;
206
  animation: button-glow 2s infinite alternate !important;
207
- }
208
-
209
- .download-container [data-testid="file"] button:hover {
210
- transform: translateY(-2px) !important;
211
- box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4) !important;
212
  }
213
 
214
  /* Labels */
@@ -218,20 +166,23 @@ with gr.Blocks(css="""
218
  font-size: 1.1em !important;
219
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5) !important;
220
  margin-bottom: 8px !important;
 
221
  }
222
 
223
- /* Progress bar */
224
- .progress-bar {
225
- background: linear-gradient(90deg, var(--neon-cyan), var(--neon-pink)) !important;
226
- height: 4px !important;
227
- border-radius: 2px !important;
228
- box-shadow: 0 0 10px rgba(0, 255, 255, 0.5) !important;
229
- }
230
-
231
- /* Additional Animations */
232
- @keyframes button-glow {
233
- 0% { box-shadow: 0 0 5px rgba(0, 255, 255, 0.5); }
234
- 100% { box-shadow: 0 0 15px rgba(0, 255, 255, 0.8), 0 0 25px rgba(255, 0, 222, 0.5); }
 
 
235
  }
236
  """) as demo:
237
  gr.Markdown(description)
@@ -243,8 +194,7 @@ with gr.Blocks(css="""
243
  label="Upload Your Image",
244
  elem_id="input-image",
245
  elem_classes="input-image",
246
- height=512,
247
- width=512
248
  )
249
 
250
  with gr.Column(scale=1):
@@ -253,8 +203,7 @@ with gr.Blocks(css="""
253
  label="Result",
254
  elem_id="output-image",
255
  elem_classes="output-image",
256
- height=512,
257
- width=512
258
  )
259
 
260
  with gr.Row(elem_classes="download-container"):
 
24
  OUTPUT_DIR = "output_images"
25
  os.makedirs(OUTPUT_DIR, exist_ok=True)
26
 
27
+ # ... keep all imports and model loading code the same ...
 
28
 
29
+ # Define different sizes for different devices
30
+ DESKTOP_SIZE = (512, 512)
31
+ MOBILE_SIZE = (300, 300) # Smaller size for mobile devices
 
 
32
 
33
+ # Update the description and CSS part with responsive design:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  description = """
35
  <style>
36
  .custom-container {
37
  text-align: center;
38
+ max-width: 100%;
39
  margin: 0 auto;
40
  padding: 20px;
41
  background: rgba(10, 10, 30, 0.6);
 
45
  .title-text {
46
  color: #ff00de;
47
  font-family: 'Orbitron', sans-serif;
48
+ font-size: 2em;
49
  margin: 20px 0;
50
  text-shadow: 0 0 10px rgba(255, 0, 222, 0.5);
51
  animation: title-pulse 2s infinite alternate;
 
53
  .subtitle-text {
54
  color: #00ffff;
55
  font-family: 'Roboto Mono', monospace;
56
+ font-size: 1em;
57
  margin-top: 10px;
58
  line-height: 1.5;
59
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
 
62
  0% { text-shadow: 0 0 5px rgba(255, 0, 222, 0.5); }
63
  100% { text-shadow: 0 0 15px rgba(255, 0, 222, 0.8), 0 0 25px rgba(255, 0, 222, 0.5); }
64
  }
65
+
66
+ /* Responsive text sizes */
67
+ @media (max-width: 768px) {
68
+ .title-text {
69
+ font-size: 1.5em;
70
+ }
71
+ .subtitle-text {
72
+ font-size: 0.9em;
73
+ }
74
+ }
75
  </style>
76
  <div class="custom-container">
77
  <h1 class="title-text">AI Background Removal</h1>
 
81
  </div>
82
  """
83
 
84
+ # Update the Gradio interface with responsive CSS
85
  with gr.Blocks(css="""
86
  /* Import fonts */
87
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
 
103
  min-height: 100vh;
104
  }
105
 
106
+ /* Responsive container */
107
  .container {
108
+ width: 100%;
109
+ max-width: 1200px;
110
+ margin: 0 auto;
111
+ padding: 10px;
 
 
112
  }
113
 
114
+ /* Input/Output areas with responsive sizing */
115
  .input-image, .output-image {
116
+ width: 100% !important;
117
+ max-width: 512px !important;
118
+ height: auto !important;
119
+ aspect-ratio: 1/1 !important;
120
  object-fit: contain !important;
121
  background: rgba(18, 18, 56, 0.7) !important;
122
  border: 2px solid var(--neon-cyan) !important;
123
  border-radius: 12px !important;
124
  transition: all 0.3s ease !important;
125
  overflow: hidden !important;
126
+ margin: 0 auto !important;
127
  }
128
 
129
  .input-image img, .output-image img {
 
132
  object-fit: contain !important;
133
  }
134
 
135
+ /* Responsive columns */
136
+ .contain-center {
137
+ display: flex;
138
+ flex-direction: column;
139
+ align-items: center;
140
+ gap: 20px;
141
  }
142
 
143
  /* Download button styling */
 
155
  cursor: pointer !important;
156
  transition: all 0.3s ease !important;
157
  animation: button-glow 2s infinite alternate !important;
158
+ width: 100% !important;
159
+ max-width: 300px !important;
 
 
 
160
  }
161
 
162
  /* Labels */
 
166
  font-size: 1.1em !important;
167
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5) !important;
168
  margin-bottom: 8px !important;
169
+ text-align: center !important;
170
  }
171
 
172
+ /* Responsive layout */
173
+ @media (max-width: 768px) {
174
+ .input-image, .output-image {
175
+ max-width: 300px !important;
176
+ }
177
+
178
+ label {
179
+ font-size: 0.9em !important;
180
+ }
181
+
182
+ .download-container [data-testid="file"] button {
183
+ padding: 10px 20px !important;
184
+ font-size: 14px !important;
185
+ }
186
  }
187
  """) as demo:
188
  gr.Markdown(description)
 
194
  label="Upload Your Image",
195
  elem_id="input-image",
196
  elem_classes="input-image",
197
+ container=True
 
198
  )
199
 
200
  with gr.Column(scale=1):
 
203
  label="Result",
204
  elem_id="output-image",
205
  elem_classes="output-image",
206
+ container=True
 
207
  )
208
 
209
  with gr.Row(elem_classes="download-container"):