import os import streamlit as st import requests from transformers import pipeline from typing import Dict from together import Together from utils import ( img2txt, txt2story, txt2speech, get_user_preferences, send_story_email, validate_email, validate_gmail_credentials ) def main(): st.set_page_config( page_title="🎨 Image-to-Audio Story 🎧", page_icon="🖼️", layout="wide" ) st.title("Turn the Image into Audio Story") # Email Configuration in Sidebar st.sidebar.markdown("## 📧 Email Configuration") with st.sidebar.expander("Configure Gmail Settings", expanded=not bool(os.getenv('SENDER_EMAIL'))): sender_email = st.text_input( "Gmail Address:", value=os.getenv('SENDER_EMAIL', ''), type="default", key="gmail" ) sender_password = st.text_input( "App Password:", value=os.getenv('SENDER_PASSWORD', ''), type="password", key="password", help="Use an App Password from your Google Account settings" ) if st.button("Save Configuration"): if not sender_email or not sender_password: st.error("Please provide both email and password.") elif not validate_email(sender_email): st.error("Please enter a valid Gmail address.") else: # Validate credentials error = validate_gmail_credentials(sender_email, sender_password) if error: st.error(f"Failed to validate credentials: {error}") else: # Save credentials to environment variables os.environ['SENDER_EMAIL'] = sender_email os.environ['SENDER_PASSWORD'] = sender_password st.success("Email configuration saved successfully!") # LLM Parameters in sidebar st.sidebar.markdown("# ⚙️ LLM Configuration") with st.sidebar.expander("Model Parameters", expanded=False): top_k = st.number_input("Top-K", min_value=1, max_value=100, value=5) top_p = st.number_input("Top-P", min_value=0.0, max_value=1.0, value=0.8) temperature = st.number_input("Temperature", min_value=0.1, max_value=2.0, value=1.5) # 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") # 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, top_p, temperature) # Convert to audio txt2speech(story) # Display results st.markdown("---") # Image caption with st.expander("📜 Image Caption", expanded=True): st.write(scenario) # Story text with st.expander("📖 Generated Story", expanded=True): st.write(story) # Audio player with st.expander("🎧 Audio Version", expanded=True): st.audio("audio_story.mp3") # Email section if os.getenv('SENDER_EMAIL') and os.getenv('SENDER_PASSWORD'): 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, story, "audio_story.mp3"): st.success("✉️ Story sent successfully! Check your email.") else: st.error("❌ Failed to send email. Please try again.") else: st.warning("⚠️ Please configure Gmail settings in the sidebar to enable email delivery.") except Exception as e: st.error(f"An error occurred: {str(e)}") st.warning("Please try again or contact support if the problem persists.") # Footer st.markdown("---") st.markdown("### Credits") st.caption(''' Made with ❤️ by @Aditya-Neural-Net-Ninja\n Utilizes Image-to-Text model, Text Generation model, Google Text-to-Speech library\n Gratitude to Streamlit, 🤗 Spaces for Deployment & Hosting ''') if __name__ == '__main__': main()