TLH01 commited on
Commit
0cecc1f
Β·
verified Β·
1 Parent(s): 833f88a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -49
app.py CHANGED
@@ -1,77 +1,73 @@
1
- # --- Import part ---
 
2
  import streamlit as st
3
  from transformers import pipeline
4
  from PIL import Image
 
 
5
 
6
- # --- Stage 1: Image to Caption ---
7
  @st.cache_resource
8
- def load_image_caption_model():
9
  return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
10
 
11
  def generate_image_caption(image):
12
- img2caption = load_image_caption_model()
13
- result = img2caption(image)
14
  return result[0]['generated_text']
15
 
16
- # --- Stage 2: Caption to Story ---
17
  @st.cache_resource
18
- def load_story_generator_model():
19
- return pipeline(
20
- "text-generation",
21
- model="roneneldan/TinyStories-Instruct-3M",
22
- max_length=200,
23
- do_sample=True,
24
- temperature=0.8,
25
- )
26
 
27
- def caption_to_story(caption_text):
28
- story_prompt = f"Write a short story for children aged 3 to 10 based on this description: {caption_text}. The story should be creative, friendly, and use simple words."
29
- generator = load_story_generator_model()
30
- result = generator(story_prompt)
31
- return result[0]['generated_text']
32
 
33
- # --- Stage 3: Story to Speech ---
34
  @st.cache_resource
35
- def load_text_to_speech_model():
36
- return pipeline("text-to-speech", model="suno/bark-small")
37
 
38
- def story_to_audio(story):
39
- tts = load_text_to_speech_model()
40
- result = tts(story)
41
- return result["audio"]
42
 
43
- # --- Main App ---
44
  def main():
45
  st.set_page_config(page_title="Kids Story Maker πŸ§’πŸ“–", layout="centered")
46
 
47
- st.markdown("<h1 style='color:#FF69B4;'>🎨 Kids Story Maker</h1>", unsafe_allow_html=True)
48
- st.markdown("Upload a picture, and we'll turn it into a magical story and voice! 🐻✨")
49
 
50
- uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
51
 
52
- if uploaded_image is not None:
53
- image = Image.open(uploaded_image).convert("RGB")
54
- st.image(image, caption="Uploaded Image", use_column_width=True)
55
 
56
- # Stage 1: image β†’ caption
57
- st.subheader("πŸ” Stage 1: Describe the image")
58
- with st.spinner("Generating caption..."):
59
- caption = generate_image_caption(image)
60
- st.success(f"Description: {caption}")
61
 
62
- # Stage 2: caption β†’ story
63
- st.subheader("πŸ“– Stage 2: Create a story")
64
- with st.spinner("Generating story..."):
65
- story = caption_to_story(caption)
66
- st.success("Here's your story:")
67
  st.write(story)
68
 
69
- # Stage 3: story β†’ audio
70
- st.subheader("πŸ”Š Stage 3: Listen to your story")
71
- with st.spinner("Converting story to voice..."):
72
- audio = story_to_audio(story)
73
- st.audio(audio, format="audio/wav")
74
 
75
- # Run app
76
  if __name__ == "__main__":
77
  main()
 
1
+ # app.py
2
+
3
  import streamlit as st
4
  from transformers import pipeline
5
  from PIL import Image
6
+ import numpy as np
7
+ import io
8
 
9
+ # Stage 1 - Image to Caption
10
  @st.cache_resource
11
+ def load_image_captioner():
12
  return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
13
 
14
  def generate_image_caption(image):
15
+ captioner = load_image_captioner()
16
+ result = captioner(image)
17
  return result[0]['generated_text']
18
 
19
+ # Stage 2 - Description to Story
20
  @st.cache_resource
21
+ def load_story_generator():
22
+ return pipeline("text2text-generation", model="nateraw/english-text-to-story")
 
 
 
 
 
 
23
 
24
+ def text2story(description):
25
+ story_gen = load_story_generator()
26
+ prompt = f"{description}\n\nStory:"
27
+ result = story_gen(prompt, max_length=200)[0]['generated_text']
28
+ return result.strip()
29
 
30
+ # Stage 3 - Story to Audio
31
  @st.cache_resource
32
+ def load_tts():
33
+ return pipeline("text-to-audio", model="suno/bark-small")
34
 
35
+ def story_to_audio(story_text):
36
+ tts = load_tts()
37
+ result = tts(story_text)
38
+ return result["audio"], result["sampling_rate"]
39
 
40
+ # Main App
41
  def main():
42
  st.set_page_config(page_title="Kids Story Maker πŸ§’πŸ“–", layout="centered")
43
 
44
+ st.title("🎨 Kids Story Maker")
45
+ st.markdown("Upload a picture, and we will turn it into a story and voice!")
46
 
47
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
48
 
49
+ if uploaded_file:
50
+ image = Image.open(uploaded_file).convert("RGB")
51
+ st.image(image, caption="Your Uploaded Image", use_column_width=True)
52
 
53
+ # Stage 1: Image β†’ Description
54
+ st.subheader("Step 1: Generating Image Caption...")
55
+ with st.spinner("Generating caption from image..."):
56
+ description = generate_image_caption(image)
57
+ st.success(f"Caption: {description}")
58
 
59
+ # Stage 2: Description β†’ Story
60
+ st.subheader("Step 2: Generating Story from Caption...")
61
+ with st.spinner("Generating children's story..."):
62
+ story = text2story(description)
63
+ st.success("Generated Story:")
64
  st.write(story)
65
 
66
+ # Stage 3: Story β†’ Audio
67
+ st.subheader("Step 3: Converting Story to Voice...")
68
+ with st.spinner("Converting to audio..."):
69
+ audio, sample_rate = story_to_audio(story)
70
+ st.audio(audio, sample_rate=sample_rate, format="audio/wav")
71
 
 
72
  if __name__ == "__main__":
73
  main()