Spaces:
Sleeping
Sleeping
| # Import Part | |
| import streamlit as st | |
| from PIL import Image | |
| import gtts | |
| import io | |
| import time | |
| import tensorflow as tf | |
| from transformers import pipeline | |
| # Generates a caption for the given image using a pre-trained model | |
| def image_to_caption(image_path): | |
| imgtocaption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
| caption = imgtocaption(image_path)[0]['generated_text'] | |
| return caption | |
| # Generates a story for the caption using a pre-trained model | |
| def caption_to_story(text): | |
| captiontostory = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2") | |
| story = captiontostory(text, max_length=150, min_length=50)[0]['generated_text'] | |
| return story | |
| # Generates an audio for the story | |
| def story_to_audio(text): | |
| audio = io.BytesIO() | |
| tts = gTTS(text=text, lang='en', slow=False) | |
| tts.write_to_fp(audio) | |
| audio.seek(0) | |
| return audio | |
| # Child-Friendly Interface Design | |
| st.set_page_config(page_title="Magic Story House", page_icon="🧚") | |
| st.title("🧚 Magic Image Story Generator") | |
| st.markdown("Upload an image and generate your exclusive fairy tale!") | |
| # File Upload | |
| uploaded_image = st.file_uploader("Choose a picture", type=["jpg", "jpeg", "png"], key="image_uploader") | |
| # Main Part | |
| if uploaded_image is not None: | |
| # Display the uploaded image | |
| st.image(uploaded_image, caption='Uploaded Image', use_column_width=True) | |
| # Save the uploaded image as a temporary file since the pipeline requires a file path as input | |
| import tempfile | |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file: | |
| tmp_file.write(uploaded_image.getvalue()) | |
| tmp_file_path = tmp_file.name | |
| # Generate an image caption | |
| caption = image_to_caption(tmp_file_path) | |
| st.write(f"Caption: {caption}") | |
| # Generate a story based on the caption | |
| story = caption_to_story(caption) | |
| st.write(f"Story: {story}") | |
| # Convert the story to audio | |
| audio = story_to_audio(story) | |
| if audio: | |
| st.audio(audio, format='audio/mp3') | |
| # Delete the temporary file | |
| import os | |
| os.remove(tmp_file_path) | |