Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,61 @@
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
-
from gtts import gTTS
|
5 |
-
import os
|
6 |
|
7 |
# function part
|
8 |
# img2text
|
9 |
def img2text(url):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
return fun_caption
|
16 |
-
except Exception as e:
|
17 |
-
st.error(f"Oops! Something went wrong while looking at your picture. Please try again! 🪄")
|
18 |
-
return None
|
19 |
|
20 |
# text2story
|
21 |
def text2story(text):
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
except Exception as e:
|
31 |
-
st.error(f"Oops! Something went wrong while creating your story. Please try again! 🪄")
|
32 |
-
return None
|
33 |
|
34 |
# text2audio
|
35 |
def text2audio(story_text):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
return audio_file
|
41 |
-
except Exception as e:
|
42 |
-
st.error(f"Oops! Something went wrong while turning your story into audio. Please try again! 🪄")
|
43 |
-
return None
|
44 |
|
45 |
# main part
|
46 |
-
st.set_page_config(page_title="Story
|
47 |
-
st.header("
|
48 |
-
|
|
|
49 |
|
50 |
if uploaded_file is not None:
|
51 |
bytes_data = uploaded_file.getvalue()
|
52 |
with open(uploaded_file.name, "wb") as file:
|
53 |
file.write(bytes_data)
|
54 |
|
55 |
-
st.image(uploaded_file, caption="Your
|
56 |
|
57 |
# Stage 1: Image to Text
|
58 |
-
st.
|
59 |
scenario = img2text(uploaded_file.name)
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
if story:
|
67 |
-
st.write("📖 Here’s your story:")
|
68 |
-
st.write(story)
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
if audio_file:
|
74 |
-
# Play button
|
75 |
-
if st.button("🎧 Listen to Your Story!"):
|
76 |
-
st.audio(audio_file, format="audio/mp3")
|
77 |
-
# Clean up the audio file after playing
|
78 |
-
os.remove(audio_file)
|
|
|
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")
|
9 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
10 |
+
# Make the caption more fun and descriptive
|
11 |
+
fun_caption = f"Wow! {text.capitalize()}! 🌟"
|
12 |
+
return fun_caption
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# text2story
|
15 |
def text2story(text):
|
16 |
+
# Use a text-generation model to create a fun and realistic story
|
17 |
+
story_generator = pipeline("text-generation", model="gpt2")
|
18 |
+
# Add a prompt to guide the story generation (avoid magical elements)
|
19 |
+
prompt = f"Write a fun and realistic story for kids based on this: {text}. Keep it simple and under 95 words."
|
20 |
+
story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
|
21 |
+
# Ensure the story is simple and fun for kids
|
22 |
+
simple_story = story[:95] # Limit to 95 words
|
23 |
+
return simple_story
|
|
|
|
|
|
|
24 |
|
25 |
# text2audio
|
26 |
def text2audio(story_text):
|
27 |
+
# Use a TTS model to convert text to audio
|
28 |
+
tts = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
|
29 |
+
audio_data = tts(story_text)
|
30 |
+
return audio_data
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# main part
|
33 |
+
st.set_page_config(page_title="Story Explorer", page_icon="🦜")
|
34 |
+
st.header("Story Explorer: Turn Your Picture into a Fun Story! 🎨📖")
|
35 |
+
|
36 |
+
uploaded_file = st.file_uploader("Choose a picture...", type=["jpg", "png", "jpeg"])
|
37 |
|
38 |
if uploaded_file is not None:
|
39 |
bytes_data = uploaded_file.getvalue()
|
40 |
with open(uploaded_file.name, "wb") as file:
|
41 |
file.write(bytes_data)
|
42 |
|
43 |
+
st.image(uploaded_file, caption="Your Picture", use_container_width=True)
|
44 |
|
45 |
# Stage 1: Image to Text
|
46 |
+
st.text('Let’s explore your picture! 🧐✨')
|
47 |
scenario = img2text(uploaded_file.name)
|
48 |
+
st.write(f"**Here’s what I see in your picture:** {scenario}")
|
49 |
+
|
50 |
+
# Stage 2: Text to Story
|
51 |
+
st.text('Creating a fun story for you! 📖')
|
52 |
+
story = text2story(scenario)
|
53 |
+
st.write(f"**Here’s your story:** {story}")
|
54 |
|
55 |
+
# Stage 3: Story to Audio data
|
56 |
+
st.text('Turning your story into audio... 🎧')
|
57 |
+
audio_data = text2audio(story)
|
|
|
|
|
|
|
58 |
|
59 |
+
# Play button
|
60 |
+
if st.button("Play Audio"):
|
61 |
+
st.audio(audio_data['audio'], format="audio/wav", start_time=0, sample_rate=audio_data['sampling_rate'])
|
|
|
|
|
|
|
|
|
|
|
|