bkhmsi commited on
Commit
5f3f221
1 Parent(s): ed088d2

added support to add more fonts

Browse files
app.py CHANGED
@@ -25,6 +25,8 @@ sys.path.append("/home/user/.local/lib/python3.10/site-packages/diffvg-0.0.1-py3
25
 
26
  os.chdir('/home/user/app')
27
 
 
 
28
  import torch
29
  from diffusers import StableDiffusionPipeline
30
 
@@ -72,6 +74,26 @@ warnings.filterwarnings("ignore")
72
  pydiffvg.set_print_timing(False)
73
  gamma = 1.0
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  def set_config(semantic_concept, word, script, prompt_suffix, font_name, num_steps, seed, is_seed_rand, dist_loss_weight, pixel_dist_kernel_blur, pixel_dist_sigma, angeles_w):
77
 
@@ -99,18 +121,13 @@ def set_config(semantic_concept, word, script, prompt_suffix, font_name, num_ste
99
  cfg.optimized_letter = word
100
  cfg.script = script.lower()
101
 
102
- if cfg.script == "simplified chinese":
103
- cfg.script = "chinese"
104
-
105
- script_path = f"code/data/fonts/{cfg.script}"
106
- cfg.font = [x for x in os.listdir(script_path) if "ttf" in x][0][:-4]
107
 
108
  if is_seed_rand == "Random Seed":
109
  cfg.seed = np.random.randint(10000)
110
  else:
111
  cfg.seed = int(seed)
112
 
113
-
114
  cfg.num_iter = num_steps
115
  cfg.batch_size = 1
116
  cfg.loss.tone.dist_loss_weight = int(dist_loss_weight)
@@ -118,7 +135,6 @@ def set_config(semantic_concept, word, script, prompt_suffix, font_name, num_ste
118
  cfg.loss.tone.pixel_dist_sigma = int(pixel_dist_sigma)
119
  cfg.loss.conformal.angeles_w = angeles_w
120
 
121
-
122
  cfg.caption = f"a {cfg.semantic_concept}. {cfg.prompt_suffix}"
123
  cfg.log_dir = f"{cfg.script}"
124
  if cfg.optimized_letter in cfg.word:
@@ -171,12 +187,37 @@ def init_shapes(svg_path, trainable: Mapping[str, bool]):
171
  return shapes_init, shape_groups_init, parameters
172
 
173
 
174
- def run_main_ex(word, semantic_concept, script, num_steps, seed):
175
  prompt_suffix = "minimal flat 2d vector. lineal color. trending on artstation"
176
  is_seed_rand = "Use Set Value"
177
- return list(next(run_main_app(semantic_concept, word, script, prompt_suffix, font_name, num_steps, seed, is_seed_rand, 100, 201, 30, 0.5, 1)))
178
 
179
- def run_main_app(semantic_concept, word, script, prompt_suffix, font_name, num_steps, seed, is_seed_rand, dist_loss_weight, pixel_dist_kernel_blur, pixel_dist_sigma, angeles_w, example=0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  cfg = set_config(semantic_concept, word, script, prompt_suffix, font_name, num_steps, seed, is_seed_rand, dist_loss_weight, pixel_dist_kernel_blur, pixel_dist_sigma, angeles_w)
182
 
@@ -247,8 +288,8 @@ def run_main_app(semantic_concept, word, script, prompt_suffix, font_name, num_s
247
 
248
  if step % skip == 0:
249
  img_tensor = x.detach().cpu()
250
- img_tensor = torch.nn.functional.interpolate(img_tensor, size=(200, 200), mode='bicubic', align_corners=False)
251
- img_tensor = img_tensor.permute(0, 3, 2, 1).squeeze(0)
252
  gif_frames += [img_tensor.numpy()]
253
 
254
  x = x.repeat(cfg.batch_size, 1, 1, 1)
@@ -278,13 +319,13 @@ def run_main_app(semantic_concept, word, script, prompt_suffix, font_name, num_s
278
  # for frame in gif_frames: writer.append_data(frame)
279
  # writer.close()
280
  gif_frames = np.array(gif_frames) * 255
281
- print(gif_frames[0])
282
  imageio.mimsave(filename, gif_frames.astype(np.uint8))
283
  # imageio.mimsave(filename, np.array(gif_frames))
284
 
285
  yield gr.update(value=filename_init,visible=True),gr.update(visible=False),gr.update(value=filename,visible=True),gr.update(value=cfg.caption,visible=True),gr.update(value=cfg.seed,visible=True)
286
 
287
 
 
288
  with gr.Blocks() as demo:
289
 
290
  gr.HTML(TITLE)
@@ -307,12 +348,21 @@ with gr.Blocks() as demo:
307
  'Enter a semantic concept that you want your text to morph into (in English). For example: cat'
308
  )
309
 
310
- script = gr.Dropdown(
311
- ["Arabic", "Simplified Chinese", "Cyrillic", "Greek", "Latin", "Tamil"],
312
- value="Arabic",
313
- label="Font Script"
314
- )
315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
 
317
  prompt_suffix = gr.Text(
318
  label='Prompt Suffix',
@@ -358,15 +408,7 @@ with gr.Blocks() as demo:
358
  maximum=500,
359
  step=10,
360
  value=250)
361
-
362
- font_name = gr.Text(value=None,visible=False,label="Font Name")
363
-
364
-
365
- def on_select(evt: gr.SelectData):
366
- return evt.value
367
-
368
- font_name.value = "ArefRuqaa"
369
-
370
  run = gr.Button('Generate')
371
 
372
  with gr.Column():
@@ -395,7 +437,7 @@ with gr.Blocks() as demo:
395
  with gr.Row():
396
  # examples
397
  examples = [
398
- ["قطة", "Cat", "Arabic", 50, 42],
399
  # ["猫", "Cat", "Simplified Chinese", 250, 42],
400
  # ["γάτα", "Cat", "Greek", 250, 42],
401
  # ["кошка", "Cat", "Cyrillic", 250, 42],
@@ -407,7 +449,8 @@ with gr.Blocks() as demo:
407
  inputs=[
408
  word,
409
  semantic_concept,
410
- script,
 
411
  num_steps,
412
  seed
413
  ],
@@ -426,9 +469,9 @@ with gr.Blocks() as demo:
426
  inputs = [
427
  semantic_concept,
428
  word,
429
- script,
 
430
  prompt_suffix,
431
- font_name,
432
  num_steps,
433
  seed,
434
  is_seed_rand,
 
25
 
26
  os.chdir('/home/user/app')
27
 
28
+ # os.system('bash code/data/fonts/arabic/download_fonts.sh')
29
+
30
  import torch
31
  from diffusers import StableDiffusionPipeline
32
 
 
74
  pydiffvg.set_print_timing(False)
75
  gamma = 1.0
76
 
77
+ def read_font_names(all_scripts):
78
+
79
+ font_names = []
80
+ font_dict = {}
81
+ for script in all_scripts:
82
+ script = script.lower()
83
+ font_dict[script] = []
84
+ if script == "simplified chinese":
85
+ script = "chinese"
86
+
87
+ path = f"code/data/fonts/{script.lower()}/font_names.txt"
88
+ if not os.path.exists(path):
89
+ font_dict[script] = [x[:-4] for x in os.listdir(os.path.dirname(path)) if "ttf" in x]
90
+ else:
91
+ with open(path, 'r', encoding="utf-8") as fin:
92
+ font_dict[script] = [line.strip() for line in fin.readlines()]
93
+
94
+ font_names.extend([f"{script.capitalize()}: {f}" for f in font_dict[script]])
95
+
96
+ return ["Default"] + sorted(font_names), font_dict
97
 
98
  def set_config(semantic_concept, word, script, prompt_suffix, font_name, num_steps, seed, is_seed_rand, dist_loss_weight, pixel_dist_kernel_blur, pixel_dist_sigma, angeles_w):
99
 
 
121
  cfg.optimized_letter = word
122
  cfg.script = script.lower()
123
 
124
+ cfg.font = font_name
 
 
 
 
125
 
126
  if is_seed_rand == "Random Seed":
127
  cfg.seed = np.random.randint(10000)
128
  else:
129
  cfg.seed = int(seed)
130
 
 
131
  cfg.num_iter = num_steps
132
  cfg.batch_size = 1
133
  cfg.loss.tone.dist_loss_weight = int(dist_loss_weight)
 
135
  cfg.loss.tone.pixel_dist_sigma = int(pixel_dist_sigma)
136
  cfg.loss.conformal.angeles_w = angeles_w
137
 
 
138
  cfg.caption = f"a {cfg.semantic_concept}. {cfg.prompt_suffix}"
139
  cfg.log_dir = f"{cfg.script}"
140
  if cfg.optimized_letter in cfg.word:
 
187
  return shapes_init, shape_groups_init, parameters
188
 
189
 
190
+ def run_main_ex(word, semantic_concept, script, font_selector, num_steps, seed):
191
  prompt_suffix = "minimal flat 2d vector. lineal color. trending on artstation"
192
  is_seed_rand = "Use Set Value"
193
+ return list(next(run_main_app(semantic_concept, word, script, font_selector, prompt_suffix, num_steps, seed, is_seed_rand, 100, 201, 30, 0.5, 1)))
194
 
195
+ def run_main_app(semantic_concept, word, script, font_selected, prompt_suffix, num_steps, seed, is_seed_rand, dist_loss_weight, pixel_dist_kernel_blur, pixel_dist_sigma, angeles_w, example=0):
196
+
197
+ if font_selected.lower() != "default":
198
+ font_key, font_val = font_selected.lower().split(":")
199
+ font_key = font_key.strip()
200
+ font_val = font_val.strip()
201
+ else:
202
+ font_key = "default"
203
+ font_val = "default"
204
+
205
+ if script.lower() == "simplified chinese":
206
+ script = "chinese"
207
+
208
+ if font_key != script.lower():
209
+ print(f"Setting font to {script} default font")
210
+ font_key = script.lower()
211
+
212
+ if len(font_dict[font_key]) == 1:
213
+ font_name = font_dict[font_key][0]
214
+ else:
215
+ if font_val == "default":
216
+ font_name = "00"
217
+ else:
218
+ font_name = str(font_dict[font_key].index(font_val)).zfill(2)
219
+
220
+ print(font_name)
221
 
222
  cfg = set_config(semantic_concept, word, script, prompt_suffix, font_name, num_steps, seed, is_seed_rand, dist_loss_weight, pixel_dist_kernel_blur, pixel_dist_sigma, angeles_w)
223
 
 
288
 
289
  if step % skip == 0:
290
  img_tensor = x.detach().cpu()
291
+ img_tensor = torch.nn.functional.interpolate(img_tensor, size=(300, 300), mode='bilinear', align_corners=False)
292
+ img_tensor = img_tensor.permute(0, 2, 3, 1).squeeze(0)
293
  gif_frames += [img_tensor.numpy()]
294
 
295
  x = x.repeat(cfg.batch_size, 1, 1, 1)
 
319
  # for frame in gif_frames: writer.append_data(frame)
320
  # writer.close()
321
  gif_frames = np.array(gif_frames) * 255
 
322
  imageio.mimsave(filename, gif_frames.astype(np.uint8))
323
  # imageio.mimsave(filename, np.array(gif_frames))
324
 
325
  yield gr.update(value=filename_init,visible=True),gr.update(visible=False),gr.update(value=filename,visible=True),gr.update(value=cfg.caption,visible=True),gr.update(value=cfg.seed,visible=True)
326
 
327
 
328
+ all_scripts = ["Arabic", "Simplified Chinese", "Cyrillic", "Greek", "Latin", "Tamil"]
329
  with gr.Blocks() as demo:
330
 
331
  gr.HTML(TITLE)
 
348
  'Enter a semantic concept that you want your text to morph into (in English). For example: cat'
349
  )
350
 
351
+ with gr.Row():
 
 
 
 
352
 
353
+ script_selector = gr.Dropdown(
354
+ all_scripts,
355
+ value="Arabic",
356
+ label="Font Script"
357
+ )
358
+
359
+ font_names, font_dict = read_font_names(all_scripts)
360
+ font_selector = gr.Dropdown(
361
+ font_names,
362
+ value=font_names[0],
363
+ label="Font Name",
364
+ visible=True,
365
+ )
366
 
367
  prompt_suffix = gr.Text(
368
  label='Prompt Suffix',
 
408
  maximum=500,
409
  step=10,
410
  value=250)
411
+
 
 
 
 
 
 
 
 
412
  run = gr.Button('Generate')
413
 
414
  with gr.Column():
 
437
  with gr.Row():
438
  # examples
439
  examples = [
440
+ ["قطة", "Cat", "Arabic", "Arabic: ArefRuqaa-Regular", 100, 42],
441
  # ["猫", "Cat", "Simplified Chinese", 250, 42],
442
  # ["γάτα", "Cat", "Greek", 250, 42],
443
  # ["кошка", "Cat", "Cyrillic", 250, 42],
 
449
  inputs=[
450
  word,
451
  semantic_concept,
452
+ script_selector,
453
+ font_selector,
454
  num_steps,
455
  seed
456
  ],
 
469
  inputs = [
470
  semantic_concept,
471
  word,
472
+ script_selector,
473
+ font_selector,
474
  prompt_suffix,
 
475
  num_steps,
476
  seed,
477
  is_seed_rand,
code/data/fonts/arabic/01.ttf ADDED
Binary file (114 kB). View file
 
code/data/fonts/arabic/02.ttf ADDED
Binary file (208 kB). View file
 
code/data/fonts/arabic/03.ttf ADDED
Binary file (103 kB). View file
 
code/data/fonts/arabic/04.ttf ADDED
Binary file (669 kB). View file
 
code/data/fonts/arabic/05.ttf ADDED
Binary file (48.9 kB). View file
 
code/data/fonts/arabic/06.ttf ADDED
Binary file (125 kB). View file
 
code/data/fonts/arabic/07.ttf ADDED
Binary file (118 kB). View file
 
code/data/fonts/arabic/08.ttf ADDED
Binary file (124 kB). View file
 
code/data/fonts/arabic/09.ttf ADDED
Binary file (111 kB). View file
 
code/data/fonts/arabic/10.ttf ADDED
Binary file (79 kB). View file
 
code/data/fonts/arabic/11.ttf ADDED
Binary file (89.8 kB). View file
 
code/data/fonts/arabic/12.ttf ADDED
Binary file (246 kB). View file
 
code/data/fonts/arabic/13.ttf ADDED
Binary file (39.1 kB). View file
 
code/data/fonts/arabic/14.ttf ADDED
Binary file (87.1 kB). View file
 
code/data/fonts/arabic/15.ttf ADDED
Binary file (80.9 kB). View file
 
code/data/fonts/arabic/16.ttf ADDED
Binary file (83.5 kB). View file
 
code/data/fonts/arabic/17.ttf ADDED
Binary file (30.8 kB). View file
 
code/data/fonts/arabic/18.ttf ADDED
Binary file (50.6 kB). View file
 
code/data/fonts/arabic/download_fonts.sh CHANGED
@@ -1,20 +1,20 @@
 
 
1
  wget https://arbfonts.com//wp-content/fonts/diwany-arabic-fonts//mcs-diwany-jaly-s-u.ttf -O 01.ttf
2
  wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//46-Diwani-Bent.ttf -O 02.ttf
3
  wget https://arbfonts.com//wp-content/fonts/diwany-arabic-fonts//diwany-edited.ttf -O 03.ttf
4
  wget https://arbfonts.com/wp-content/fonts/diwany-arabic-fonts//arbfonts-samt-7017.ttf -O 04.ttf
5
  wget https://arbfonts.com//wp-content/fonts/kufi-arabic-fonts//QadasiRegular.ttf -O 05.ttf
6
  wget https://arbfonts.com//wp-content/fonts/kufi-arabic-fonts//Spirit-Of-Doha-Black.otf -O 06.ttf
7
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//AlQalam-alavi.ttf -O 07.ttf
8
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//22-andlso.ttf -O 08.ttf
9
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//ArefRuqaa-Bold-1.ttf -O 09.ttf
10
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//ArefRuqaa-Regular-1.ttf -O 10.ttf
11
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//117-Barada-Reqa.ttf -O 11.ttf
12
- wget https://arbfonts.com/wp-content/fonts/diwany-arabic-fonts//arbfonts-diwany-thuluth.ttf -O 12.ttf
13
- wget https://arbfonts.com//wp-content/fonts/unlimited-free-arabic-fonts//UthmanicHafs1-Ver09_2.otf -O 13.ttf
14
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//ALAMERAH-Thin.ttf -O 14.ttf
15
- wget https://arbfonts.com/font_files/horr/unicode/Boahmed%20Alhour.ttf -O 15.ttf
16
- wget https://arbfonts.com//wp-content/fonts/misc//K-Kamran.ttf -O 16.ttf
17
- wget https://arbfonts.com//wp-content/fonts/farsi-free-fonts//Jamil-nory.ttf -O 17.ttf
18
- wget https://arbfonts.com//wp-content/fonts/brands-arasbic-fonts//Mobily.ttf -O 18.ttf
19
- wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//QTSManga-Regular-1.ttf -O 19.ttf
20
- wget https://arbfonts.com//wp-content/fonts/arabic-fonts/new//Al-Jazeera-Arabic-Regular.ttf -O 20.ttf
 
1
+ #!/bin/sh
2
+
3
  wget https://arbfonts.com//wp-content/fonts/diwany-arabic-fonts//mcs-diwany-jaly-s-u.ttf -O 01.ttf
4
  wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//46-Diwani-Bent.ttf -O 02.ttf
5
  wget https://arbfonts.com//wp-content/fonts/diwany-arabic-fonts//diwany-edited.ttf -O 03.ttf
6
  wget https://arbfonts.com/wp-content/fonts/diwany-arabic-fonts//arbfonts-samt-7017.ttf -O 04.ttf
7
  wget https://arbfonts.com//wp-content/fonts/kufi-arabic-fonts//QadasiRegular.ttf -O 05.ttf
8
  wget https://arbfonts.com//wp-content/fonts/kufi-arabic-fonts//Spirit-Of-Doha-Black.otf -O 06.ttf
9
+ wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//22-andlso.ttf -O 07.ttf
10
+ wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//ArefRuqaa-Bold-1.ttf -O 08.ttf
11
+ wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//ArefRuqaa-Regular-1.ttf -O 09.ttf
12
+ wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//117-Barada-Reqa.ttf -O 10.ttf
13
+ wget https://arbfonts.com/wp-content/fonts/diwany-arabic-fonts//arbfonts-diwany-thuluth.ttf -O 11.ttf
14
+ wget https://arbfonts.com//wp-content/fonts/unlimited-free-arabic-fonts//UthmanicHafs1-Ver09_2.otf -O 12.ttf
15
+ wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//ALAMERAH-Thin.ttf -O 13.ttf
16
+ wget https://arbfonts.com/font_files/horr/unicode/Boahmed%20Alhour.ttf -O 14.ttf
17
+ wget https://arbfonts.com//wp-content/fonts/misc//K-Kamran.ttf -O 15.ttf
18
+ wget https://arbfonts.com//wp-content/fonts/brands-arasbic-fonts//Mobily.ttf -O 16.ttf
19
+ wget https://arbfonts.com//wp-content/fonts/new-arabic-fonts//QTSManga-Regular-1.ttf -O 17.ttf
20
+ wget https://arbfonts.com//wp-content/fonts/arabic-fonts/new//Al-Jazeera-Arabic-Regular.ttf -O 18.ttf
 
 
code/data/fonts/arabic/font_names.txt CHANGED
@@ -1,10 +1,10 @@
 
1
  رقعة عارف
2
  ديواني جلي
3
  ديواني مشكل
4
  ديواني طويل
5
  ديواني بسيط
6
  كوفي بسيط
7
- كوفي منحني
8
  فارسي بسيط
9
  مغربي اندلس
10
  رقعة مدبب
@@ -14,7 +14,6 @@
14
  ثلث بسيط
15
  مربع بسيط
16
  حر مدبب
17
- حر بسيط
18
  حر طويل
19
  موبايلي
20
  منجا
 
1
+ ArefRuqaa-Regular
2
  رقعة عارف
3
  ديواني جلي
4
  ديواني مشكل
5
  ديواني طويل
6
  ديواني بسيط
7
  كوفي بسيط
 
8
  فارسي بسيط
9
  مغربي اندلس
10
  رقعة مدبب
 
14
  ثلث بسيط
15
  مربع بسيط
16
  حر مدبب
 
17
  حر طويل
18
  موبايلي
19
  منجا