Yaron Koresh commited on
Commit
f89f832
·
verified ·
1 Parent(s): 9225203

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -557,15 +557,18 @@ def get_tensor_length(tensor):
557
  def summarize(
558
  text, max_len=20, min_len=10
559
  ):
560
- log(f'CALL summarize_text')
561
  inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=float('inf'), truncation=False)
 
 
 
562
  i = 1
563
  while get_tensor_length(inputs) > max_len:
564
- print(f'DBG summarize_text 1 {i}')
565
  outputs = model.generate(
566
- inputs[0][:512],
567
  length_penalty=2.0,
568
- num_beams=max(8,get_tensor_length(inputs)),
569
  early_stopping=True,
570
  max_length=max( get_tensor_length(inputs) // 4 , max_len ),
571
  min_length=min_len
@@ -573,7 +576,7 @@ def summarize(
573
  inputs = torch.tensor([[*list(outputs[0]), *list(inputs[0][512:])]])
574
  i = i + 1
575
  summary = tokenizer.decode(inputs[0])
576
- log(f'RET summarize_text with summary as {summary}')
577
  return summary
578
 
579
  def generate_random_string(length):
@@ -662,11 +665,11 @@ def handle_generation(artist,song,genre,lyrics):
662
  pos_genre = ' '.join(word[0].upper() + word[1:] for word in pos_genre.split())
663
 
664
  pos_lyrics = re.sub(f'[{punctuation}]', '', re.sub("([ \t\n]){1,}", " ", lyrics)).lower().strip()
665
- pos_lyrics_sum = pos_lyrics if pos_lyrics == "" else summarize(pos_lyrics)
666
 
667
  neg = f"Sexuality, Humanity, Textual, Labeled, Distorted, Discontinuous, Blurry, Doll-Like, Overly Plastic, Low-Quality, Painted, Smoothed, Artificial, Phony, Gaudy, Digital Effects."
668
  q = "\""
669
- pos = f'HQ Hyper-realistic { translate(pos_genre) } song "{ translate(pos_song) }"{ pos_lyrics_sum if pos_lyrics_sum == "" else ": " + translate(pos_lyrics_sum) }.'
670
 
671
  print(f"""
672
  Positive: {pos}
 
557
  def summarize(
558
  text, max_len=20, min_len=10
559
  ):
560
+ log(f'CALL summarize')
561
  inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=float('inf'), truncation=False)
562
+ if get_tensor_length(inputs) < 3:
563
+ print("Summarization Error: Text is too short, 3 words minimum!")
564
+ return text
565
  i = 1
566
  while get_tensor_length(inputs) > max_len:
567
+ print(f'DBG summarize 1 {i}')
568
  outputs = model.generate(
569
+ torch.tensor(list(inputs[0][:512])),
570
  length_penalty=2.0,
571
+ num_beams=min(4,get_tensor_length(inputs) - 1),
572
  early_stopping=True,
573
  max_length=max( get_tensor_length(inputs) // 4 , max_len ),
574
  min_length=min_len
 
576
  inputs = torch.tensor([[*list(outputs[0]), *list(inputs[0][512:])]])
577
  i = i + 1
578
  summary = tokenizer.decode(inputs[0])
579
+ log(f'RET summarize with summary as {summary}')
580
  return summary
581
 
582
  def generate_random_string(length):
 
665
  pos_genre = ' '.join(word[0].upper() + word[1:] for word in pos_genre.split())
666
 
667
  pos_lyrics = re.sub(f'[{punctuation}]', '', re.sub("([ \t\n]){1,}", " ", lyrics)).lower().strip()
668
+ pos_lyrics_sum = pos_lyrics if pos_lyrics == "" else summarize(translate(pos_lyrics))
669
 
670
  neg = f"Sexuality, Humanity, Textual, Labeled, Distorted, Discontinuous, Blurry, Doll-Like, Overly Plastic, Low-Quality, Painted, Smoothed, Artificial, Phony, Gaudy, Digital Effects."
671
  q = "\""
672
+ pos = f'HQ Hyper-realistic { translate(pos_genre) } song "{ translate(pos_song) }"{ pos_lyrics_sum if pos_lyrics_sum == "" else ": " + pos_lyrics_sum }.'
673
 
674
  print(f"""
675
  Positive: {pos}