jamino30 commited on
Commit
764d4ab
1 Parent(s): e16d255

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +11 -20
app.py CHANGED
@@ -23,21 +23,20 @@ for param in model.parameters():
23
 
24
  style_files = os.listdir('./style_images')
25
  style_options = {' '.join(style_file.split('.')[0].split('_')): f'./style_images/{style_file}' for style_file in style_files}
 
 
26
 
27
  cached_style_features = {}
28
  for style_name, style_img_path in style_options.items():
29
- style_img_512 = preprocess_img_from_path(style_img_path, 512)[0].to(device)
30
- style_img_1024 = preprocess_img_from_path(style_img_path, 1024)[0].to(device)
31
  with torch.no_grad():
32
- style_features = (model(style_img_512), model(style_img_1024))
33
  cached_style_features[style_name] = style_features
34
 
35
- lrs = np.logspace(np.log10(0.001), np.log10(0.1), 10).tolist()
36
-
37
  @spaces.GPU(duration=15)
38
- def run(content_image, style_name, style_strength=5, output_quality=False, progress=gr.Progress(track_tqdm=True)):
39
  yield None
40
- img_size = 1024 if output_quality else 512
41
  content_img, original_size = preprocess_img(content_image, img_size)
42
  content_img = content_img.to(device)
43
 
@@ -45,10 +44,9 @@ def run(content_image, style_name, style_strength=5, output_quality=False, progr
45
  print('DATETIME:', datetime.now(timezone.utc) - timedelta(hours=4)) # est
46
  print('STYLE:', style_name)
47
  print('CONTENT IMG SIZE:', original_size)
48
- print('STYLE STRENGTH:', style_strength)
49
- print('HIGH QUALITY:', output_quality)
50
 
51
- style_features = cached_style_features[style_name][0 if img_size == 512 else 1]
52
 
53
  st = time.time()
54
  generated_img = inference(
@@ -79,15 +77,8 @@ with gr.Blocks(css=css) as demo:
79
  content_and_output = gr.Image(label='Content', show_label=False, type='pil', sources=['upload', 'webcam', 'clipboard'], format='jpg', show_download_button=False)
80
  style_dropdown = gr.Radio(choices=list(style_options.keys()), label='Style', value='Starry Night', type='value')
81
 
82
- with gr.Group():
83
- style_strength_slider = gr.Slider(label='Style Strength', minimum=1, maximum=10, step=1, value=5)
84
- with gr.Row():
85
- low_button = gr.Button('Low', size='sm').click(fn=lambda: set_slider(10), outputs=[style_strength_slider])
86
- medium_button = gr.Button('Medium', size='sm').click(fn=lambda: set_slider(50), outputs=[style_strength_slider])
87
- high_button = gr.Button('High', size='sm').click(fn=lambda: set_slider(100), outputs=[style_strength_slider])
88
-
89
- output_quality = gr.Checkbox(label='More Realistic', info='Note: If unchecked, the resulting image will have a more artistic flair.')
90
-
91
  submit_button = gr.Button('Submit', variant='primary')
92
  download_button = gr.DownloadButton(label='Download Image', visible=False)
93
 
@@ -98,7 +89,7 @@ with gr.Blocks(css=css) as demo:
98
 
99
  submit_button.click(
100
  fn=run,
101
- inputs=[content_and_output, style_dropdown, style_strength_slider, output_quality],
102
  outputs=[content_and_output]
103
  ).then(
104
  fn=save_image,
 
23
 
24
  style_files = os.listdir('./style_images')
25
  style_options = {' '.join(style_file.split('.')[0].split('_')): f'./style_images/{style_file}' for style_file in style_files}
26
+ lrs = np.logspace(np.log10(0.001), np.log10(0.1), 10).tolist()
27
+ img_size = 512
28
 
29
  cached_style_features = {}
30
  for style_name, style_img_path in style_options.items():
31
+ style_img = preprocess_img_from_path(style_img_path, img_size)[0].to(device)
 
32
  with torch.no_grad():
33
+ style_features = model(style_img)
34
  cached_style_features[style_name] = style_features
35
 
 
 
36
  @spaces.GPU(duration=15)
37
+ def run(content_image, style_name, style_strength=5, progress=gr.Progress(track_tqdm=True)):
38
  yield None
39
+ img_size = img_size
40
  content_img, original_size = preprocess_img(content_image, img_size)
41
  content_img = content_img.to(device)
42
 
 
44
  print('DATETIME:', datetime.now(timezone.utc) - timedelta(hours=4)) # est
45
  print('STYLE:', style_name)
46
  print('CONTENT IMG SIZE:', original_size)
47
+ print('STYLE STRENGTH:', style_strength, f'(lr={lrs[style_strength-1]})')
 
48
 
49
+ style_features = cached_style_features[style_name]
50
 
51
  st = time.time()
52
  generated_img = inference(
 
77
  content_and_output = gr.Image(label='Content', show_label=False, type='pil', sources=['upload', 'webcam', 'clipboard'], format='jpg', show_download_button=False)
78
  style_dropdown = gr.Radio(choices=list(style_options.keys()), label='Style', value='Starry Night', type='value')
79
 
80
+ style_strength_slider = gr.Slider(label='Style Strength', minimum=1, maximum=10, step=1, value=5)
81
+
 
 
 
 
 
 
 
82
  submit_button = gr.Button('Submit', variant='primary')
83
  download_button = gr.DownloadButton(label='Download Image', visible=False)
84
 
 
89
 
90
  submit_button.click(
91
  fn=run,
92
+ inputs=[content_and_output, style_dropdown, style_strength_slider],
93
  outputs=[content_and_output]
94
  ).then(
95
  fn=save_image,