ElvisTsang commited on
Commit
5e51a2a
·
verified ·
1 Parent(s): 0c9e697

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -8,10 +8,34 @@ def img2text(url):
8
 
9
  def text2story(text):
10
  story_generator = pipeline("text-generation", model="pranavpsv/gpt2-genre-story-generator", device_map="auto", return_full_text=False)
11
- prompt = f"A short story under 100 English words about: {text}."
12
- generated = story_generator(text, max_new_tokens=140, do_sample=True, temperature=0.7)
13
  story_text = generated[0]['generated_text']
14
- return story_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def text2audio(story_text):
17
  audio_generator = pipeline("text-to-speech", model="ylacombe/vits_vctk_scottish_female")
 
8
 
9
  def text2story(text):
10
  story_generator = pipeline("text-generation", model="pranavpsv/gpt2-genre-story-generator", device_map="auto", return_full_text=False)
11
+ prompt = f"<BOS> {text}"
12
+ generated = story_generator(prompt, max_new_tokens=140, do_sample=True, temperature=0.7)
13
  story_text = generated[0]['generated_text']
14
+
15
+ # Split into sentences
16
+ sentences = re.split(r'(?<=[.!?])\s+', story_text.strip())
17
+
18
+ # Initialize variables
19
+ current_word_count = 0
20
+ final_sentences = []
21
+
22
+ # Iterate through each sentence and accumulate until the word count is within 100
23
+ for sentence in sentences:
24
+ words = sentence.split()
25
+ word_count = len(words)
26
+ if current_word_count + word_count > 100:
27
+ break
28
+ final_sentences.append(sentence)
29
+ current_word_count += word_count
30
+
31
+ # Join the final sentences to form the story
32
+ final_story = ' '.join(final_sentences)
33
+
34
+ # Ensure it ends with a punctuation mark
35
+ if not final_story.endswith(('.', '!', '?')):
36
+ final_story += '.'
37
+
38
+ return final_story
39
 
40
  def text2audio(story_text):
41
  audio_generator = pipeline("text-to-speech", model="ylacombe/vits_vctk_scottish_female")