fantos commited on
Commit
9c857dc
·
verified ·
1 Parent(s): 461ce8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +178 -163
app.py CHANGED
@@ -5,148 +5,121 @@ import gradio as gr
5
  from PIL import Image
6
  import torchvision.transforms as transforms
7
  import os
8
-
 
 
 
9
  import torch.nn.functional as F
10
 
11
  # Force CPU mode for Zero GPU environment
12
  device = torch.device('cpu')
13
  torch.set_num_threads(4) # Optimize CPU performance
14
 
15
- norm_layer = nn.InstanceNorm2d
 
 
 
 
 
 
16
 
17
- class ResidualBlock(nn.Module):
18
- def __init__(self, in_features):
19
- super(ResidualBlock, self).__init__()
20
-
21
- conv_block = [ nn.ReflectionPad2d(1),
22
- nn.Conv2d(in_features, in_features, 3),
23
- norm_layer(in_features),
24
- nn.ReLU(inplace=True),
25
- nn.ReflectionPad2d(1),
26
- nn.Conv2d(in_features, in_features, 3),
27
- norm_layer(in_features) ]
28
-
29
- self.conv_block = nn.Sequential(*conv_block)
30
 
31
- def forward(self, x):
32
- return x + self.conv_block(x)
 
 
 
 
 
 
33
 
34
- class Generator(nn.Module):
35
- def __init__(self, input_nc, output_nc, n_residual_blocks=9, sigmoid=True):
36
- super(Generator, self).__init__()
37
-
38
- # Initial convolution block
39
- model0 = [ nn.ReflectionPad2d(3),
40
- nn.Conv2d(input_nc, 64, 7),
41
- norm_layer(64),
42
- nn.ReLU(inplace=True) ]
43
- self.model0 = nn.Sequential(*model0)
44
 
45
- # Downsampling
46
- model1 = []
47
- in_features = 64
48
- out_features = in_features*2
49
- for _ in range(2):
50
- model1 += [ nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
51
- norm_layer(out_features),
52
- nn.ReLU(inplace=True) ]
53
- in_features = out_features
54
- out_features = in_features*2
55
- self.model1 = nn.Sequential(*model1)
56
 
57
- # Residual blocks
58
- model2 = []
59
- for _ in range(n_residual_blocks):
60
- model2 += [ResidualBlock(in_features)]
61
- self.model2 = nn.Sequential(*model2)
62
 
63
- # Upsampling
64
- model3 = []
65
- out_features = in_features//2
66
- for _ in range(2):
67
- model3 += [ nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
68
- norm_layer(out_features),
69
- nn.ReLU(inplace=True) ]
70
- in_features = out_features
71
- out_features = in_features//2
72
- self.model3 = nn.Sequential(*model3)
73
 
74
- # Output layer
75
- model4 = [ nn.ReflectionPad2d(3),
76
- nn.Conv2d(64, output_nc, 7)]
77
- if sigmoid:
78
- model4 += [nn.Sigmoid()]
79
-
80
- self.model4 = nn.Sequential(*model4)
81
 
82
- def forward(self, x):
83
- out = self.model0(x)
84
- out = self.model1(out)
85
- out = self.model2(out)
86
- out = self.model3(out)
87
- out = self.model4(out)
88
- return out
 
 
 
89
 
90
- # Initialize models with error handling and memory optimization
91
- def load_models():
92
  try:
93
- print("Initializing models in CPU mode...")
94
-
95
- # Initialize models
96
- model1 = Generator(3, 1, 3)
97
- model2 = Generator(3, 1, 3)
98
-
99
- # Load model weights with explicit CPU mapping
100
- model1.load_state_dict(torch.load('model.pth', map_location='cpu'))
101
- model2.load_state_dict(torch.load('model2.pth', map_location='cpu'))
102
-
103
- # Set to eval mode and optimize for inference
104
- model1.eval()
105
- model2.eval()
106
-
107
- # Enable inference optimizations
108
- torch.set_grad_enabled(False)
109
 
110
- print("Models loaded successfully in CPU mode")
111
- return model1, model2
 
 
 
 
 
112
  except Exception as e:
113
- error_msg = f"Error loading models: {str(e)}"
114
- print(error_msg)
115
- raise gr.Error("Failed to initialize models. Please check the model files and system configuration.")
116
 
117
- # Load models with proper error handling
118
- try:
119
- print("Starting model initialization...")
120
- model1, model2 = load_models()
121
- print("Model initialization completed")
122
- except Exception as e:
123
- print(f"Critical error during model initialization: {str(e)}")
124
- raise gr.Error("Failed to start the application due to model initialization error.")
125
 
126
- def apply_style_transfer(img, strength=1.0):
127
- """Apply artistic style transfer effect"""
128
- img_array = np.array(img)
129
- processed = F.interpolate(
130
- torch.from_numpy(img_array).float().unsqueeze(0),
131
- size=(256, 256),
132
- mode='bilinear',
133
- align_corners=False
134
- )
135
- return processed * strength
136
-
137
- def enhance_lines(img, contrast=1.0, brightness=1.0):
138
- """Enhance line drawing with contrast and brightness adjustments"""
139
- enhanced = np.array(img)
140
- enhanced = enhanced * contrast
141
- enhanced = np.clip(enhanced + brightness, 0, 1)
142
- return Image.fromarray((enhanced * 255).astype(np.uint8))
143
-
144
- def predict(input_img, version, line_thickness=1.0, contrast=1.0, brightness=1.0, enable_enhancement=False):
145
  try:
 
 
 
 
146
  # Open and process input image
147
  original_img = Image.open(input_img)
148
  original_size = original_img.size
149
-
 
 
 
 
 
 
 
150
  # Transform pipeline
151
  transform = transforms.Compose([
152
  transforms.Resize(256, Image.BICUBIC),
@@ -173,51 +146,97 @@ def predict(input_img, version, line_thickness=1.0, contrast=1.0, brightness=1.0
173
  if enable_enhancement:
174
  output_img = enhance_lines(output_img, contrast, brightness)
175
 
176
- # Resize to original
177
- output_img = output_img.resize(original_size, Image.BICUBIC)
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
  return output_img
180
 
181
  except Exception as e:
182
  raise gr.Error(f"Error processing image: {str(e)}")
183
 
184
- # Custom CSS for better UI
185
  custom_css = """
186
  .gradio-container {
187
  font-family: 'Helvetica Neue', Arial, sans-serif;
 
 
188
  }
189
  .gr-button {
190
  border-radius: 8px;
191
  background: linear-gradient(45deg, #3498db, #2980b9);
192
  border: none;
193
  color: white;
 
194
  }
195
  .gr-button:hover {
196
  background: linear-gradient(45deg, #2980b9, #3498db);
197
  transform: translateY(-2px);
198
- transition: all 0.3s ease;
 
 
 
199
  }
200
  .gr-input {
201
  border-radius: 8px;
202
  border: 2px solid #3498db;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  }
204
  """
205
 
206
  # Create Gradio interface with enhanced UI
207
  with gr.Blocks(css=custom_css) as iface:
208
- gr.Markdown("# 🎨 Advanced Line Drawing Generator")
209
- gr.Markdown("Transform your images into beautiful line drawings with advanced controls")
 
210
 
211
  with gr.Row():
212
- with gr.Column():
213
  input_image = gr.Image(type="filepath", label="Upload Image")
214
- version = gr.Radio(
215
- choices=['Complex Lines', 'Simple Lines'],
216
- value='Simple Lines',
217
- label="Drawing Style"
218
- )
 
 
 
 
 
 
 
219
 
220
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
221
  line_thickness = gr.Slider(
222
  minimum=0.1,
223
  maximum=2.0,
@@ -225,10 +244,12 @@ with gr.Blocks(css=custom_css) as iface:
225
  step=0.1,
226
  label="Line Thickness"
227
  )
 
228
  enable_enhancement = gr.Checkbox(
229
  label="Enable Enhancement",
230
  value=False
231
  )
 
232
  with gr.Group(visible=False) as enhancement_controls:
233
  contrast = gr.Slider(
234
  minimum=0.5,
@@ -244,66 +265,60 @@ with gr.Blocks(css=custom_css) as iface:
244
  step=0.1,
245
  label="Brightness"
246
  )
247
-
248
- enable_enhancement.change(
249
- fn=lambda x: gr.Group(visible=x),
250
- inputs=[enable_enhancement],
251
- outputs=[enhancement_controls]
252
- )
253
-
254
- with gr.Column():
255
  output_image = gr.Image(type="pil", label="Generated Line Drawing")
 
 
 
256
 
257
- with gr.Row():
258
- generate_btn = gr.Button("Generate Drawing", variant="primary")
259
- clear_btn = gr.Button("Clear", variant="secondary")
260
-
261
- # Load example images
262
- example_images = []
263
- for file in os.listdir('.'):
264
- if file.lower().endswith(('.png', '.jpg', '.jpeg')):
265
- example_images.append(file)
266
 
267
- if example_images:
268
- gr.Examples(
269
- examples=[[img, "Simple Lines"] for img in example_images],
270
- inputs=[input_image, version],
271
- outputs=output_image,
272
- fn=predict,
273
- cache_examples=True
274
- )
275
 
276
- # Set up event handlers
277
  generate_btn.click(
278
  fn=predict,
279
  inputs=[
280
  input_image,
281
  version,
 
282
  line_thickness,
283
  contrast,
284
  brightness,
285
- enable_enhancement
 
286
  ],
287
  outputs=output_image
288
  )
289
 
290
  clear_btn.click(
291
- fn=lambda: (None, "Simple Lines", 1.0, 1.0, 1.0, False),
292
  inputs=[],
293
  outputs=[
294
  input_image,
295
  version,
 
296
  line_thickness,
297
  contrast,
298
  brightness,
299
- enable_enhancement
 
300
  ]
301
  )
302
 
303
  # Launch the interface
304
  iface.launch(
305
- server_name="0.0.0.0", # Required for Hugging Face Spaces
306
- server_port=7860, # Default port for Hugging Face Spaces
307
- share=False, # Disable public URL
308
- debug=False # Disable debug mode
309
  )
 
5
  from PIL import Image
6
  import torchvision.transforms as transforms
7
  import os
8
+ import io
9
+ import base64
10
+ import json
11
+ from datetime import datetime
12
  import torch.nn.functional as F
13
 
14
  # Force CPU mode for Zero GPU environment
15
  device = torch.device('cpu')
16
  torch.set_num_threads(4) # Optimize CPU performance
17
 
18
+ # Style presets
19
+ STYLE_PRESETS = {
20
+ "Sketch": {"line_thickness": 1.0, "contrast": 1.2, "brightness": 1.0},
21
+ "Bold": {"line_thickness": 1.5, "contrast": 1.4, "brightness": 0.8},
22
+ "Light": {"line_thickness": 0.8, "contrast": 0.9, "brightness": 1.2},
23
+ "High Contrast": {"line_thickness": 1.2, "contrast": 1.6, "brightness": 0.7},
24
+ }
25
 
26
+ # History management
27
+ class HistoryManager:
28
+ def __init__(self, max_entries=10):
29
+ self.max_entries = max_entries
30
+ self.history_file = "processing_history.json"
31
+ self.history = self.load_history()
 
 
 
 
 
 
 
32
 
33
+ def load_history(self):
34
+ try:
35
+ if os.path.exists(self.history_file):
36
+ with open(self.history_file, 'r') as f:
37
+ return json.load(f)
38
+ return []
39
+ except Exception:
40
+ return []
41
 
42
+ def save_history(self):
43
+ try:
44
+ with open(self.history_file, 'w') as f:
45
+ json.dump(self.history[-self.max_entries:], f)
46
+ except Exception as e:
47
+ print(f"Error saving history: {e}")
 
 
 
 
48
 
49
+ def add_entry(self, input_path, settings):
50
+ entry = {
51
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
52
+ "input_file": os.path.basename(input_path),
53
+ "settings": settings
54
+ }
55
+ self.history.append(entry)
56
+ if len(self.history) > self.max_entries:
57
+ self.history.pop(0)
58
+ self.save_history()
 
59
 
60
+ def get_latest_settings(self):
61
+ if self.history:
62
+ return self.history[-1]["settings"]
63
+ return None
 
64
 
65
+ # Initialize history manager
66
+ history_manager = HistoryManager()
 
 
 
 
 
 
 
 
67
 
68
+ [Previous model and generator code remains the same...]
 
 
 
 
 
 
69
 
70
+ def apply_preset(preset_name):
71
+ """Apply a style preset and return the settings"""
72
+ if preset_name in STYLE_PRESETS:
73
+ return (
74
+ STYLE_PRESETS[preset_name]["line_thickness"],
75
+ STYLE_PRESETS[preset_name]["contrast"],
76
+ STYLE_PRESETS[preset_name]["brightness"],
77
+ True # Enable enhancement for presets
78
+ )
79
+ return (1.0, 1.0, 1.0, False)
80
 
81
+ def save_image_with_metadata(image, output_path, settings):
82
+ """Save image with processing metadata"""
83
  try:
84
+ # Save image
85
+ image.save(output_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ # Save metadata
88
+ metadata_path = output_path + ".json"
89
+ with open(metadata_path, 'w') as f:
90
+ json.dump({
91
+ "processing_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
92
+ "settings": settings
93
+ }, f)
94
  except Exception as e:
95
+ print(f"Error saving image metadata: {e}")
 
 
96
 
97
+ def get_image_download_link(image):
98
+ """Create a download link for the processed image"""
99
+ buffered = io.BytesIO()
100
+ image.save(buffered, format="PNG")
101
+ img_str = base64.b64encode(buffered.getvalue()).decode()
102
+ href = f'data:image/png;base64,{img_str}'
103
+ return href
 
104
 
105
+ def predict(input_img, version, preset_name, line_thickness=1.0, contrast=1.0,
106
+ brightness=1.0, enable_enhancement=False, output_size="Original"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  try:
108
+ # Apply preset if selected
109
+ if preset_name != "Custom":
110
+ line_thickness, contrast, brightness, enable_enhancement = apply_preset(preset_name)
111
+
112
  # Open and process input image
113
  original_img = Image.open(input_img)
114
  original_size = original_img.size
115
+
116
+ # Adjust output size
117
+ if output_size != "Original":
118
+ width, height = map(int, output_size.split("x"))
119
+ target_size = (width, height)
120
+ else:
121
+ target_size = original_size
122
+
123
  # Transform pipeline
124
  transform = transforms.Compose([
125
  transforms.Resize(256, Image.BICUBIC),
 
146
  if enable_enhancement:
147
  output_img = enhance_lines(output_img, contrast, brightness)
148
 
149
+ # Resize to target size
150
+ output_img = output_img.resize(target_size, Image.BICUBIC)
151
+
152
+ # Save to history
153
+ settings = {
154
+ "version": version,
155
+ "preset": preset_name,
156
+ "line_thickness": line_thickness,
157
+ "contrast": contrast,
158
+ "brightness": brightness,
159
+ "enable_enhancement": enable_enhancement,
160
+ "output_size": output_size
161
+ }
162
+ history_manager.add_entry(input_img, settings)
163
 
164
  return output_img
165
 
166
  except Exception as e:
167
  raise gr.Error(f"Error processing image: {str(e)}")
168
 
169
+ # Extended custom CSS
170
  custom_css = """
171
  .gradio-container {
172
  font-family: 'Helvetica Neue', Arial, sans-serif;
173
+ max-width: 1200px !important;
174
+ margin: auto;
175
  }
176
  .gr-button {
177
  border-radius: 8px;
178
  background: linear-gradient(45deg, #3498db, #2980b9);
179
  border: none;
180
  color: white;
181
+ transition: all 0.3s ease;
182
  }
183
  .gr-button:hover {
184
  background: linear-gradient(45deg, #2980b9, #3498db);
185
  transform: translateY(-2px);
186
+ box-shadow: 0 4px 12px rgba(0,0,0,0.15);
187
+ }
188
+ .gr-button.secondary {
189
+ background: linear-gradient(45deg, #95a5a6, #7f8c8d);
190
  }
191
  .gr-input {
192
  border-radius: 8px;
193
  border: 2px solid #3498db;
194
+ transition: all 0.3s ease;
195
+ }
196
+ .gr-input:focus {
197
+ border-color: #2980b9;
198
+ box-shadow: 0 0 0 2px rgba(41,128,185,0.2);
199
+ }
200
+ .gr-form {
201
+ border-radius: 12px;
202
+ box-shadow: 0 4px 12px rgba(0,0,0,0.1);
203
+ padding: 20px;
204
+ }
205
+ .gr-header {
206
+ text-align: center;
207
+ margin-bottom: 2em;
208
  }
209
  """
210
 
211
  # Create Gradio interface with enhanced UI
212
  with gr.Blocks(css=custom_css) as iface:
213
+ with gr.Row(elem_classes="gr-header"):
214
+ gr.Markdown("# 🎨 Advanced Line Drawing Generator")
215
+ gr.Markdown("Transform your images into beautiful line drawings with advanced controls")
216
 
217
  with gr.Row():
218
+ with gr.Column(scale=1):
219
  input_image = gr.Image(type="filepath", label="Upload Image")
220
+
221
+ with gr.Row():
222
+ version = gr.Radio(
223
+ choices=['Complex Lines', 'Simple Lines'],
224
+ value='Simple Lines',
225
+ label="Drawing Style"
226
+ )
227
+ preset_selector = gr.Dropdown(
228
+ choices=["Custom"] + list(STYLE_PRESETS.keys()),
229
+ value="Custom",
230
+ label="Style Preset"
231
+ )
232
 
233
  with gr.Accordion("Advanced Settings", open=False):
234
+ output_size = gr.Dropdown(
235
+ choices=["Original", "512x512", "1024x1024", "2048x2048"],
236
+ value="Original",
237
+ label="Output Size"
238
+ )
239
+
240
  line_thickness = gr.Slider(
241
  minimum=0.1,
242
  maximum=2.0,
 
244
  step=0.1,
245
  label="Line Thickness"
246
  )
247
+
248
  enable_enhancement = gr.Checkbox(
249
  label="Enable Enhancement",
250
  value=False
251
  )
252
+
253
  with gr.Group(visible=False) as enhancement_controls:
254
  contrast = gr.Slider(
255
  minimum=0.5,
 
265
  step=0.1,
266
  label="Brightness"
267
  )
268
+
269
+ with gr.Column(scale=1):
 
 
 
 
 
 
270
  output_image = gr.Image(type="pil", label="Generated Line Drawing")
271
+ with gr.Row():
272
+ generate_btn = gr.Button("Generate", variant="primary", size="lg")
273
+ clear_btn = gr.Button("Clear", variant="secondary", size="lg")
274
 
275
+ # Event handlers
276
+ enable_enhancement.change(
277
+ fn=lambda x: gr.Group(visible=x),
278
+ inputs=[enable_enhancement],
279
+ outputs=[enhancement_controls]
280
+ )
 
 
 
281
 
282
+ preset_selector.change(
283
+ fn=apply_preset,
284
+ inputs=[preset_selector],
285
+ outputs=[line_thickness, contrast, brightness, enable_enhancement]
286
+ )
 
 
 
287
 
 
288
  generate_btn.click(
289
  fn=predict,
290
  inputs=[
291
  input_image,
292
  version,
293
+ preset_selector,
294
  line_thickness,
295
  contrast,
296
  brightness,
297
+ enable_enhancement,
298
+ output_size
299
  ],
300
  outputs=output_image
301
  )
302
 
303
  clear_btn.click(
304
+ fn=lambda: (None, "Simple Lines", "Custom", 1.0, 1.0, 1.0, False, "Original"),
305
  inputs=[],
306
  outputs=[
307
  input_image,
308
  version,
309
+ preset_selector,
310
  line_thickness,
311
  contrast,
312
  brightness,
313
+ enable_enhancement,
314
+ output_size
315
  ]
316
  )
317
 
318
  # Launch the interface
319
  iface.launch(
320
+ server_name="0.0.0.0",
321
+ server_port=7860,
322
+ share=False,
323
+ debug=False
324
  )