Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,73 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
@st.cache_resource
|
| 8 |
-
def
|
| 9 |
return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 10 |
|
| 11 |
def generate_image_caption(image):
|
| 12 |
-
|
| 13 |
-
result =
|
| 14 |
return result[0]['generated_text']
|
| 15 |
|
| 16 |
-
#
|
| 17 |
@st.cache_resource
|
| 18 |
-
def
|
| 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
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
result =
|
| 31 |
-
return result
|
| 32 |
|
| 33 |
-
#
|
| 34 |
@st.cache_resource
|
| 35 |
-
def
|
| 36 |
-
return pipeline("text-to-
|
| 37 |
|
| 38 |
-
def story_to_audio(
|
| 39 |
-
tts =
|
| 40 |
-
result = tts(
|
| 41 |
-
return result["audio"]
|
| 42 |
|
| 43 |
-
#
|
| 44 |
def main():
|
| 45 |
st.set_page_config(page_title="Kids Story Maker π§π", layout="centered")
|
| 46 |
|
| 47 |
-
st.
|
| 48 |
-
st.markdown("Upload a picture, and we
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
if
|
| 53 |
-
image = Image.open(
|
| 54 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 55 |
|
| 56 |
-
# Stage 1:
|
| 57 |
-
st.subheader("
|
| 58 |
-
with st.spinner("Generating caption..."):
|
| 59 |
-
|
| 60 |
-
st.success(f"
|
| 61 |
|
| 62 |
-
# Stage 2:
|
| 63 |
-
st.subheader("
|
| 64 |
-
with st.spinner("Generating story..."):
|
| 65 |
-
story =
|
| 66 |
-
st.success("
|
| 67 |
st.write(story)
|
| 68 |
|
| 69 |
-
# Stage 3:
|
| 70 |
-
st.subheader("
|
| 71 |
-
with st.spinner("Converting
|
| 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()
|