brayden-gg commited on
Commit
5e527f9
1 Parent(s): 323e91a

added SVG button for speed

Browse files
Files changed (2) hide show
  1. .vscode/launch.json +16 -0
  2. app.py +57 -35
.vscode/launch.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Python: Current File",
9
+ "type": "python",
10
+ "request": "launch",
11
+ "program": "${file}",
12
+ "console": "integratedTerminal",
13
+ "justMyCode": true
14
+ }
15
+ ]
16
+ }
app.py CHANGED
@@ -36,7 +36,7 @@ writer_mean_Ws = []
36
  all_word_writer_Ws = []
37
  all_word_writer_Cs = []
38
  writer_weight = 0.7
39
-
40
 
41
  # data for char interpolation
42
  blend_chars = ["y", "s"]
@@ -45,14 +45,14 @@ char_weight = 0.7
45
  default_mean_global_W = convenience.get_mean_global_W(net, default_loaded_data, device)
46
  char_Ws = default_mean_global_W.reshape(1, 1, convenience.L)
47
  char_Cs = all_Cs = torch.zeros(1, 2, convenience.L, convenience.L)
48
-
49
 
50
  # data for MDN
51
  mdn_words = ["hello", "world"]
52
  mdn_mean_global_W = None
53
  all_word_mdn_Ws = []
54
  all_word_mdn_Cs = []
55
-
56
 
57
  def update_writer_word(target_word):
58
  writer_words.clear()
@@ -72,13 +72,13 @@ def update_writer_word(target_word):
72
  # for writer interpolation
73
  def update_writer_slider(val):
74
  global writer_weight
 
75
  writer_weight = val
76
  weights = [1 - writer_weight, writer_weight]
77
 
78
  net.clamp_mdn = 0
79
- svg = convenience.draw_words_svg(writer_words, all_word_writer_Ws, all_word_writer_Cs, weights, net)
80
- svg.saveas("DSD_writer_interpolation.svg")
81
- return gr.HTML.update(value=svg.tostring()), gr.File.update(value="./DSD_writer_interpolation.svg")
82
 
83
 
84
  def update_chosen_writers(writer1, writer2):
@@ -97,22 +97,26 @@ def update_chosen_writers(writer1, writer2):
97
 
98
  return gr.Slider.update(label=f"{writer1} vs. {writer2}"), *update_writer_slider(writer_weight)
99
 
100
- # for character blend
 
 
101
 
 
102
 
103
  def update_char_slider(weight):
104
  """Generates an image of handwritten text based on target_sentence"""
 
 
 
105
  net.clamp_mdn = 0
106
 
107
- global char_weight
108
  char_weight = weight
109
  character_weights = [1 - weight, weight]
110
 
111
  all_W_c = convenience.get_character_blend_W_c(character_weights, char_Ws, char_Cs)
112
  all_commands = convenience.get_commands(net, blend_chars[0], all_W_c)
113
- svg = convenience.commands_to_svg(all_commands, 750, 160, 375)
114
- svg.saveas("DSD_char_interpolation.svg")
115
- return gr.HTML.update(value=svg.tostring()), gr.File.update(value="./DSD_char_interpolation.svg")
116
 
117
 
118
  def update_blend_chars(c1, c2):
@@ -125,6 +129,10 @@ def update_blend_chars(c1, c2):
125
 
126
  return gr.Slider.update(label=f"'{c1}' vs. '{c2}'")
127
 
 
 
 
 
128
  # for MDN
129
 
130
 
@@ -144,18 +152,20 @@ def update_mdn_word(target_word):
144
 
145
 
146
  def sample_mdn(maxs, maxr):
 
147
  net.clamp_mdn = maxr
148
  net.scale_sd = maxs
149
- svg = convenience.draw_words_svg(mdn_words, all_word_mdn_Ws, all_word_mdn_Cs, [1], net)
150
- svg.saveas("DSD_add_randomness.svg")
151
- return gr.HTML.update(value=svg.tostring()), gr.File.update(value="./DSD_add_randomness.svg")
152
 
 
 
 
153
 
154
  update_writer_word(" ".join(writer_words))
155
  update_chosen_writers(f"Writer {chosen_writers[0]}", f"Writer {chosen_writers[1]}")
156
 
157
  update_mdn_word(" ".join(writer_words))
158
-
159
  update_blend_chars(*blend_chars)
160
 
161
  with gr.Blocks() as demo:
@@ -169,41 +179,50 @@ with gr.Blocks() as demo:
169
  writer1 = gr.Radio(left_ratio_options, value="Style 120", label="Style for first writer")
170
  with gr.Column():
171
  writer2 = gr.Radio(right_ratio_options, value="Style 80", label="Style for second writer")
172
- with gr.Row():
173
- writer_slider = gr.Slider(0, 1, value=writer_weight, label="Style 120 vs. Style 80")
174
  with gr.Row():
175
  writer_submit = gr.Button("Submit")
 
 
176
  with gr.Row():
177
  writer_default_image = update_writer_slider(writer_weight)
178
  writer_output = gr.HTML(writer_default_image[0]["value"])
179
  with gr.Row():
180
- writer_download = gr.File("./DSD_writer_interpolation.svg", interactive=False, show_label=False)
181
- writer_submit.click(fn=update_writer_slider, inputs=[writer_slider], outputs=[writer_output, writer_download], show_progress=False)
182
- writer_slider.change(fn=update_writer_slider, inputs=[writer_slider], outputs=[writer_output, writer_download], show_progress=False)
183
- target_word.submit(fn=update_writer_word, inputs=[target_word], outputs=[writer_output, writer_download], show_progress=False)
184
-
185
- writer1.change(fn=update_chosen_writers, inputs=[writer1, writer2], outputs=[writer_slider, writer_output, writer_download])
186
- writer2.change(fn=update_chosen_writers, inputs=[writer1, writer2], outputs=[writer_slider, writer_output, writer_download])
187
-
 
 
188
  with gr.TabItem("Blend Characters"):
189
  with gr.Row():
190
  with gr.Column():
191
  char1 = gr.Dropdown(choices=avail_char_list, value=blend_chars[0], label="Character 1")
192
  with gr.Column():
193
  char2 = gr.Dropdown(choices=avail_char_list, value=blend_chars[1], label="Character 2")
 
 
194
  with gr.Row():
195
  char_slider = gr.Slider(0, 1, value=char_weight, label=f"'{blend_chars[0]}' vs. '{blend_chars[1]}'")
196
  with gr.Row():
197
  char_default_image = update_char_slider(char_weight)
198
  char_output = gr.HTML(char_default_image[0]["value"])
199
  with gr.Row():
200
- char_download = gr.File("./DSD_char_interpolation.svg", interactive=False, show_label=False)
 
 
 
201
 
202
- char_slider.change(fn=update_char_slider, inputs=[char_slider], outputs=[char_output, char_download], show_progress=False)
 
203
 
204
- char1.change(fn=update_blend_chars, inputs=[char1, char2], outputs=[char_slider, char_download])
205
- char2.change(fn=update_blend_chars, inputs=[char1, char2], outputs=[char_slider, char_download])
206
 
 
 
207
  with gr.TabItem("Add Randomness"):
208
  mdn_word = gr.Textbox(label="Target Word", value=" ".join(mdn_words), max_lines=1)
209
  with gr.Row():
@@ -212,16 +231,19 @@ with gr.Blocks() as demo:
212
  with gr.Column():
213
  scale_rand = gr.Slider(0, 3, value=net.scale_sd, label="Scale of Randomness")
214
  with gr.Row():
215
- mdn_sample_button = gr.Button(value="Resample!")
216
  with gr.Row():
217
  default_im = sample_mdn(net.scale_sd, net.clamp_mdn)
218
  mdn_output = gr.HTML(default_im[0]["value"])
219
  with gr.Row():
220
- randomness_download = gr.File("./DSD_add_randomness.svg", interactive=False, show_label=False)
 
221
 
222
- max_rand.change(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output, randomness_download], show_progress=False)
223
- scale_rand.change(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output, randomness_download], show_progress=False)
224
- mdn_sample_button.click(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output, randomness_download], show_progress=False)
225
- mdn_word.submit(fn=update_mdn_word, inputs=[mdn_word], outputs=[mdn_output, randomness_download], show_progress=False)
226
 
 
 
227
  demo.launch()
 
36
  all_word_writer_Ws = []
37
  all_word_writer_Cs = []
38
  writer_weight = 0.7
39
+ writer_svg = None
40
 
41
  # data for char interpolation
42
  blend_chars = ["y", "s"]
 
45
  default_mean_global_W = convenience.get_mean_global_W(net, default_loaded_data, device)
46
  char_Ws = default_mean_global_W.reshape(1, 1, convenience.L)
47
  char_Cs = all_Cs = torch.zeros(1, 2, convenience.L, convenience.L)
48
+ char_svg = None
49
 
50
  # data for MDN
51
  mdn_words = ["hello", "world"]
52
  mdn_mean_global_W = None
53
  all_word_mdn_Ws = []
54
  all_word_mdn_Cs = []
55
+ mdn_svg = None
56
 
57
  def update_writer_word(target_word):
58
  writer_words.clear()
 
72
  # for writer interpolation
73
  def update_writer_slider(val):
74
  global writer_weight
75
+ global writer_svg
76
  writer_weight = val
77
  weights = [1 - writer_weight, writer_weight]
78
 
79
  net.clamp_mdn = 0
80
+ writer_svg = convenience.draw_words_svg(writer_words, all_word_writer_Ws, all_word_writer_Cs, weights, net)
81
+ return gr.HTML.update(value=writer_svg.tostring()), gr.Slider.update(visible=False), gr.Button.update(visible=True)
 
82
 
83
 
84
  def update_chosen_writers(writer1, writer2):
 
97
 
98
  return gr.Slider.update(label=f"{writer1} vs. {writer2}"), *update_writer_slider(writer_weight)
99
 
100
+ def update_writer_download():
101
+ writer_svg.saveas("./DSD_writer_interpolation.svg")
102
+ return gr.File.update(value="./DSD_writer_interpolation.svg", visible=True), gr.Button.update(visible=False)
103
 
104
+ # for character blend
105
 
106
  def update_char_slider(weight):
107
  """Generates an image of handwritten text based on target_sentence"""
108
+ global char_weight
109
+ global char_svg
110
+
111
  net.clamp_mdn = 0
112
 
 
113
  char_weight = weight
114
  character_weights = [1 - weight, weight]
115
 
116
  all_W_c = convenience.get_character_blend_W_c(character_weights, char_Ws, char_Cs)
117
  all_commands = convenience.get_commands(net, blend_chars[0], all_W_c)
118
+ char_svg = convenience.commands_to_svg(all_commands, 750, 160, 375)
119
+ return gr.HTML.update(value=char_svg.tostring()), gr.Slider.update(visible=False), gr.Button.update(visible=True)
 
120
 
121
 
122
  def update_blend_chars(c1, c2):
 
129
 
130
  return gr.Slider.update(label=f"'{c1}' vs. '{c2}'")
131
 
132
+ def update_char_download():
133
+ char_svg.saveas("./DSD_char_interpolation.svg")
134
+ return gr.File.update(value="./DSD_char_interpolation.svg", visible=True), gr.Button.update(visible=False)
135
+
136
  # for MDN
137
 
138
 
 
152
 
153
 
154
  def sample_mdn(maxs, maxr):
155
+ global mdn_svg
156
  net.clamp_mdn = maxr
157
  net.scale_sd = maxs
158
+ mdn_svg = convenience.draw_words_svg(mdn_words, all_word_mdn_Ws, all_word_mdn_Cs, [1], net)
159
+ return gr.HTML.update(value=mdn_svg.tostring()), gr.Slider.update(visible=False), gr.Button.update(visible=True)
 
160
 
161
+ def update_mdn_download():
162
+ mdn_svg.saveas("./DSD_add_randomness.svg")
163
+ return gr.File.update(value="./DSD_add_randomness.svg", visible=True), gr.Button.update(visible=False)
164
 
165
  update_writer_word(" ".join(writer_words))
166
  update_chosen_writers(f"Writer {chosen_writers[0]}", f"Writer {chosen_writers[1]}")
167
 
168
  update_mdn_word(" ".join(writer_words))
 
169
  update_blend_chars(*blend_chars)
170
 
171
  with gr.Blocks() as demo:
 
179
  writer1 = gr.Radio(left_ratio_options, value="Style 120", label="Style for first writer")
180
  with gr.Column():
181
  writer2 = gr.Radio(right_ratio_options, value="Style 80", label="Style for second writer")
 
 
182
  with gr.Row():
183
  writer_submit = gr.Button("Submit")
184
+ with gr.Row():
185
+ writer_slider = gr.Slider(0, 1, value=writer_weight, label="Style 120 vs. Style 80")
186
  with gr.Row():
187
  writer_default_image = update_writer_slider(writer_weight)
188
  writer_output = gr.HTML(writer_default_image[0]["value"])
189
  with gr.Row():
190
+ writer_download_btn = gr.Button("Save to SVG file")
191
+ writer_download = gr.File(interactive=False, show_label=False, visible=False)
192
+ writer_submit.click(fn=update_writer_slider, inputs=[writer_slider], outputs=[writer_output, writer_download, writer_download_btn], show_progress=False)
193
+ writer_slider.change(fn=update_writer_slider, inputs=[writer_slider], outputs=[writer_output, writer_download, writer_download_btn], show_progress=False)
194
+ target_word.submit(fn=update_writer_word, inputs=[target_word], outputs=[writer_output, writer_download, writer_download_btn], show_progress=False)
195
+
196
+ writer1.change(fn=update_chosen_writers, inputs=[writer1, writer2], outputs=[writer_slider, writer_output, writer_download, writer_download_btn])
197
+ writer2.change(fn=update_chosen_writers, inputs=[writer1, writer2], outputs=[writer_slider, writer_output, writer_download, writer_download_btn])
198
+ writer_download_btn.click(fn=update_writer_download, inputs=[], outputs=[writer_download, writer_download_btn])
199
+ writer_download_btn.style(full_width="true")
200
  with gr.TabItem("Blend Characters"):
201
  with gr.Row():
202
  with gr.Column():
203
  char1 = gr.Dropdown(choices=avail_char_list, value=blend_chars[0], label="Character 1")
204
  with gr.Column():
205
  char2 = gr.Dropdown(choices=avail_char_list, value=blend_chars[1], label="Character 2")
206
+ with gr.Row():
207
+ char_submit_button = gr.Button(value="Submit")
208
  with gr.Row():
209
  char_slider = gr.Slider(0, 1, value=char_weight, label=f"'{blend_chars[0]}' vs. '{blend_chars[1]}'")
210
  with gr.Row():
211
  char_default_image = update_char_slider(char_weight)
212
  char_output = gr.HTML(char_default_image[0]["value"])
213
  with gr.Row():
214
+ char_download_btn = gr.Button("Save to SVG file")
215
+ char_download = gr.File(interactive=False, show_label=False, visible=False)
216
+
217
+ char_slider.change(fn=update_char_slider, inputs=[char_slider], outputs=[char_output, char_download, char_download_btn], show_progress=False)
218
 
219
+ char1.change(fn=update_blend_chars, inputs=[char1, char2], outputs=[char_slider])
220
+ char2.change(fn=update_blend_chars, inputs=[char1, char2], outputs=[char_slider])
221
 
222
+ char_submit_button.click(fn=update_char_slider, inputs=[char_slider], outputs=[char_output, char_download, char_download_btn], show_progress=False)
 
223
 
224
+ char_download_btn.click(fn=update_char_download, inputs=[], outputs=[char_download, char_download_btn], show_progress=True)
225
+ char_download_btn.style(full_width="true")
226
  with gr.TabItem("Add Randomness"):
227
  mdn_word = gr.Textbox(label="Target Word", value=" ".join(mdn_words), max_lines=1)
228
  with gr.Row():
 
231
  with gr.Column():
232
  scale_rand = gr.Slider(0, 3, value=net.scale_sd, label="Scale of Randomness")
233
  with gr.Row():
234
+ mdn_sample_button = gr.Button(value="Resample")
235
  with gr.Row():
236
  default_im = sample_mdn(net.scale_sd, net.clamp_mdn)
237
  mdn_output = gr.HTML(default_im[0]["value"])
238
  with gr.Row():
239
+ randomness_download_btn = gr.Button("Save to SVG file")
240
+ randomness_download = gr.File(interactive=False, show_label=False, visible=False)
241
 
242
+ max_rand.change(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output, randomness_download, randomness_download_btn], show_progress=False)
243
+ scale_rand.change(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output, randomness_download, randomness_download_btn], show_progress=False)
244
+ mdn_sample_button.click(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output, randomness_download, randomness_download_btn], show_progress=False)
245
+ mdn_word.submit(fn=update_mdn_word, inputs=[mdn_word], outputs=[mdn_output, randomness_download, randomness_download_btn], show_progress=False)
246
 
247
+ randomness_download_btn.click(fn=update_mdn_download, inputs=[], outputs=[randomness_download, randomness_download_btn])
248
+ randomness_download_btn.style(full_width="true")
249
  demo.launch()