Fanny1366 commited on
Commit
3fceda9
·
verified ·
1 Parent(s): 7b4668d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -2,7 +2,7 @@
2
  import streamlit as st
3
  from transformers import pipeline
4
  from gtts import gTTS
5
- import os
6
 
7
  # function part
8
  # img2text
@@ -14,15 +14,19 @@ def img2text(url):
14
 
15
  # text2story
16
  def text2story(text):
17
- story_text = pipeline("text-generation", model="perplexity-ai/r1-1776", trust_remote_code=True) # to be completed
 
18
  return story_text
19
 
20
  # text2audio
21
  def text2audio(story_text):
22
- # Convert text to audio using gTTS
23
- tts = gTTS(story_text, lang="en")
24
- audio_file = "story_audio.wav"
25
- tts.save(audio_file)
 
 
 
26
  return audio_file
27
 
28
  # main part
@@ -55,12 +59,10 @@ if uploaded_file is not None:
55
 
56
  #Stage 3: Story to Audio data
57
  st.text('Generating audio data...')
58
- audio_data =text2audio(story)
59
 
60
  # Play button
61
  if st.button("Play Audio"):
62
- st.audio(audio_data['audio'],
63
  format="audio/wav",
64
- start_time=0,
65
- sample_rate = audio_data['sampling_rate'])
66
- st.audio("kids_playing_audio.wav")
 
2
  import streamlit as st
3
  from transformers import pipeline
4
  from gtts import gTTS
5
+ import io
6
 
7
  # function part
8
  # img2text
 
14
 
15
  # text2story
16
  def text2story(text):
17
+ story_pipeline = pipeline("text-generation", model="perplexity-ai/r1-1776", trust_remote_code=True)
18
+ story_text = story_pipeline(text, max_length=200)[0]['generated_text']
19
  return story_text
20
 
21
  # text2audio
22
  def text2audio(story_text):
23
+ # 使用 gTTS 将文本转换为音频
24
+ tts = gTTS(text=story_text, lang='en')
25
+ # 创建一个内存中的字节流
26
+ audio_file = io.BytesIO()
27
+ # 将音频保存到字节流中
28
+ tts.write_to_fp(audio_file)
29
+ audio_file.seek(0)
30
  return audio_file
31
 
32
  # main part
 
59
 
60
  #Stage 3: Story to Audio data
61
  st.text('Generating audio data...')
62
+ audio_data = text2audio(story)
63
 
64
  # Play button
65
  if st.button("Play Audio"):
66
+ st.audio(audio_data,
67
  format="audio/wav",
68
+ start_time=0)