Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,120 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import streamlit as st
|
4 |
from PIL import Image
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
7 |
import tempfile
|
|
|
8 |
|
9 |
-
# —––––––– Page config
|
10 |
-
st.set_page_config(
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
# —––––––– Cache model loading
|
14 |
@st.cache_resource
|
15 |
def load_pipelines():
|
16 |
-
#
|
17 |
captioner = pipeline(
|
18 |
"image-to-text",
|
19 |
-
model="Salesforce/blip-image-captioning-base"
|
|
|
20 |
)
|
21 |
-
|
|
|
22 |
storyteller = pipeline(
|
23 |
"text2text-generation",
|
24 |
-
model="google/flan-t5-
|
25 |
-
|
|
|
26 |
)
|
|
|
27 |
return captioner, storyteller
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
# —–––––––
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
tts = gTTS(story, lang="en")
|
64 |
-
tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
65 |
-
tts.write_to_fp(tmp)
|
66 |
-
tmp.flush()
|
67 |
-
st.audio(tmp.name, format="audio/mp3")
|
68 |
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
from transformers import pipeline
|
4 |
from gtts import gTTS
|
5 |
import tempfile
|
6 |
+
import os
|
7 |
|
8 |
+
# —––––––– Page config —–––––––
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="Storyteller for Kids",
|
11 |
+
page_icon="📚",
|
12 |
+
layout="centered",
|
13 |
+
initial_sidebar_state="collapsed"
|
14 |
+
)
|
15 |
+
st.title("🖼️➡️📖 Interactive Storyteller")
|
16 |
|
17 |
+
# —––––––– Cache model loading —–––––––
|
18 |
@st.cache_resource
|
19 |
def load_pipelines():
|
20 |
+
# Image-to-text pipeline
|
21 |
captioner = pipeline(
|
22 |
"image-to-text",
|
23 |
+
model="Salesforce/blip-image-captioning-base",
|
24 |
+
max_new_tokens=50
|
25 |
)
|
26 |
+
|
27 |
+
# Story generation pipeline with better parameters
|
28 |
storyteller = pipeline(
|
29 |
"text2text-generation",
|
30 |
+
model="google/flan-t5-xxl",
|
31 |
+
device_map="auto",
|
32 |
+
model_kwargs={"load_in_8bit": True}
|
33 |
)
|
34 |
+
|
35 |
return captioner, storyteller
|
36 |
|
37 |
+
# —––––––– Main workflow —–––––––
|
38 |
+
def main():
|
39 |
+
captioner, storyteller = load_pipelines()
|
40 |
+
|
41 |
+
# —––––––– Image upload —–––––––
|
42 |
+
uploaded = st.file_uploader(
|
43 |
+
"Upload an image:",
|
44 |
+
type=["jpg", "jpeg", "png"],
|
45 |
+
help="Max size: 5MB"
|
46 |
+
)
|
47 |
+
|
48 |
+
if uploaded:
|
49 |
+
try:
|
50 |
+
# —––––––– Display image —–––––––
|
51 |
+
image = Image.open(uploaded).convert("RGB")
|
52 |
+
st.image(image, caption="Your Image", use_column_width=True)
|
53 |
|
54 |
+
# —––––––– Generate caption —–––––––
|
55 |
+
with st.spinner("🔍 Analyzing image content..."):
|
56 |
+
cap_outputs = captioner(image)
|
57 |
+
cap = cap_outputs[0].get("generated_text", "").strip()
|
58 |
+
|
59 |
+
st.subheader("Image Understanding")
|
60 |
+
st.info(f"**Detected:** {cap}")
|
61 |
|
62 |
+
# —––––––– Generate story —–––––––
|
63 |
+
st.subheader("Story Creation")
|
64 |
+
prompt = f"""Create a children's story (3-10 years old) based on this description:
|
65 |
+
|
66 |
+
{cap}
|
67 |
+
|
68 |
+
Requirements:
|
69 |
+
- 50-100 words
|
70 |
+
- Playful and imaginative
|
71 |
+
- Positive message
|
72 |
+
- Simple vocabulary
|
73 |
+
- Include animal characters
|
74 |
+
|
75 |
+
Story:"""
|
76 |
+
|
77 |
+
with st.spinner("✍️ Crafting a magical story..."):
|
78 |
+
story_output = storyteller(
|
79 |
+
prompt,
|
80 |
+
max_length=300,
|
81 |
+
do_sample=True,
|
82 |
+
top_p=0.95,
|
83 |
+
temperature=0.85,
|
84 |
+
num_beams=4,
|
85 |
+
repetition_penalty=1.2
|
86 |
+
)
|
87 |
+
story = story_output[0]["generated_text"].strip()
|
88 |
+
|
89 |
+
st.success("**Generated Story:**")
|
90 |
+
st.write(story)
|
91 |
|
92 |
+
# —––––––– Text-to-Speech —–––––––
|
93 |
+
st.subheader("Audio Version")
|
94 |
+
with st.spinner("🔊 Generating audio..."):
|
95 |
+
try:
|
96 |
+
tts = gTTS(text=story, lang="en", slow=False)
|
97 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp:
|
98 |
+
tts.write_to_fp(tmp)
|
99 |
+
tmp_path = tmp.name
|
100 |
+
|
101 |
+
st.audio(tmp_path, format="audio/mp3")
|
102 |
+
|
103 |
+
# Add download button
|
104 |
+
with open(tmp_path, "rb") as f:
|
105 |
+
st.download_button(
|
106 |
+
label="Download Audio Story",
|
107 |
+
data=f,
|
108 |
+
file_name="kids_story.mp3",
|
109 |
+
mime="audio/mpeg"
|
110 |
+
)
|
111 |
+
|
112 |
+
finally:
|
113 |
+
if os.path.exists(tmp_path):
|
114 |
+
os.remove(tmp_path)
|
115 |
|
116 |
+
except Exception as e:
|
117 |
+
st.error(f"Error processing your request: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
if __name__ == "__main__":
|
120 |
+
main()
|