TLH01 commited on
Commit
6eda0b8
Β·
verified Β·
1 Parent(s): 0491fab

Create app1.py

Browse files
Files changed (1) hide show
  1. app1.py +52 -0
app1.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import tempfile
4
+ from transformers import pipeline
5
+
6
+ # --- Stage 1: Image β†’ Caption ---
7
+ def generate_caption(image):
8
+ caption_pipeline = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
9
+ caption = caption_pipeline(image)[0]['generated_text']
10
+ return caption
11
+
12
+ # --- Stage 2: Caption β†’ Story ---
13
+ def generate_story(caption):
14
+ story_pipeline = pipeline("text-generation", model="gpt2")
15
+ prompt = f"Write a fun, short story (50-100 words) for a child based on: {caption}"
16
+ story = story_pipeline(prompt, max_length=100, do_sample=True)[0]['generated_text']
17
+ return story
18
+
19
+ # --- Stage 3: Story β†’ Audio ---
20
+ def generate_audio(story_text):
21
+ tts_pipeline = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
22
+ speech = tts_pipeline(story_text)
23
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
24
+ f.write(speech["audio"])
25
+ return f.name
26
+
27
+ # --- Streamlit UI ---
28
+ def main():
29
+ st.title("πŸ“– AI Storyteller for Kids (3 Stages)")
30
+ st.write("Upload a child-friendly image and let the app create a story and read it out loud!")
31
+
32
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
33
+
34
+ if uploaded_image:
35
+ image = Image.open(uploaded_image)
36
+ st.image(image, caption="Your uploaded image", use_column_width=True)
37
+
38
+ with st.spinner("πŸ” Generating caption..."):
39
+ caption = generate_caption(image)
40
+ st.success(f"πŸ–ΌοΈ Caption: {caption}")
41
+
42
+ with st.spinner("πŸ“ Generating story..."):
43
+ story = generate_story(caption)
44
+ st.markdown("### πŸ“š Generated Story:")
45
+ st.write(story)
46
+
47
+ with st.spinner("πŸ”Š Generating audio..."):
48
+ audio_path = generate_audio(story)
49
+ st.audio(audio_path, format="audio/wav")
50
+
51
+ if __name__ == "__main__":
52
+ main()