Leo Liu commited on
Commit
6a488e2
·
verified ·
1 Parent(s): 9a8f95f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -54
app.py CHANGED
@@ -3,6 +3,7 @@ import streamlit as st
3
  from transformers import pipeline
4
  from gtts import gTTS
5
  import io
 
6
 
7
 
8
  # function part
@@ -15,61 +16,15 @@ def img2text(url):
15
 
16
  # text2story
17
  def text2story(text):
18
- # 儿童故事提示模板
19
- child_prompt = f"""Write a children's story for ages 3-10 based on: {text}
20
- Requirements:
21
- - Use simple words (1st-3rd grade level)
22
- - Main character: friendly animal
23
- - Story elements: magic, friendship, happy ending
24
- - Moral lesson: sharing is caring
25
- - Length: 100-120 words
26
-
27
- Story structure:
28
- 1. Introduce characters and setting
29
- 2. A problem occurs
30
- 3. Magical solution with teamwork
31
- 4. Happy ending with a lesson
32
- """
33
-
34
- pipe = pipeline(
35
- "text-generation",
36
- model="pranavpsv/genre-story-generator-v2",
37
- max_new_tokens=160, # 160 tokens ≈ 120 words
38
- min_new_tokens=134, # 134 tokens ≈ 100 words
39
- temperature=0.7, # 平衡创造性与稳定性
40
- top_p=0.9,
41
- repetition_penalty=1.2,
42
- pad_token_id=50256, # 对齐模型config的bos/eos_token_id
43
- num_return_sequences=1
44
- )
45
-
46
- # 生成故事
47
- raw_story = pipe(child_prompt, return_full_text=False)[0]['generated_text']
48
-
49
- # 智能后处理
50
- def format_story(text):
51
- # 移除技术性标记
52
- text = text.replace("<|endoftext|>", "").strip()
53
- # 寻找自然结尾点
54
- end_marks = ['.', '!', '?', '...']
55
- last_positions = [text.rfind(mark) for mark in end_marks]
56
- valid_positions = [pos for pos in last_positions if pos != -1]
57
- cutoff = max(valid_positions) + 1 if valid_positions else len(text)
58
- return text[:cutoff]
59
-
60
- formatted_story = format_story(raw_story)
61
-
62
- # 字数验证
63
- word_count = len(formatted_story.split())
64
- if word_count < 100:
65
- formatted_story += " And they lived happily ever after."
66
-
67
- return formatted_story
68
 
69
 
70
  # text2audio
71
  def text2audio(story_text):
72
- tts = gTTS(text=story_text, lang='en')
73
  audio_bytes = io.BytesIO()
74
  tts.write_to_fp(audio_bytes)
75
  audio_bytes.seek(0)
@@ -120,20 +75,20 @@ def main():
120
  progress_bar = st.progress(0)
121
 
122
  # Stage 1: Image to Text
123
- with status_container.status("🔮 **Step 1/3**: Decoding picture magic...", expanded=True) as status:
124
  progress_bar.progress(33)
125
  scenario = img2text(uploaded_file.name)
126
  status.update(label="✅ Picture decoded!", state="complete")
127
  st.write(f"**What I see:** {scenario}")
128
 
129
- # Stage 2: Text to Story
130
  with status_container.status("📚 **Step 2/3**: Writing your fairy tale...", expanded=True) as status:
131
  progress_bar.progress(66)
132
  story = text2story(scenario)
133
  status.update(label="✅ Story created!", state="complete")
134
  st.write(f"**Your Story:**\n{story}")
135
 
136
- # Stage 3: Story to Audio data
137
  with status_container.status("🎵 **Step 3/3**: Adding magic audio...", expanded=True) as status:
138
  progress_bar.progress(100)
139
  audio_data = text2audio(story)
 
3
  from transformers import pipeline
4
  from gtts import gTTS
5
  import io
6
+ import re
7
 
8
 
9
  # function part
 
16
 
17
  # text2story
18
  def text2story(text):
19
+ prompt = f"Generate a children's story for 3-10 ages based on {text}"
20
+ pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2", max_new_tokens=160, min_new_tokens=130, num_return_sequences=1)
21
+ story_text = pipe(prompt)[0]['generated_text']
22
+ return story_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
 
25
  # text2audio
26
  def text2audio(story_text):
27
+ tts = gTTS(text = story_text, lang='en')
28
  audio_bytes = io.BytesIO()
29
  tts.write_to_fp(audio_bytes)
30
  audio_bytes.seek(0)
 
75
  progress_bar = st.progress(0)
76
 
77
  # Stage 1: Image to Text
78
+ with status_container.status("🔮 **Step 1/3**: Decoding picture magic...", expanded=True) as status: # Add progress bar components to improve experience
79
  progress_bar.progress(33)
80
  scenario = img2text(uploaded_file.name)
81
  status.update(label="✅ Picture decoded!", state="complete")
82
  st.write(f"**What I see:** {scenario}")
83
 
84
+ #Stage 2: Text to Story
85
  with status_container.status("📚 **Step 2/3**: Writing your fairy tale...", expanded=True) as status:
86
  progress_bar.progress(66)
87
  story = text2story(scenario)
88
  status.update(label="✅ Story created!", state="complete")
89
  st.write(f"**Your Story:**\n{story}")
90
 
91
+ #Stage 3: Story to Audio data
92
  with status_container.status("🎵 **Step 3/3**: Adding magic audio...", expanded=True) as status:
93
  progress_bar.progress(100)
94
  audio_data = text2audio(story)