mayf commited on
Commit
8367fb2
·
verified ·
1 Parent(s): 596328e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from transformers import pipeline
4
+ from gtts import gTTS
5
+ import tempfile
6
+
7
+ # —––––––– Page config
8
+ st.set_page_config(page_title="Storyteller for Kids", layout="centered")
9
+ st.title("🖼️ ➡️ 📖 Interactive Storyteller")
10
+
11
+ # —––––––– Cache model loading
12
+ @st.cache_resource
13
+ def load_pipelines():
14
+ # 1) Image captioning
15
+ captioner = pipeline(
16
+ "image-captioning",
17
+ model="Salesforce/blip-image-captioning-base"
18
+ )
19
+ # 2) Story generation with Flan-T5
20
+ storyteller = pipeline(
21
+ "text2text-generation",
22
+ model="google/flan-t5-base"
23
+ )
24
+ return captioner, storyteller
25
+
26
+ captioner, storyteller = load_pipelines()
27
+
28
+ # —––––––– Image upload
29
+ uploaded = st.file_uploader("Upload an image:", type=["jpg", "jpeg", "png"])
30
+ if uploaded:
31
+ image = Image.open(uploaded).convert("RGB")
32
+ st.image(image, caption="Your image", use_column_width=True)
33
+
34
+ # —––––––– 1. Caption
35
+ with st.spinner("🔍 Looking at the image..."):
36
+ cap = captioner(image)[0]["generated_text"]
37
+ st.markdown(f"**Caption:** {cap}")
38
+
39
+ # —––––––– 2. Story generation
40
+ prompt = (
41
+ "Write a playful, 50–100 word story for 3–10 year-old children "
42
+ f"based on this description:\n\n“{cap}”\n\nStory:"
43
+ )
44
+ with st.spinner("✍️ Writing a story..."):
45
+ out = storyteller(
46
+ prompt,
47
+ max_length=200,
48
+ do_sample=True,
49
+ top_p=0.9,
50
+ temperature=0.8,
51
+ num_return_sequences=1
52
+ )
53
+ story = out[0]["generated_text"].strip()
54
+ st.markdown("**Story:**")
55
+ st.write(story)
56
+
57
+ # —––––––– 3. Text-to-Speech
58
+ with st.spinner("🔊 Converting to speech..."):
59
+ tts = gTTS(story, lang="en")
60
+ tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
61
+ tts.write_to_fp(tmp)
62
+ tmp.flush()
63
+ st.audio(tmp.name, format="audio/mp3")