StevenChen16 commited on
Commit
daf327e
·
verified ·
1 Parent(s): 764064c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -41
app.py CHANGED
@@ -33,54 +33,58 @@ def handle_image_size_selection(img_size, content_img):
33
  None)
34
 
35
  # Define the function to process images
36
- @spaces.GPU
37
  def process_images(content_img, style_img, epochs, steps_per_epoch, learning_rate, content_loss_factor, style_loss_factor, img_size, img_width, img_height):
38
  print("Start processing")
39
  output_img = main(content_img, style_img, epochs, steps_per_epoch, learning_rate, content_loss_factor, style_loss_factor, img_size, img_width, img_height)
40
  return output_img
41
 
42
- with gr.Blocks() as demo:
43
- aspect_ratio = gr.State(None)
44
- with gr.Row():
45
- with gr.Column(scale=1):
46
- content_img = gr.Image(type="numpy", label="Content Image")
47
- style_img = gr.Image(type="numpy", label="Style Image")
48
- process_button = gr.Button("Process")
49
- with gr.Accordion("Parameters", open=False):
50
- epochs = gr.Slider(minimum=1, maximum=100, step=1, label="Epochs", value=20)
51
- steps_per_epoch = gr.Slider(minimum=1, maximum=1000, step=1, label="Steps per Epoch", value=100)
52
- learning_rate = gr.Slider(minimum=0.0001, maximum=0.1, step=0.0001, label="Learning Rate", value=0.01)
53
- content_loss_factor = gr.Slider(minimum=0.1, maximum=10, step=0.1, label="Content Loss Factor", value=1.0)
54
- style_loss_factor = gr.Slider(minimum=0.1, maximum=1000, step=0.1, label="Style Loss Factor", value=100.0)
55
- img_size = gr.Dropdown(choices=["default size", "custom size"], label="Image Size", value="default size")
56
- img_width = gr.Number(label="Image Width", value=450, visible=False)
57
- img_height = gr.Number(label="Image Height", value=300, visible=False)
58
- with gr.Column(scale=1):
59
- output_img = gr.Image(label="Output Image")
60
 
61
- img_size.change(
62
- fn=handle_image_size_selection,
63
- inputs=[img_size, content_img],
64
- outputs=[img_width, img_height, img_width, img_height, aspect_ratio]
65
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- img_width.change(
68
- fn=lambda w, ar: update_dimensions(w, ar, "width"),
69
- inputs=[img_width, aspect_ratio],
70
- outputs=[img_width, img_height]
71
- )
72
 
73
- img_height.change(
74
- fn=lambda h, ar: update_dimensions(h, ar, "height"),
75
- inputs=[img_height, aspect_ratio],
76
- outputs=[img_width, img_height]
77
- )
78
 
79
- process_button.click(
80
- process_images,
81
- inputs=[content_img, style_img, epochs, steps_per_epoch, learning_rate, content_loss_factor, style_loss_factor, img_size, img_width, img_height],
82
- outputs=[output_img]
83
- )
84
 
85
- # Launch the app
86
- demo.launch()
 
 
 
 
 
 
 
 
 
33
  None)
34
 
35
  # Define the function to process images
 
36
  def process_images(content_img, style_img, epochs, steps_per_epoch, learning_rate, content_loss_factor, style_loss_factor, img_size, img_width, img_height):
37
  print("Start processing")
38
  output_img = main(content_img, style_img, epochs, steps_per_epoch, learning_rate, content_loss_factor, style_loss_factor, img_size, img_width, img_height)
39
  return output_img
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ @space.GPU
43
+ def create_app():
44
+ with gr.Blocks() as demo:
45
+ aspect_ratio = gr.State(None)
46
+ with gr.Row():
47
+ with gr.Column(scale=1):
48
+ content_img = gr.Image(type="numpy", label="Content Image")
49
+ style_img = gr.Image(type="numpy", label="Style Image")
50
+ process_button = gr.Button("Process")
51
+ with gr.Accordion("Parameters", open=False):
52
+ epochs = gr.Slider(minimum=1, maximum=100, step=1, label="Epochs", value=20)
53
+ steps_per_epoch = gr.Slider(minimum=1, maximum=1000, step=1, label="Steps per Epoch", value=100)
54
+ learning_rate = gr.Slider(minimum=0.0001, maximum=0.1, step=0.0001, label="Learning Rate", value=0.01)
55
+ content_loss_factor = gr.Slider(minimum=0.1, maximum=10, step=0.1, label="Content Loss Factor", value=1.0)
56
+ style_loss_factor = gr.Slider(minimum=0.1, maximum=1000, step=0.1, label="Style Loss Factor", value=100.0)
57
+ img_size = gr.Dropdown(choices=["default size", "custom size"], label="Image Size", value="default size")
58
+ img_width = gr.Number(label="Image Width", value=450, visible=False)
59
+ img_height = gr.Number(label="Image Height", value=300, visible=False)
60
+ with gr.Column(scale=1):
61
+ output_img = gr.Image(label="Output Image")
62
 
63
+ img_size.change(
64
+ fn=handle_image_size_selection,
65
+ inputs=[img_size, content_img],
66
+ outputs=[img_width, img_height, img_width, img_height, aspect_ratio]
67
+ )
68
 
69
+ img_width.change(
70
+ fn=lambda w, ar: update_dimensions(w, ar, "width"),
71
+ inputs=[img_width, aspect_ratio],
72
+ outputs=[img_width, img_height]
73
+ )
74
 
75
+ img_height.change(
76
+ fn=lambda h, ar: update_dimensions(h, ar, "height"),
77
+ inputs=[img_height, aspect_ratio],
78
+ outputs=[img_width, img_height]
79
+ )
80
 
81
+ process_button.click(
82
+ process_images,
83
+ inputs=[content_img, style_img, epochs, steps_per_epoch, learning_rate, content_loss_factor, style_loss_factor, img_size, img_width, img_height],
84
+ outputs=[output_img]
85
+ )
86
+
87
+ demo.launch()
88
+
89
+ if __name__ == "__main__":
90
+ create_app()