Upload 2 files
Browse files- app.py +124 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Initialize session state to hold the generated document bytes
|
| 5 |
+
if "docx_bytes" not in st.session_state:
|
| 6 |
+
st.session_state.docx_bytes = None
|
| 7 |
+
|
| 8 |
+
st.title("Bedtime Story Generator")
|
| 9 |
+
st.write(
|
| 10 |
+
"Choose from the options below or enter your own values to generate a personalized bedtime story."
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
with st.form(key="story_form"):
|
| 14 |
+
# 1. Age Range
|
| 15 |
+
age_options = ["3-5", "6-8", "9-12"]
|
| 16 |
+
selected_age = st.selectbox("Age Range", age_options, index=0)
|
| 17 |
+
custom_age = st.text_input(
|
| 18 |
+
"Or enter a custom Age Range (e.g., 2-4, 13-15)"
|
| 19 |
+
)
|
| 20 |
+
age_value = custom_age.strip() if custom_age.strip() else selected_age
|
| 21 |
+
|
| 22 |
+
# 2. Theme
|
| 23 |
+
theme_options = ["adventure", "friendship", "magic", "space", "bedtime calming"]
|
| 24 |
+
selected_theme = st.selectbox("Theme", theme_options, index=0)
|
| 25 |
+
custom_theme = st.text_input("Or enter a custom Theme")
|
| 26 |
+
theme_value = custom_theme.strip() if custom_theme.strip() else selected_theme
|
| 27 |
+
|
| 28 |
+
# 3. Number of Pages
|
| 29 |
+
# We map option labels to their numeric values
|
| 30 |
+
pages_options = {"Short (5)": 5, "Medium (10)": 10, "Long (15)": 15}
|
| 31 |
+
selected_pages_option = st.selectbox("Number of Pages", list(pages_options.keys()), index=1)
|
| 32 |
+
custom_pages = st.text_input(
|
| 33 |
+
"Or enter a custom number of pages", placeholder="e.g. 7"
|
| 34 |
+
)
|
| 35 |
+
if custom_pages.strip():
|
| 36 |
+
try:
|
| 37 |
+
pages_value = int(custom_pages.strip())
|
| 38 |
+
except ValueError:
|
| 39 |
+
st.error("Invalid custom number of pages. Using selected option.")
|
| 40 |
+
pages_value = pages_options[selected_pages_option]
|
| 41 |
+
else:
|
| 42 |
+
pages_value = pages_options[selected_pages_option]
|
| 43 |
+
|
| 44 |
+
# 4. Reading Time
|
| 45 |
+
time_options = {"3 min": 3, "5 min": 5, "10 min": 10}
|
| 46 |
+
selected_time_option = st.selectbox("Reading Time", list(time_options.keys()), index=1)
|
| 47 |
+
custom_time = st.text_input(
|
| 48 |
+
"Or enter a custom reading time (in minutes)", placeholder="e.g. 7"
|
| 49 |
+
)
|
| 50 |
+
if custom_time.strip():
|
| 51 |
+
try:
|
| 52 |
+
time_value = int(custom_time.strip())
|
| 53 |
+
except ValueError:
|
| 54 |
+
st.error("Invalid custom reading time. Using selected option.")
|
| 55 |
+
time_value = time_options[selected_time_option]
|
| 56 |
+
else:
|
| 57 |
+
time_value = time_options[selected_time_option]
|
| 58 |
+
|
| 59 |
+
# 5. Story Tone
|
| 60 |
+
tone_options = ["fun", "calming", "inspiring", "silly"]
|
| 61 |
+
selected_tone = st.selectbox("Story Tone", tone_options, index=0)
|
| 62 |
+
custom_tone = st.text_input("Or enter a custom Story Tone")
|
| 63 |
+
tone_value = custom_tone.strip() if custom_tone.strip() else selected_tone
|
| 64 |
+
|
| 65 |
+
# 6. Setting
|
| 66 |
+
setting_options = ["forest", "ocean", "outer space", "castle"]
|
| 67 |
+
selected_setting = st.selectbox("Setting", setting_options, index=0)
|
| 68 |
+
custom_setting = st.text_input("Or enter a custom Setting")
|
| 69 |
+
setting_value = custom_setting.strip() if custom_setting.strip() else selected_setting
|
| 70 |
+
|
| 71 |
+
# 7. Lesson or Moral (optional)
|
| 72 |
+
moral_options = ["None", "kindness", "bravery", "sharing"]
|
| 73 |
+
selected_moral = st.selectbox("Lesson or Moral (optional)", moral_options, index=0)
|
| 74 |
+
custom_moral = st.text_input("Or enter a custom Moral/Lesson (leave blank for none)")
|
| 75 |
+
if custom_moral.strip():
|
| 76 |
+
moral_value = custom_moral.strip()
|
| 77 |
+
elif selected_moral == "None":
|
| 78 |
+
moral_value = ""
|
| 79 |
+
else:
|
| 80 |
+
moral_value = selected_moral
|
| 81 |
+
|
| 82 |
+
# 8. Illustration Style
|
| 83 |
+
illustration_options = ["cartoon", "watercolor", "sketch", "none"]
|
| 84 |
+
selected_illustration = st.selectbox("Illustration Style", illustration_options, index=0)
|
| 85 |
+
custom_illustration = st.text_input("Or enter a custom Illustration Style")
|
| 86 |
+
illustration_value = custom_illustration.strip() if custom_illustration.strip() else selected_illustration
|
| 87 |
+
|
| 88 |
+
submit_button = st.form_submit_button(label="Generate Story")
|
| 89 |
+
|
| 90 |
+
# When the form is submitted, build the payload and call the FastAPI endpoint
|
| 91 |
+
if submit_button:
|
| 92 |
+
payload = {
|
| 93 |
+
"Age": age_value,
|
| 94 |
+
"Theme": theme_value,
|
| 95 |
+
"Pages": pages_value,
|
| 96 |
+
"Time": time_value,
|
| 97 |
+
"Tone": tone_value,
|
| 98 |
+
"Setting": setting_value,
|
| 99 |
+
"Moral": moral_value,
|
| 100 |
+
"IllustrationStyle": illustration_value, # Ensure your FastAPI endpoint supports this parameter.
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
fastapi_url = "http://localhost:8000/generate-story" # Update if your endpoint URL differs.
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
with st.spinner("Generating your story, please wait..."):
|
| 107 |
+
response = requests.post(fastapi_url, json=payload)
|
| 108 |
+
|
| 109 |
+
if response.status_code == 200:
|
| 110 |
+
st.session_state.docx_bytes = response.content
|
| 111 |
+
st.success("Story generated successfully!")
|
| 112 |
+
else:
|
| 113 |
+
st.error(f"Error {response.status_code}: {response.text}")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
st.error(f"An error occurred: {str(e)}")
|
| 116 |
+
|
| 117 |
+
# If a document was generated, show a download button
|
| 118 |
+
if st.session_state.docx_bytes is not None:
|
| 119 |
+
st.download_button(
|
| 120 |
+
label="Download Your Bedtime Story",
|
| 121 |
+
data=st.session_state.docx_bytes,
|
| 122 |
+
file_name="bedtime_story.docx",
|
| 123 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 124 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain_openai
|
| 2 |
+
pydantic
|
| 3 |
+
python-docx
|
| 4 |
+
Pillow
|
| 5 |
+
requests
|
| 6 |
+
python-dotenv
|
| 7 |
+
uvicorn
|
| 8 |
+
fastapi
|
| 9 |
+
streamlit
|