File size: 4,708 Bytes
e15f81c
3e74eb0
a814e5a
 
 
 
 
 
bcc2fa3
a814e5a
e7bd9fb
 
a814e5a
 
 
 
 
e7bd9fb
 
073629f
 
 
 
 
 
 
 
a814e5a
 
ad83944
a814e5a
 
 
 
 
143d77d
a814e5a
e7bd9fb
a814e5a
 
 
e7bd9fb
a814e5a
 
 
 
 
 
 
 
e7bd9fb
a814e5a
 
 
 
 
 
073629f
a814e5a
 
 
 
 
 
 
 
 
 
bcc2fa3
073629f
a814e5a
 
 
073629f
e7bd9fb
a814e5a
 
 
2b7f7bd
073629f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7bd9fb
a814e5a
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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()