Spaces:
Build error
Build error
File size: 2,125 Bytes
9373b81 9e54ce8 9373b81 9e54ce8 9373b81 9e54ce8 9373b81 9e54ce8 9373b81 9e54ce8 9373b81 9e54ce8 9373b81 9e54ce8 9373b81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import streamlit as st
from transformers import pipeline
from PIL import Image
from gtts import gTTS
import tempfile
# Page configuration
st.set_page_config(page_title="π§Έ Story Generator (CPU Friendly)", page_icon="π")
st.title("πΌοΈ Image to Story Generator (CPU Version)")
st.write("Upload an image and enjoy a short story with audio narration β all without a GPU!")
# Image upload
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
if st.button("Generate Story"):
with st.spinner("π· Generating caption..."):
# Image captioning model (CPU-friendly)
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
caption = captioner(image)[0]['generated_text'].strip()
with st.spinner("βοΈ Generating story..."):
# Using Falcon-rw-1b text generation model (CPU-adapted)
story_prompt = f"A short and fun story for children about: {caption}"
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
story = generator(
story_prompt,
max_length=150,
do_sample=True,
temperature=0.9,
top_p=0.95
)[0]['generated_text'].strip()
# Limit maximum word count to 100
story = story.replace("\n", " ")
words = story.split()
if len(words) > 100:
story = " ".join(words[:100]) + "..."
with st.spinner("π Converting to speech..."):
# Text-to-speech
tts = gTTS(text=story, lang='en')
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(temp_file.name)
# Display results
st.subheader("π Generated Story")
st.write(story)
st.subheader("π Listen to the Story")
st.audio(temp_file.name, format="audio/mp3") |