Yaron Koresh commited on
Commit
af91dd8
·
verified ·
1 Parent(s): c87d615

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -615,13 +615,13 @@ def generate_random_string(length):
615
  characters = str(ascii_letters + digits)
616
  return ''.join(random.choice(characters) for _ in range(length))
617
 
618
- def pipe_generate_image(p1,p2):
619
  log(f'CALL pipe_generate')
620
  imgs = image_pipe(
621
  prompt=p1,
622
  negative_prompt=p2,
623
- height=height,
624
- width=width,
625
  guidance_scale=img_accu,
626
  num_images_per_prompt=1,
627
  num_inference_steps=image_steps,
@@ -631,28 +631,28 @@ def pipe_generate_image(p1,p2):
631
  log(f'RET pipe_generate')
632
  return imgs
633
 
634
- def add_song_cover_text(img,artist,song,height,width):
635
 
636
  draw = ImageDraw.Draw(img,mode="RGBA")
637
 
638
  labels_distance = 1/3
639
 
640
  rows = len(song.split("\n"))
641
- textheight=min(math.ceil( width / 10 ), math.ceil( height / 5 ))
642
  font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
643
  textwidth = draw.textlength(song,font)
644
- x = math.ceil((width - textwidth) / 2)
645
- y = height - (textheight * rows / 2) - (height / 2)
646
- y = math.ceil(y - (height / 2 * labels_distance))
647
  draw.text((x, y), song, (255,255,255,85), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(0,0,0,170))
648
 
649
  rows = len(artist.split("\n"))
650
- textheight=min(math.ceil( width / 10 ), math.ceil( height / 5 ))
651
  font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
652
  textwidth = draw.textlength(artist,font)
653
- x = math.ceil((width - textwidth) / 2)
654
- y = height - (textheight * rows / 2) - (height / 2)
655
- y = math.ceil(y + (height / 2 * labels_distance))
656
  draw.text((x, y), artist, (0,0,0,85), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(255,255,255,170))
657
 
658
  return img
@@ -673,9 +673,9 @@ def upscale_image(img,factor):
673
 
674
  return ret
675
 
676
- def all_pipes(pos,neg,artist,song):
677
 
678
- imgs = pipe_generate_image(pos,neg)
679
 
680
  for i in range(len(imgs)):
681
  imgs[i] = upscaler(imgs[i],upscale_factor=factor)
@@ -1294,7 +1294,7 @@ def translate(txt,to_lang="en",from_lang="auto"):
1294
  return translation.lower()
1295
 
1296
  @spaces.GPU(duration=300)
1297
- def handle_generation(artist,song,lyrics,height,width):
1298
 
1299
  log(f'CALL handle_generate')
1300
 
@@ -1333,9 +1333,9 @@ def handle_generation(artist,song,lyrics,height,width):
1333
  Negative: {neg}
1334
  """)
1335
 
1336
- img = all_pipes(pos,neg,pos_artist,pos_song)[0]
1337
 
1338
- labeled_img = add_song_cover_text(img,artist,song,height,width)
1339
  name = f'{generate_random_string(8)}.png'
1340
  labeled_img.save(name)
1341
 
 
615
  characters = str(ascii_letters + digits)
616
  return ''.join(random.choice(characters) for _ in range(length))
617
 
618
+ def pipe_generate_image(p1,p2,h,w):
619
  log(f'CALL pipe_generate')
620
  imgs = image_pipe(
621
  prompt=p1,
622
  negative_prompt=p2,
623
+ height=h,
624
+ width=w,
625
  guidance_scale=img_accu,
626
  num_images_per_prompt=1,
627
  num_inference_steps=image_steps,
 
631
  log(f'RET pipe_generate')
632
  return imgs
633
 
634
+ def add_song_cover_text(img,artist,song,h,w):
635
 
636
  draw = ImageDraw.Draw(img,mode="RGBA")
637
 
638
  labels_distance = 1/3
639
 
640
  rows = len(song.split("\n"))
641
+ textheight=min(math.ceil( w / 10 ), math.ceil( h / 5 ))
642
  font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
643
  textwidth = draw.textlength(song,font)
644
+ x = math.ceil((w - textwidth) / 2)
645
+ y = h - (textheight * rows / 2) - (h / 2)
646
+ y = math.ceil(y - (h / 2 * labels_distance))
647
  draw.text((x, y), song, (255,255,255,85), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(0,0,0,170))
648
 
649
  rows = len(artist.split("\n"))
650
+ textheight=min(math.ceil( w / 10 ), math.ceil( h / 5 ))
651
  font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
652
  textwidth = draw.textlength(artist,font)
653
+ x = math.ceil((w - textwidth) / 2)
654
+ y = h - (textheight * rows / 2) - (h / 2)
655
+ y = math.ceil(y + (h / 2 * labels_distance))
656
  draw.text((x, y), artist, (0,0,0,85), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(255,255,255,170))
657
 
658
  return img
 
673
 
674
  return ret
675
 
676
+ def all_pipes(pos,neg,artist,song,h,w):
677
 
678
+ imgs = pipe_generate_image(pos,neg,h,w)
679
 
680
  for i in range(len(imgs)):
681
  imgs[i] = upscaler(imgs[i],upscale_factor=factor)
 
1294
  return translation.lower()
1295
 
1296
  @spaces.GPU(duration=300)
1297
+ def handle_generation(artist,song,lyrics,h,w):
1298
 
1299
  log(f'CALL handle_generate')
1300
 
 
1333
  Negative: {neg}
1334
  """)
1335
 
1336
+ img = all_pipes(pos,neg,pos_artist,pos_song,h,w)[0]
1337
 
1338
+ labeled_img = add_song_cover_text(img,artist,song,h,w)
1339
  name = f'{generate_random_string(8)}.png'
1340
  labeled_img.save(name)
1341