import os import streamlit as st from utils import ( img2txt, txt2story, txt2speech, get_user_preferences, send_story_email, validate_email ) def main(): st.set_page_config( page_title="🎨 Image-to-Audio Story 🎧", page_icon="🖼️", layout="wide" ) st.title("Turn the Image into Audio Story") # Initialize session state variables if "story" not in st.session_state: st.session_state.story = "" if "audio_file_path" not in st.session_state: st.session_state.audio_file_path = "" if "caption" not in st.session_state: st.session_state.caption = "" # Main content area col1, col2 = st.columns([2, 3]) with col1: # Image upload section st.markdown("## 📷 Upload Image") uploaded_file = st.file_uploader( "Choose an image...", type=["jpg", "jpeg", "png"] ) # Story preferences section st.markdown("## 🎭 Story Preferences") preferences = get_user_preferences() with col2: if uploaded_file is not None: # Display uploaded image st.markdown("## 🖼️ Your Image") bytes_data = uploaded_file.read() with open("uploaded_image.jpg", "wb") as file: file.write(bytes_data) st.image(uploaded_file, use_column_width=True) # Process image and generate story if st.button("🎨 Generate Story"): with st.spinner("🤖 AI is working its magic..."): try: # Get image description scenario = img2txt("uploaded_image.jpg") st.session_state.caption = scenario # Store caption in session state # Create story prompt prompt = f"""Based on the image description: '{scenario}', create a {preferences['genre']} story set in {preferences['setting']} in {preferences['continent']}. The story should have a {preferences['tone']} tone and explore the theme of {preferences['theme']}. The main conflict should be {preferences['conflict']}. The story should have a {preferences['twist']} and end with a {preferences['ending']} ending.""" # Generate story story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5) st.session_state.story = story # Store story in session state # Convert to audio txt2speech(story) st.session_state.audio_file_path = "audio_story.mp3" # Store audio path in session state except Exception as e: st.error(f"An error occurred: {str(e)}") st.warning("Please try again or contact support if the problem persists.") # Display results if story exists in session state if st.session_state.story: st.markdown("---") # Image caption with st.expander("📜 Image Caption", expanded=True): st.write(st.session_state.caption) # Story text with st.expander("📖 Generated Story", expanded=True): st.write(st.session_state.story) # Audio player with st.expander("🎧 Audio Version", expanded=True): st.audio(st.session_state.audio_file_path) # Email section st.markdown("---") st.markdown("## 📧 Get Story via Email") email = st.text_input( "Enter your email address:", help="We'll send you the story text and audio file" ) if st.button("📤 Send to Email"): if not email: st.warning("Please enter an email address.") elif not validate_email(email): st.error("Please enter a valid email address.") else: with st.spinner("📨 Sending email..."): if send_story_email(email, st.session_state.story, st.session_state.audio_file_path): st.success("✉️ Story sent successfully! Check your email.") else: st.error("❌ Failed to send email. Please try again.") if __name__ == '__main__': main()