Spaces:
Build error
Build error
Leo Liu commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# import part
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import streamlit as st
|
|
|
|
| 4 |
|
| 5 |
# function part
|
| 6 |
# img2text
|
|
@@ -11,66 +11,51 @@ def img2text(url):
|
|
| 11 |
|
| 12 |
# text2story
|
| 13 |
def text2story(text):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
f"Based on the image description below:{text}\n"
|
| 17 |
-
f"Please create a funny, heartwarming, imaginative fairy tale of no less than 50 words and less than 100 words."
|
| 18 |
-
f"Output only the body of the story, do not include other content."
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
story_text = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B",
|
| 22 |
-
max_length=100,
|
| 23 |
-
do_sample=True,
|
| 24 |
-
top_p=0.95,
|
| 25 |
-
temperature=0.7)
|
| 26 |
-
generation = story_text(story_prompt)
|
| 27 |
-
raw_output = generation[0]['generated_text']
|
| 28 |
-
|
| 29 |
-
if raw_output.startswith(story_prompt):
|
| 30 |
-
story_text = raw_output[len(story_prompt):].strip()
|
| 31 |
-
else:
|
| 32 |
-
story_text = raw_output
|
| 33 |
return story_text
|
| 34 |
|
| 35 |
# text2audio
|
| 36 |
def text2audio(story_text):
|
| 37 |
-
|
|
|
|
| 38 |
return audio_data
|
| 39 |
|
| 40 |
-
# main part
|
| 41 |
-
st.set_page_config(page_title="Your Image to Audio Story",
|
| 42 |
-
page_icon="🦜")
|
| 43 |
-
st.header("Turn Your Image to Audio Story")
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
file.write(bytes_data)
|
| 53 |
-
st.image(uploaded_file, caption="Uploaded Image",
|
| 54 |
-
use_container_width=True)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
st.write(scenario)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
#Stage 3: Story to Audio data
|
| 67 |
-
st.text('Generating audio data...')
|
| 68 |
-
audio_data =text2audio(story)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
st.audio(audio_data['audio'],
|
| 73 |
-
format="audio/wav",
|
| 74 |
-
start_time=0,
|
| 75 |
-
sample_rate = audio_data['sampling_rate'])
|
| 76 |
-
st.audio("kids_playing_audio.wav")
|
|
|
|
| 1 |
# import part
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
# function part
|
| 6 |
# img2text
|
|
|
|
| 11 |
|
| 12 |
# text2story
|
| 13 |
def text2story(text):
|
| 14 |
+
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 15 |
+
story_text = pipe(text)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return story_text
|
| 17 |
|
| 18 |
# text2audio
|
| 19 |
def text2audio(story_text):
|
| 20 |
+
pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
|
| 21 |
+
audio_data = pipe(story_text)
|
| 22 |
return audio_data
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
def main():
|
| 26 |
+
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
| 27 |
+
st.header("Turn Your Image to Audio Story")
|
| 28 |
+
uploaded_file = st.file_uploader("Select an Image...")
|
| 29 |
+
|
| 30 |
+
if uploaded_file is not None:
|
| 31 |
+
print(uploaded_file)
|
| 32 |
+
bytes_data = uploaded_file.getvalue()
|
| 33 |
+
with open(uploaded_file.name, "wb") as file:
|
| 34 |
+
file.write(bytes_data)
|
| 35 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
#Stage 1: Image to Text
|
| 39 |
+
st.text('Processing img2text...')
|
| 40 |
+
scenario = img2text(uploaded_file.name)
|
| 41 |
+
st.write(scenario)
|
| 42 |
|
| 43 |
+
#Stage 2: Text to Story
|
| 44 |
+
st.text('Generating a story...')
|
| 45 |
+
story = text2story(scenario)
|
| 46 |
+
st.write(story)
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
#Stage 3: Story to Audio data
|
| 49 |
+
st.text('Generating audio data...')
|
| 50 |
+
audio_data =text2audio(story)
|
|
|
|
| 51 |
|
| 52 |
+
# Play button
|
| 53 |
+
if st.button("Play Audio"):
|
| 54 |
+
st.audio(audio_data['audio'],
|
| 55 |
+
format="audio/wav",
|
| 56 |
+
start_time=0,
|
| 57 |
+
sample_rate = audio_data['sampling_rate'])
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|