Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -214,7 +214,7 @@ def generate_audio_from_story(story_text: str, output_path: str = "output.wav")
|
|
| 214 |
st.set_page_config(
|
| 215 |
page_title="Magic Story Generator",
|
| 216 |
page_icon="๐ง",
|
| 217 |
-
layout="
|
| 218 |
initial_sidebar_state="collapsed"
|
| 219 |
)
|
| 220 |
|
|
@@ -224,7 +224,7 @@ st.markdown("""
|
|
| 224 |
/* Primary title styling */
|
| 225 |
.main-title {
|
| 226 |
color: #E91E63;
|
| 227 |
-
font-size:
|
| 228 |
text-align: center;
|
| 229 |
padding: 20px;
|
| 230 |
text-shadow: 2px 2px #FFC107;
|
|
@@ -264,7 +264,13 @@ st.markdown('<p class="main-title">๐ง Welcome to Magic Story Maker!</p>', unsa
|
|
| 264 |
# File upload section
|
| 265 |
with st.container():
|
| 266 |
st.subheader("Step 1: Upload Your Picture")
|
| 267 |
-
uploaded_image = st.file_uploader("Choose an image...",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
# Main processing flow
|
| 270 |
if uploaded_image is not None:
|
|
@@ -284,55 +290,74 @@ if uploaded_image is not None:
|
|
| 284 |
help="Generate educational story with life lessons",
|
| 285 |
key="edu_btn"):
|
| 286 |
st.session_state.selected_prompt = "educational"
|
|
|
|
| 287 |
with col2:
|
| 288 |
if st.button("๐ Fantasy Adventure",
|
| 289 |
help="Create magical adventure story",
|
| 290 |
key="fantasy_btn"):
|
| 291 |
st.session_state.selected_prompt = "adventure"
|
|
|
|
| 292 |
with col3:
|
| 293 |
if st.button("๐ป Animal Friends",
|
| 294 |
help="Make story about friendly animals",
|
| 295 |
key="animal_btn"):
|
| 296 |
st.session_state.selected_prompt = "animal"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
#
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
"
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
"
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
"
|
|
|
|
|
|
|
|
|
|
| 311 |
}
|
| 312 |
-
}
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
# Generate image caption
|
| 317 |
-
image_caption = generate_image_caption(image)
|
| 318 |
-
|
| 319 |
-
# Generate story content
|
| 320 |
-
selected_template = PROMPT_TEMPLATES[st.session_state.selected_prompt]
|
| 321 |
-
story_text = generate_story_content(
|
| 322 |
-
system_prompt=selected_template["system"],
|
| 323 |
-
user_prompt=image_caption
|
| 324 |
-
)
|
| 325 |
|
| 326 |
-
#
|
| 327 |
-
st.
|
| 328 |
-
|
| 329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
# Help section
|
| 338 |
st.markdown("---")
|
|
@@ -340,6 +365,7 @@ st.subheader("๐ How to Use:")
|
|
| 340 |
st.info("""
|
| 341 |
1. Upload any picture (animals, nature, or people work best!)
|
| 342 |
2. Choose your favorite story style
|
| 343 |
-
3.
|
| 344 |
-
4.
|
|
|
|
| 345 |
""")
|
|
|
|
| 214 |
st.set_page_config(
|
| 215 |
page_title="Magic Story Generator",
|
| 216 |
page_icon="๐ง",
|
| 217 |
+
layout="centered",
|
| 218 |
initial_sidebar_state="collapsed"
|
| 219 |
)
|
| 220 |
|
|
|
|
| 224 |
/* Primary title styling */
|
| 225 |
.main-title {
|
| 226 |
color: #E91E63;
|
| 227 |
+
font-size: 6rem;
|
| 228 |
text-align: center;
|
| 229 |
padding: 20px;
|
| 230 |
text-shadow: 2px 2px #FFC107;
|
|
|
|
| 264 |
# File upload section
|
| 265 |
with st.container():
|
| 266 |
st.subheader("Step 1: Upload Your Picture")
|
| 267 |
+
uploaded_image = st.file_uploader("Choose an image...",
|
| 268 |
+
type=["png", "jpg", "jpeg"],
|
| 269 |
+
label_visibility="collapsed")
|
| 270 |
+
|
| 271 |
+
# Initialize session state for confirmation status
|
| 272 |
+
if 'confirmed' not in st.session_state:
|
| 273 |
+
st.session_state.confirmed = False
|
| 274 |
|
| 275 |
# Main processing flow
|
| 276 |
if uploaded_image is not None:
|
|
|
|
| 290 |
help="Generate educational story with life lessons",
|
| 291 |
key="edu_btn"):
|
| 292 |
st.session_state.selected_prompt = "educational"
|
| 293 |
+
st.session_state.confirmed = False # Reset confirmation
|
| 294 |
with col2:
|
| 295 |
if st.button("๐ Fantasy Adventure",
|
| 296 |
help="Create magical adventure story",
|
| 297 |
key="fantasy_btn"):
|
| 298 |
st.session_state.selected_prompt = "adventure"
|
| 299 |
+
st.session_state.confirmed = False
|
| 300 |
with col3:
|
| 301 |
if st.button("๐ป Animal Friends",
|
| 302 |
help="Make story about friendly animals",
|
| 303 |
key="animal_btn"):
|
| 304 |
st.session_state.selected_prompt = "animal"
|
| 305 |
+
st.session_state.confirmed = False
|
| 306 |
+
|
| 307 |
+
# Add confirmation button
|
| 308 |
+
with st.container():
|
| 309 |
+
st.subheader("Step 3: Confirm Selection")
|
| 310 |
+
if st.button("๐ฎ Start Magic Creation!",
|
| 311 |
+
help="Click to generate story after choosing style",
|
| 312 |
+
type="primary"):
|
| 313 |
+
st.session_state.confirmed = True
|
| 314 |
|
| 315 |
+
# Only show generation when confirmed
|
| 316 |
+
if st.session_state.get('confirmed', False):
|
| 317 |
+
# Define prompt templates
|
| 318 |
+
PROMPT_TEMPLATES = {
|
| 319 |
+
"educational": {
|
| 320 |
+
"system": "You are a children's educator. Create a simple 150-word story that teaches basic life skills or moral lessons.",
|
| 321 |
+
"icon": "๐"
|
| 322 |
+
},
|
| 323 |
+
"adventure": {
|
| 324 |
+
"system": "You are a fantasy writer. Create a 150-word magical adventure story suitable for children.",
|
| 325 |
+
"icon": "๐ "
|
| 326 |
+
},
|
| 327 |
+
"animal": {
|
| 328 |
+
"system": "You are an animal expert. Create a 150-word story about friendly animals learning together.",
|
| 329 |
+
"icon": "๐ป"
|
| 330 |
+
}
|
| 331 |
}
|
|
|
|
| 332 |
|
| 333 |
+
# Safe access with default fallback
|
| 334 |
+
selected_prompt = st.session_state.get("selected_prompt", "educational")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
+
# Story generation section
|
| 337 |
+
with st.spinner(f"{PROMPT_TEMPLATES[selected_prompt]['icon']} Creating your story..."):
|
| 338 |
+
# Generate image caption
|
| 339 |
+
image_caption = generate_image_caption(image)
|
| 340 |
+
|
| 341 |
+
# Generate story content
|
| 342 |
+
selected_template = PROMPT_TEMPLATES[selected_prompt]
|
| 343 |
+
story_text = generate_story_content(
|
| 344 |
+
system_prompt=selected_template["system"],
|
| 345 |
+
user_prompt=image_caption
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
# Display formatted story
|
| 349 |
+
st.subheader("โจ Your Magical Story")
|
| 350 |
+
st.markdown(f'<div class="story-container">{story_text}</div>',
|
| 351 |
+
unsafe_allow_html=True)
|
| 352 |
|
| 353 |
+
# Audio generation section
|
| 354 |
+
with st.spinner("๐ฎ Preparing story narration..."):
|
| 355 |
+
audio_file = generate_audio_from_story(story_text, "story_audio.wav")
|
| 356 |
+
st.subheader("๐ง Listen to Your Story")
|
| 357 |
+
st.audio(audio_file)
|
| 358 |
+
else:
|
| 359 |
+
# Show waiting message
|
| 360 |
+
st.info("โน๏ธ Please select a story style and click the confirmation button to continue")
|
| 361 |
|
| 362 |
# Help section
|
| 363 |
st.markdown("---")
|
|
|
|
| 365 |
st.info("""
|
| 366 |
1. Upload any picture (animals, nature, or people work best!)
|
| 367 |
2. Choose your favorite story style
|
| 368 |
+
3. Click the confirmation button
|
| 369 |
+
4. Wait for magic to happen!
|
| 370 |
+
5. Listen to your personalized story
|
| 371 |
""")
|