| | |
| | import streamlit as st |
| | from transformers import pipeline |
| | import textwrap |
| | import numpy as np |
| | import soundfile as sf |
| | import tempfile |
| | import os |
| | from PIL import Image |
| | import string |
| |
|
| | |
| | @st.cache_resource |
| | def load_pipelines(): |
| | captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large") |
| | storyer = pipeline("text-generation", model="aspis/gpt2-genre-story-generation") |
| | tts = pipeline("text-to-speech", model="facebook/mms-tts-eng") |
| | return captioner, storyer, tts |
| |
|
| | captioner, storyer, tts = load_pipelines() |
| |
|
| | |
| | |
| | def generate_content(image): |
| | pil_image = Image.open(image) |
| | |
| | |
| | caption = captioner(pil_image)[0]["generated_text"] |
| | st.write("**๐ What's in the picture: ๐**") |
| | st.write(caption) |
| |
|
| | |
| | prompt = ( |
| | f"Write a funny, warm children's story for ages 3-10, 50โ100 words, " |
| | f"Completely and precisely centered on this scene {caption}\nStory:" |
| | ) |
| | |
| | |
| | raw = storyer( |
| | prompt, |
| | max_new_tokens=200, |
| | temperature=0.7, |
| | top_p=0.9, |
| | no_repeat_ngram_size=2, |
| | return_full_text=False |
| | )[0]["generated_text"].strip() |
| |
|
| | |
| | allowed_chars = string.ascii_letters + string.digits + " .,!?\"'-" |
| | |
| | |
| | clean_raw = ''.join(c for c in raw if c in allowed_chars) |
| | |
| | |
| | words = clean_raw.split() |
| | story = " ".join(words[:100]) |
| | |
| | st.write("**๐ Your funny story: ๐**") |
| | st.write(story) |
| |
|
| | |
| | chunks = textwrap.wrap(story, width=200) |
| | audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks]) |
| |
|
| | |
| | with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file: |
| | sf.write(temp_file.name, audio, tts.model.config.sampling_rate) |
| | temp_file_path = temp_file.name |
| |
|
| | return caption, story, temp_file_path |
| |
|
| | |
| | st.title("โจ Magic Story Maker โจ") |
| | st.markdown("Upload a picture to make a funny story and hear it too! ๐ธ") |
| |
|
| | uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"]) |
| |
|
| | if uploaded_image is None: |
| | st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here! ๐ท", use_column_width=True) |
| | else: |
| | st.image(uploaded_image, caption="Your Picture ๐", use_column_width=True) |
| |
|
| | if st.button("โจ Make My Story! โจ"): |
| | if uploaded_image is not None: |
| | with st.spinner("๐ฎ Creating your magical story..."): |
| | caption, story, audio_path = generate_content(uploaded_image) |
| | st.success("๐ Your story is ready! ๐") |
| | st.audio(audio_path, format="audio/wav") |
| | os.remove(audio_path) |
| | else: |
| | st.warning("Please upload a picture first! ๐ธ") |