Leo Liu commited on
Commit
59e88fb
·
verified ·
1 Parent(s): 5756cb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -7
app.py CHANGED
@@ -1,8 +1,20 @@
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
 
4
 
5
  # function part
 
 
 
 
 
 
 
 
 
 
 
6
  # img2text
7
  def img2text(url):
8
  image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
@@ -109,14 +121,29 @@ def main():
109
  with status_container.status("🎵 **Step 3/3**: Adding magic audio...", expanded=True) as status:
110
  progress_bar.progress(100)
111
  audio_data = text2audio(story)
 
 
 
 
 
112
  status.update(label="✅ Start playing the story!", state="complete")
113
-
114
- # Auto-play the audio
115
- st.audio(audio_data['audio'],
116
- format="audio/wav",
117
- start_time=0,
118
- sample_rate=audio_data['sampling_rate'],
119
- autoplay=True)
 
 
 
 
 
 
 
 
 
 
120
 
121
  if __name__ == "__main__":
122
  main()
 
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
+ import math
5
 
6
  # function part
7
+ # 时间戳
8
+ def split_story_with_delay(story_text, sampling_rate=16000):
9
+ """将故事分割为带时间戳的段落"""
10
+ words = story_text.split()
11
+ chunk_size = max(1, len(words)//5) # 按词数均分5段
12
+ chunks = [' '.join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
13
+ # 假设每段持续2秒(根据音频长度动态调整更佳)
14
+ duration = len(audio_data["audio"]) / sampling_rate
15
+ chunk_duration = duration / len(chunks)
16
+ return list(zip(chunks, [chunk_duration]*len(chunks)))
17
+
18
  # img2text
19
  def img2text(url):
20
  image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
 
121
  with status_container.status("🎵 **Step 3/3**: Adding magic audio...", expanded=True) as status:
122
  progress_bar.progress(100)
123
  audio_data = text2audio(story)
124
+
125
+ # 新增字幕处理
126
+ subtitle_chunks = split_story_with_delay(story, audio_data['sampling_rate'])
127
+ current_subtitle = st.empty()
128
+
129
  status.update(label="✅ Start playing the story!", state="complete")
130
+
131
+ # 播放音频并更新字幕
132
+ with st.audio(...): # 保持原有参数
133
+ for text, duration in subtitle_chunks:
134
+ current_subtitle.markdown(f"""
135
+ <div style="
136
+ background: rgba(255,255,255,0.9);
137
+ padding: 1rem;
138
+ border-radius: 10px;
139
+ margin: 1rem 0;
140
+ font-size: 1.2rem;
141
+ color: #FF6B6B;
142
+ text-align: center;
143
+ font-family: 'Comic Neue', cursive;
144
+ ">{text}</div>
145
+ """, unsafe_allow_html=True)
146
+ time.sleep(duration) # 需import time
147
 
148
  if __name__ == "__main__":
149
  main()