Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
# Load Hugging Face pipelines
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_pipelines():
|
| 12 |
+
image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 13 |
+
story_generator = pipeline("text-generation", model="Tevatron/tiny-stories-generator", max_length=200)
|
| 14 |
+
text_to_speech = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits", framework="pt")
|
| 15 |
+
return image_captioner, story_generator, text_to_speech
|
| 16 |
+
|
| 17 |
+
# Define main interface
|
| 18 |
+
def main():
|
| 19 |
+
st.set_page_config(page_title="Kids Story Maker ๐ง๐", page_icon="๐ธ", layout="centered")
|
| 20 |
+
|
| 21 |
+
# Child-friendly header
|
| 22 |
+
st.markdown("<h1 style='color:#FF69B4; font-family:Comic Sans MS;'>๐จ Welcome to Kids Story Maker!</h1>", unsafe_allow_html=True)
|
| 23 |
+
st.markdown("<h3 style='color:#4CAF50;'>Upload a picture, and we'll turn it into a magical story and voice! ๐ปโจ</h3>", unsafe_allow_html=True)
|
| 24 |
+
|
| 25 |
+
image_captioner, story_generator, text_to_speech = load_pipelines()
|
| 26 |
+
|
| 27 |
+
uploaded_file = st.file_uploader("๐ผ๏ธ Upload an image:", type=["png", "jpg", "jpeg"])
|
| 28 |
+
|
| 29 |
+
if uploaded_file:
|
| 30 |
+
image = Image.open(uploaded_file)
|
| 31 |
+
st.image(image, caption="Your Image", use_column_width=True)
|
| 32 |
+
|
| 33 |
+
with st.spinner("๐ Generating a description..."):
|
| 34 |
+
caption = image_captioner(image)[0]['generated_text']
|
| 35 |
+
st.success(f"๐ Description: {caption}")
|
| 36 |
+
|
| 37 |
+
# Prompt template for storytelling
|
| 38 |
+
story_prompt = f"Write a short story for children aged 3 to 10 based on this description: {caption}. The story should be creative, friendly, and use simple words."
|
| 39 |
+
|
| 40 |
+
with st.spinner("โ๏ธ Creating your story..."):
|
| 41 |
+
story = story_generator(story_prompt)[0]['generated_text']
|
| 42 |
+
st.success("๐ Here's your story:")
|
| 43 |
+
st.write(story)
|
| 44 |
+
|
| 45 |
+
with st.spinner("๐ Turning your story into voice..."):
|
| 46 |
+
speech = text_to_speech(story)[0]['audio']
|
| 47 |
+
st.audio(speech, format="audio/wav")
|
| 48 |
+
|
| 49 |
+
# Run the app
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
main()
|