multimodalart HF staff commited on
Commit
13587bf
1 Parent(s): ce8f63e
Files changed (1) hide show
  1. app_dialogue.py +51 -68
app_dialogue.py CHANGED
@@ -62,7 +62,7 @@ BAN_TOKENS = ( # For documentation puporse. We are not using this list, it is h
62
  EOS_STRINGS = ["<end_of_utterance>", "\nUser:"]
63
  STOP_SUSPECT_LIST = []
64
 
65
- GRADIO_LINK = "https://multimodalart-ai-meme-generator.hf.space"
66
  API_TOKEN = os.getenv("HF_AUTH_TOKEN")
67
  IDEFICS_LOGO = "https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/IDEFICS_logo.png"
68
 
@@ -152,7 +152,7 @@ for image_dir in os.listdir("example_images"):
152
  tmp_filename = H.preprocess(H.value)
153
  DEFAULT_IMAGES_TMP_PATH_TO_URL[
154
  tmp_filename
155
- ] = f"https://huggingface.co/spaces/multimodalart/AI_Meme_Generator/resolve/main/example_images/{image_dir}/{im_path}"
156
 
157
 
158
  # Utils to handle the image markdown display logic
@@ -355,6 +355,39 @@ def resize_with_ratio(image: PIL.Image.Image, fixed_width: int) -> PIL.Image.Ima
355
 
356
  return resized_img
357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
 
359
  def test_font_size(
360
  draw,
@@ -368,75 +401,25 @@ def test_font_size(
368
  ):
369
  text_width = draw.textlength(text, font)
370
  text_is_too_long = True
371
-
372
- if num_lines == 1:
373
- while font.size > min_font and text_is_too_long:
374
- font = ImageFont.truetype(
375
- f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction
376
- )
377
  text_width = draw.textlength(text, font)
378
  text_is_too_long = text_width > image.width
379
-
380
- elif num_lines == 2:
381
- while font.size > min_font and text_is_too_long:
382
- font = ImageFont.truetype(
383
- f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction
384
- )
385
- max_len_increment = 0
386
- while (
387
- text_is_too_long
388
- and max_len_increment < 10
389
- and max_len_increment < (len(text)) // 2
390
- ):
391
- temp_text = insert_backslash(
392
- text, max_length=(len(text) + max_len_increment) // 2
393
- )
394
- first_line, second_line = (
395
- temp_text.split("\n")[0],
396
- temp_text.split("\n")[1],
397
- )
398
- text_width = max(
399
- draw.textlength(first_line, font),
400
- draw.textlength(second_line, font),
401
- )
402
- text_is_too_long = text_width > image.width
403
- max_len_increment += 1
404
-
405
- elif num_lines == 3:
406
- while font.size > min_font and text_is_too_long:
407
- font = ImageFont.truetype(
408
- f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction
409
  )
410
- max_len_incr_1_split = 0
411
- while text_is_too_long and max_len_incr_1_split < 10:
412
- first_temp_text = insert_backslash(
413
- text, max_length=(len(text) + max_len_incr_1_split) // 3
414
- )
415
- first_line, second_line = (
416
- first_temp_text.split("\n")[0],
417
- first_temp_text.split("\n")[1],
418
- )
419
- max_len_incr_2_split = 0
420
- while text_is_too_long and max_len_incr_2_split < 10:
421
- temp_text_second_line = insert_backslash(
422
- second_line,
423
- max_length=(len(second_line) + max_len_incr_2_split) // 2,
424
- )
425
- second_line_1, second_line_2 = (
426
- temp_text_second_line.split("\n")[0],
427
- temp_text_second_line.split("\n")[1],
428
- )
429
- temp_text = first_line + "\n" + second_line_1 + "\n" + second_line_2
430
- text_width = max(
431
- draw.textlength(first_line, font),
432
- draw.textlength(second_line_1, font),
433
- draw.textlength(second_line_2, font),
434
- )
435
- text_is_too_long = text_width > image.width
436
- max_len_incr_2_split += 1
437
- max_len_incr_1_split += 1
438
- else:
439
- raise (ValueError("num_lines can only be 1, 2 or 3"))
440
 
441
  if not text_is_too_long and num_lines > 1:
442
  text = temp_text
 
62
  EOS_STRINGS = ["<end_of_utterance>", "\nUser:"]
63
  STOP_SUSPECT_LIST = []
64
 
65
+ GRADIO_LINK = "https://huggingfacem4-ai-meme-generator.hf.space"
66
  API_TOKEN = os.getenv("HF_AUTH_TOKEN")
67
  IDEFICS_LOGO = "https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/IDEFICS_logo.png"
68
 
 
152
  tmp_filename = H.preprocess(H.value)
153
  DEFAULT_IMAGES_TMP_PATH_TO_URL[
154
  tmp_filename
155
+ ] = f"https://huggingface.co/spaces/HuggingFaceM4/AI_Meme_Generator/resolve/main/example_images/{image_dir}/{im_path}"
156
 
157
 
158
  # Utils to handle the image markdown display logic
 
355
 
356
  return resized_img
357
 
358
+ def make_new_lines(draw, image, font, text_is_too_long, lines, num_lines, num_loops):
359
+ max_len_increment = 0
360
+ while text_is_too_long and max_len_increment < 10:
361
+ new_lines = lines.copy()
362
+ last_line_with_backslash = insert_backslash(
363
+ new_lines[-1],
364
+ max_length=(len(new_lines[-1]) + max_len_increment)
365
+ // (num_lines - num_loops),
366
+ )
367
+ penultimate_line, last_line = (
368
+ last_line_with_backslash.split("\n")[0],
369
+ last_line_with_backslash.split("\n")[1],
370
+ )
371
+ new_lines.pop(-1)
372
+ new_lines.append(penultimate_line)
373
+ new_lines.append(last_line)
374
+ # If the we haven't reached the last line, we split it again
375
+ if len(new_lines) < num_lines:
376
+ new_lines, text_width, text_is_too_long = make_new_lines(
377
+ draw=draw,
378
+ image=image,
379
+ font=font,
380
+ text_is_too_long=text_is_too_long,
381
+ lines=new_lines,
382
+ num_lines=num_lines,
383
+ num_loops=num_loops + 1,
384
+ )
385
+ text_width = max([draw.textlength(line, font) for line in new_lines])
386
+ text_is_too_long = text_width > image.width
387
+ max_len_increment += 1
388
+ if not text_is_too_long:
389
+ lines = new_lines
390
+ return lines, text_width, text_is_too_long
391
 
392
  def test_font_size(
393
  draw,
 
401
  ):
402
  text_width = draw.textlength(text, font)
403
  text_is_too_long = True
404
+ lines = [text]
405
+ while font.size > min_font and text_is_too_long:
406
+ font = ImageFont.truetype(
407
+ f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction
408
+ )
409
+ if num_lines == 1:
410
  text_width = draw.textlength(text, font)
411
  text_is_too_long = text_width > image.width
412
+ else:
413
+ lines, text_width, text_is_too_long = make_new_lines(
414
+ draw=draw,
415
+ image=image,
416
+ font=font,
417
+ text_is_too_long=text_is_too_long,
418
+ lines=lines,
419
+ num_lines=num_lines,
420
+ num_loops=0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
  )
422
+ temp_text = "\n".join(lines)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
 
424
  if not text_is_too_long and num_lines > 1:
425
  text = temp_text