ccclllwww commited on
Commit
644d364
ยท
verified ยท
1 Parent(s): 44bce0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -114
app.py CHANGED
@@ -173,120 +173,65 @@ st.markdown("""
173
  # ======================================
174
  # Main Application Interface
175
  # ======================================
176
- st.title("๐Ÿงš Welcome to Magic Story Maker!")
177
-
178
- # File upload section
179
- with st.container():
180
- st.subheader("Step 1: Upload Your Picture")
181
- uploaded_image = st.file_uploader("Choose an image...",
182
- type=["png", "jpg", "jpeg"],
183
- label_visibility="collapsed")
184
-
185
- # Initialize session state for confirmation status
186
- if 'confirmed' not in st.session_state:
187
- st.session_state.confirmed = False
188
-
189
- # Main processing flow
190
- if uploaded_image is not None:
191
- # Display uploaded image
192
- with st.spinner("โœจ Magical image processing..."):
193
- image = Image.open(uploaded_image)
194
- st.image(image, caption="Your Magical Image", use_column_width=True)
195
-
196
- # Prompt selection section
197
- with st.container():
198
- st.subheader("Step 2: Choose Story Style")
 
 
 
 
199
 
200
- # Create three columns for prompt buttons
201
- col1, col2, col3 = st.columns(3)
202
  with col1:
203
- if st.button("๐Ÿ“š Learning Story",
204
- help="Generate educational story with life lessons",
205
- key="edu_btn"):
206
- st.session_state.selected_prompt = "educational"
207
- st.session_state.confirmed = False
208
- with col2:
209
- if st.button("๐ŸŒ  Fantasy Adventure",
210
- help="Create magical adventure story",
211
- key="fantasy_btn"):
212
- st.session_state.selected_prompt = "adventure"
213
- st.session_state.confirmed = False
214
- with col3:
215
- if st.button("๐Ÿป Animal Friends",
216
- help="Make story about friendly animals",
217
- key="animal_btn"):
218
- st.session_state.selected_prompt = "animal"
219
- st.session_state.confirmed = False
220
-
221
- # Add confirmation button
222
- with st.container():
223
- st.subheader("Step 3: Confirm Selection")
224
- if st.button("๐Ÿ”ฎ Start Magic Creation!",
225
- help="Click to generate story after choosing style",
226
- type="primary"):
227
- st.session_state.confirmed = True
228
-
229
- # Only show generation when confirmed
230
- if st.session_state.get('confirmed', False):
231
- # Generate image caption with loading state
232
- with st.spinner("๐Ÿ” Analyzing image and generating description..."):
233
- image_caption = generate_image_caption(image)
234
-
235
- # Display caption results using CSS class
236
- st.subheader("๐Ÿ“ Image Understanding")
237
- st.markdown(f'<div class="story-container image-caption">{image_caption}</div>',
238
- unsafe_allow_html=True)
239
- st.write("") # Add spacing
240
-
241
- # Define prompt templates
242
- PROMPT_TEMPLATES = {
243
- "educational": {
244
- "system": "You are a children's educator writer. Create a 100-word educational story about ",
245
- "icon": "๐Ÿ“š"
246
- },
247
- "adventure": {
248
- "system": "You are a fantasy writer. Create a 100-word adventure story about ",
249
- "icon": "๐ŸŒ "
250
- },
251
- "animal": {
252
- "system": "You are an animal themes writer. Create a 100-word animal fairy tales about ",
253
- "icon": "๐Ÿป"
254
- }
255
- }
256
-
257
- # Safe access with default fallback
258
- selected_prompt = st.session_state.get("selected_prompt", "educational")
259
 
260
- # Story generation section
261
- with st.spinner(f"{PROMPT_TEMPLATES[selected_prompt]['icon']} Creating your story..."):
262
- # Generate story content using the caption
263
- selected_template = PROMPT_TEMPLATES[selected_prompt]
264
- story_text = generate_story_content(
265
- system_prompt=selected_template["system"],
266
- user_prompt=image_caption
267
- )
268
-
269
- # Display formatted story
270
- st.subheader("โœจ Your Magical Story")
271
- st.markdown(f'<div class="story-container">{story_text}</div>',
272
- unsafe_allow_html=True)
273
-
274
- # Audio generation section
275
- with st.spinner("๐Ÿ”ฎ Preparing story narration..."):
276
- audio_file = generate_audio_from_story(story_text)
277
- st.subheader("๐ŸŽง Listen to Your Story")
278
- st.audio(data=audio_file["audio"],sample_rate=audio_file["sampling_rate"])
279
- else:
280
- # Show waiting message
281
- st.info("โ„น๏ธ Please select a story style and click the confirmation button to continue")
282
-
283
- # Help section
284
- st.markdown("---")
285
- st.subheader("๐ŸŒŸ How to Use:")
286
- st.info("""
287
- 1. Upload any picture (animals, nature, or people work best!)
288
- 2. Choose your favorite story style
289
- 3. Click the confirmation button
290
- 4. Wait for image analysis to complete
291
- 5. Enjoy your personalized story and audio!
292
- """)
 
173
  # ======================================
174
  # Main Application Interface
175
  # ======================================
176
+ def main():
177
+ """Main application interface for Streamlit"""
178
+ # Page configuration
179
+ st.set_page_config(
180
+ page_title="Fantasy Adventure Generator",
181
+ layout="wide",
182
+ initial_sidebar_state="collapsed"
183
+ )
184
+
185
+ # Title and description
186
+ st.title("๐Ÿง™โ€โ™‚๏ธ Fantasy Adventure Story Generator")
187
+ st.markdown("""
188
+ Upload an image and get:
189
+ - Automatic scene description
190
+ - AI-generated adventure story
191
+ - Audio version of the story
192
+ """)
193
+
194
+ # File uploader
195
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
196
+
197
+ if uploaded_file is not None:
198
+ # Process image
199
+ image = Image.open(uploaded_file).convert("RGB")
200
+
201
+ # Layout columns
202
+ col1, col2 = st.columns(2)
203
 
 
 
204
  with col1:
205
+ st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
+ # Generation button
208
+ if st.button("โœจ Generate Story & Audio"):
209
+ with st.spinner("Processing your request..."):
210
+ # Generate outputs
211
+ caption = generate_image_caption(image)
212
+ sys_prompt = "You are a fantasy writer. Create a 100-word adventure story about "
213
+ story = generate_story_content(sys_prompt, caption)
214
+ speech = generate_audio_from_story(story)
215
+
216
+ # Display results
217
+ with col2:
218
+ st.subheader("๐Ÿ” Scene Description")
219
+ st.write(caption)
220
+
221
+ st.subheader("๐Ÿ“– Generated Story")
222
+ st.write(story)
223
+
224
+ st.subheader("๐Ÿ”Š Audio Playback")
225
+ st.audio(speech["audio"], format='audio/wav')
226
+
227
+ # Download button
228
+ with open(speech["audio"], "rb") as audio_file:
229
+ st.download_button(
230
+ label="๐Ÿ“ฅ Download Audio",
231
+ data=audio_file,
232
+ file_name="fantasy_adventure.wav",
233
+ mime="audio/wav"
234
+ )
235
+
236
+ if __name__ == "__main__":
237
+ main()