Leo Liu commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
# import part
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# function part
|
| 6 |
# img2text
|
|
@@ -9,6 +12,7 @@ def img2text(url):
|
|
| 9 |
text = image_to_text_model(url)[0]["generated_text"]
|
| 10 |
return text
|
| 11 |
|
|
|
|
| 12 |
# text2story
|
| 13 |
def text2story(text):
|
| 14 |
pipe = pipeline(
|
|
@@ -32,12 +36,13 @@ def text2story(text):
|
|
| 32 |
target_length = min(max(len(story.split()), 50), 150)
|
| 33 |
return " ".join(story.split()[:target_length])
|
| 34 |
|
| 35 |
-
|
| 36 |
# text2audio
|
| 37 |
def text2audio(story_text):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def main():
|
|
@@ -105,9 +110,7 @@ def main():
|
|
| 105 |
|
| 106 |
# Auto-play the audio
|
| 107 |
st.audio(audio_data['audio'],
|
| 108 |
-
format="audio/
|
| 109 |
-
start_time=0,
|
| 110 |
-
sample_rate=audio_data['sampling_rate'],
|
| 111 |
autoplay=True)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# import part
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
|
| 8 |
# function part
|
| 9 |
# img2text
|
|
|
|
| 12 |
text = image_to_text_model(url)[0]["generated_text"]
|
| 13 |
return text
|
| 14 |
|
| 15 |
+
|
| 16 |
# text2story
|
| 17 |
def text2story(text):
|
| 18 |
pipe = pipeline(
|
|
|
|
| 36 |
target_length = min(max(len(story.split()), 50), 150)
|
| 37 |
return " ".join(story.split()[:target_length])
|
| 38 |
|
|
|
|
| 39 |
# text2audio
|
| 40 |
def text2audio(story_text):
|
| 41 |
+
tts = gTTS(text=story_text, lang='en')
|
| 42 |
+
audio_bytes = io.BytesIO()
|
| 43 |
+
tts.write_to_fp(audio_bytes)
|
| 44 |
+
audio_bytes.seek(0)
|
| 45 |
+
return audio_bytes
|
| 46 |
|
| 47 |
|
| 48 |
def main():
|
|
|
|
| 110 |
|
| 111 |
# Auto-play the audio
|
| 112 |
st.audio(audio_data['audio'],
|
| 113 |
+
format="audio/mp3",
|
|
|
|
|
|
|
| 114 |
autoplay=True)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|