Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,63 @@
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
-
import
|
5 |
|
6 |
# function part
|
7 |
# img2text
|
8 |
def img2text(url):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# Make the caption more fun and happy
|
15 |
-
fun_caption = f"π Wow! This picture shows {text.lower()}. Letβs turn it into a fun story! π"
|
16 |
-
return fun_caption
|
17 |
-
except Exception as e:
|
18 |
-
st.error(f"Oops! Something went wrong while looking at your picture. Please try again! π")
|
19 |
-
return None
|
20 |
|
21 |
# text2story
|
22 |
def text2story(text):
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
# Make the story more fun by adding a happy ending
|
32 |
-
happy_story = story + " And everyone had a big smile on their faces at the end of the day! ππ"
|
33 |
-
return happy_story
|
34 |
-
except Exception as e:
|
35 |
-
st.error(f"Oops! Something went wrong while creating your story. Please try again! π")
|
36 |
-
return None
|
37 |
|
38 |
# text2audio
|
39 |
def text2audio(story_text):
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
audio_output = tts_pipeline(story_text)
|
44 |
-
audio_file = "story_audio.wav"
|
45 |
-
# Save the audio file
|
46 |
-
with open(audio_file, "wb") as f:
|
47 |
-
f.write(audio_output["audio"])
|
48 |
-
return audio_file
|
49 |
-
except Exception as e:
|
50 |
-
st.error(f"Oops! Something went wrong while turning your story into audio. Please try again! π")
|
51 |
-
return None
|
52 |
|
53 |
# main part
|
54 |
-
st.set_page_config(page_title="Story Maker", page_icon="
|
55 |
-
st.header("
|
56 |
-
uploaded_file = st.file_uploader("
|
57 |
|
58 |
if uploaded_file is not None:
|
59 |
bytes_data = uploaded_file.getvalue()
|
60 |
with open(uploaded_file.name, "wb") as file:
|
61 |
file.write(bytes_data)
|
62 |
|
63 |
-
st.image(uploaded_file, caption="Your
|
64 |
|
65 |
# Stage 1: Image to Text
|
66 |
-
st.
|
67 |
scenario = img2text(uploaded_file.name)
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
# Clean up the audio file after playing
|
86 |
-
os.remove(audio_file)
|
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
+
import torch
|
5 |
|
6 |
# function part
|
7 |
# img2text
|
8 |
def img2text(url):
|
9 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
10 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
11 |
+
# Make the caption simple and fun for kids
|
12 |
+
fun_caption = f"Look what we found! π¨ {text}"
|
13 |
+
return fun_caption
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# text2story
|
16 |
def text2story(text):
|
17 |
+
story_generator = pipeline("text-generation", model="distilgpt2")
|
18 |
+
# Generate a story with a maximum of 90 words
|
19 |
+
story = story_generator(text, max_length=90, num_return_sequences=1)[0]["generated_text"]
|
20 |
+
# Ensure the story does not exceed 90 words
|
21 |
+
story = " ".join(story.split()[:90]) # Truncate to 90 words
|
22 |
+
# Make the story simple and fun for kids
|
23 |
+
fun_story = f"Once upon a time... π {story}"
|
24 |
+
return fun_story
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# text2audio
|
27 |
def text2audio(story_text):
|
28 |
+
tts_pipeline = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
|
29 |
+
audio_data = tts_pipeline(story_text)
|
30 |
+
return audio_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# main part
|
33 |
+
st.set_page_config(page_title="Story Maker", page_icon="π¦")
|
34 |
+
st.header("Story Maker: Turn Your Picture into a Story!")
|
35 |
+
uploaded_file = st.file_uploader("Select an Image...")
|
36 |
|
37 |
if uploaded_file is not None:
|
38 |
bytes_data = uploaded_file.getvalue()
|
39 |
with open(uploaded_file.name, "wb") as file:
|
40 |
file.write(bytes_data)
|
41 |
|
42 |
+
st.image(uploaded_file, caption="Your Picture", use_container_width=True)
|
43 |
|
44 |
# Stage 1: Image to Text
|
45 |
+
st.text('β¨ Discovering whatβs in your picture...')
|
46 |
scenario = img2text(uploaded_file.name)
|
47 |
+
st.write(f"Hereβs what we found: {scenario}")
|
48 |
+
|
49 |
+
# Stage 2: Text to Story
|
50 |
+
st.text('π Creating a fun story for you...')
|
51 |
+
story = text2story(scenario)
|
52 |
+
st.write(story)
|
53 |
+
|
54 |
+
# Stage 3: Story to Audio data
|
55 |
+
st.text('π Turning your story into audio...')
|
56 |
+
audio_data = text2audio(story)
|
57 |
+
|
58 |
+
# Play button
|
59 |
+
if st.button("Play Audio"):
|
60 |
+
st.audio(audio_data['audio'],
|
61 |
+
format="audio/wav",
|
62 |
+
start_time=0,
|
63 |
+
sample_rate=audio_data['sampling_rate'])
|
|
|
|