File size: 5,116 Bytes
e730989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
import streamlit as st
import requests

# Initialize session state to hold the generated document bytes
if "docx_bytes" not in st.session_state:
    st.session_state.docx_bytes = None

st.title("Bedtime Story Generator")
st.write(
    "Choose from the options below or enter your own values to generate a personalized bedtime story."
)

with st.form(key="story_form"):
    # 1. Age Range
    age_options = ["3-5", "6-8", "9-12"]
    selected_age = st.selectbox("Age Range", age_options, index=0)
    custom_age = st.text_input(
        "Or enter a custom Age Range (e.g., 2-4, 13-15)"
    )
    age_value = custom_age.strip() if custom_age.strip() else selected_age

    # 2. Theme
    theme_options = ["adventure", "friendship", "magic", "space", "bedtime calming"]
    selected_theme = st.selectbox("Theme", theme_options, index=0)
    custom_theme = st.text_input("Or enter a custom Theme")
    theme_value = custom_theme.strip() if custom_theme.strip() else selected_theme

    # 3. Number of Pages
    # We map option labels to their numeric values
    pages_options = {"Short (5)": 5, "Medium (10)": 10, "Long (15)": 15}
    selected_pages_option = st.selectbox("Number of Pages", list(pages_options.keys()), index=1)
    custom_pages = st.text_input(
        "Or enter a custom number of pages", placeholder="e.g. 7"
    )
    if custom_pages.strip():
        try:
            pages_value = int(custom_pages.strip())
        except ValueError:
            st.error("Invalid custom number of pages. Using selected option.")
            pages_value = pages_options[selected_pages_option]
    else:
        pages_value = pages_options[selected_pages_option]

    # 4. Reading Time
    time_options = {"3 min": 3, "5 min": 5, "10 min": 10}
    selected_time_option = st.selectbox("Reading Time", list(time_options.keys()), index=1)
    custom_time = st.text_input(
        "Or enter a custom reading time (in minutes)", placeholder="e.g. 7"
    )
    if custom_time.strip():
        try:
            time_value = int(custom_time.strip())
        except ValueError:
            st.error("Invalid custom reading time. Using selected option.")
            time_value = time_options[selected_time_option]
    else:
        time_value = time_options[selected_time_option]

    # 5. Story Tone
    tone_options = ["fun", "calming", "inspiring", "silly"]
    selected_tone = st.selectbox("Story Tone", tone_options, index=0)
    custom_tone = st.text_input("Or enter a custom Story Tone")
    tone_value = custom_tone.strip() if custom_tone.strip() else selected_tone

    # 6. Setting
    setting_options = ["forest", "ocean", "outer space", "castle"]
    selected_setting = st.selectbox("Setting", setting_options, index=0)
    custom_setting = st.text_input("Or enter a custom Setting")
    setting_value = custom_setting.strip() if custom_setting.strip() else selected_setting

    # 7. Lesson or Moral (optional)
    moral_options = ["None", "kindness", "bravery", "sharing"]
    selected_moral = st.selectbox("Lesson or Moral (optional)", moral_options, index=0)
    custom_moral = st.text_input("Or enter a custom Moral/Lesson (leave blank for none)")
    if custom_moral.strip():
        moral_value = custom_moral.strip()
    elif selected_moral == "None":
        moral_value = ""
    else:
        moral_value = selected_moral

    # 8. Illustration Style
    illustration_options = ["cartoon", "watercolor", "sketch", "none"]
    selected_illustration = st.selectbox("Illustration Style", illustration_options, index=0)
    custom_illustration = st.text_input("Or enter a custom Illustration Style")
    illustration_value = custom_illustration.strip() if custom_illustration.strip() else selected_illustration

    submit_button = st.form_submit_button(label="Generate Story")

# When the form is submitted, build the payload and call the FastAPI endpoint
if submit_button:
    payload = {
        "Age": age_value,
        "Theme": theme_value,
        "Pages": pages_value,
        "Time": time_value,
        "Tone": tone_value,
        "Setting": setting_value,
        "Moral": moral_value,
        "IllustrationStyle": illustration_value,  # Ensure your FastAPI endpoint supports this parameter.
    }

    fastapi_url = "http://localhost:8000/generate-story"  # Update if your endpoint URL differs.

    try:
        with st.spinner("Generating your story, please wait..."):
            response = requests.post(fastapi_url, json=payload)

        if response.status_code == 200:
            st.session_state.docx_bytes = response.content
            st.success("Story generated successfully!")
        else:
            st.error(f"Error {response.status_code}: {response.text}")
    except Exception as e:
        st.error(f"An error occurred: {str(e)}")

# If a document was generated, show a download button
if st.session_state.docx_bytes is not None:
    st.download_button(
        label="Download Your Bedtime Story",
        data=st.session_state.docx_bytes,
        file_name="bedtime_story.docx",
        mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    )