| import streamlit as st |
| from PIL import Image |
| import time |
| from transformers import pipeline |
| import tempfile |
| import os |
|
|
|
|
| |
| def image_to_caption(image_path): |
| """Generates a caption for the given image using a pre-trained model.""" |
| imgtocaption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") |
| caption = imgtocaption(image_path) |
| return caption[0]['generated_text'] |
|
|
| |
| def caption_to_story(text): |
| captiontostory = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2") |
| story = captiontostory(text, max_length=150, min_length=50) |
| return story[0]['generated_text'] |
|
|
| |
| def story_to_audio(text): |
| tts_pipe = pipeline("text-to-audio", model="facebook/mms-tts-eng") |
| audio_output = tts_pipe(text[:1000]) |
| return audio_output['audio'], audio_output['sampling_rate'] |
| |
| |
| 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!") |
|
|
| |
| uploaded_image = st.file_uploader("Choose a picture", type=["jpg", "jpeg", "png"], key="image_uploader") |
|
|
| |
| def main(): |
| if uploaded_image is not None: |
| try: |
| |
| with st.spinner("Processing image..."): |
| image = Image.open(uploaded_image) |
| st.image(image, caption="Uploaded Image", use_column_width=True) |
| |
| |
| with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file: |
| image.save(temp_file.name) |
| image_path = temp_file.name |
|
|
| |
| with st.spinner("Generating caption..."): |
| caption = image_to_caption(image_path) |
| st.subheader("Generated Caption") |
| st.write(caption) |
|
|
| |
| with st.spinner("Generating story..."): |
| story = caption_to_story(caption) |
| st.subheader("Generated Story") |
| st.write(story) |
|
|
| |
| with st.spinner("Generating audio..."): |
| audio_array, sample_rate = story_to_audio(story) |
| if audio_array is not None: |
| st.subheader("Audio Narration") |
| st.audio(audio_array, sample_rate=sample_rate) |
|
|
| except Exception as e: |
| st.error(f"An error occurred: {str(e)}") |
| finally: |
| |
| if 'image_path' in locals() and os.path.exists(image_path): |
| os.remove(image_path) |
|
|
| if __name__ == "__main__": |
| main() |