mgbam commited on
Commit
3399f15
Β·
verified Β·
1 Parent(s): ad99fb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -277
app.py CHANGED
@@ -3,61 +3,45 @@ import streamlit as st
3
  from core.gemini_handler import GeminiHandler
4
  from core.visual_engine import VisualEngine
5
  from core.prompt_engineering import (
6
- create_cinematic_treatment_prompt,
7
  construct_dalle_prompt,
8
  create_narration_script_prompt_enhanced,
9
  create_scene_regeneration_prompt,
10
  create_visual_regeneration_prompt
11
  )
12
  import os
 
13
 
14
  # --- Configuration & Initialization ---
15
  st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
16
- # For robust logging, especially on deployed environments
17
- import logging
18
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
19
  logger = logging.getLogger(__name__)
20
 
21
  # --- Global State Variables & API Key Setup ---
22
  def load_api_key(key_name_streamlit, key_name_env, service_name):
23
- key = None
24
- secrets_available = hasattr(st, 'secrets')
25
  try:
26
  if secrets_available and key_name_streamlit in st.secrets:
27
  key = st.secrets[key_name_streamlit]
28
  if key: logger.info(f"{service_name} API Key loaded from Streamlit secrets.")
29
- except Exception as e:
30
- logger.warning(f"Could not access st.secrets for {key_name_streamlit} (may be local dev or misconfiguration): {e}")
31
-
32
  if not key and key_name_env in os.environ:
33
  key = os.environ[key_name_env]
34
  if key: logger.info(f"{service_name} API Key loaded from environment variable.")
35
-
36
- if not key:
37
- logger.warning(f"{service_name} API Key NOT FOUND in secrets or environment variables.")
38
  return key
39
 
40
- # Initialize API Keys and handlers once using session state
41
  if 'services_initialized' not in st.session_state:
42
- logger.info("Initializing services and API keys for the first time...")
43
  st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY", "Gemini")
44
  st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
45
  st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
46
  st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
47
-
48
- if not st.session_state.GEMINI_API_KEY:
49
- st.error("CRITICAL: Gemini API Key is essential and missing! Application cannot proceed.")
50
- logger.error("Gemini API Key missing. Halting application.")
51
- st.stop()
52
-
53
  try:
54
  st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
55
- logger.info("GeminiHandler initialized successfully.")
56
- except Exception as e:
57
- st.error(f"Failed to initialize GeminiHandler: {e}")
58
- logger.error(f"GeminiHandler initialization failed: {e}")
59
- st.stop()
60
-
61
  try:
62
  st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
63
  st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
@@ -65,322 +49,219 @@ if 'services_initialized' not in st.session_state:
65
  st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
66
  logger.info("VisualEngine initialized and API keys set.")
67
  except Exception as e:
68
- st.error(f"Failed to initialize VisualEngine or set its API keys: {e}")
69
- logger.error(f"VisualEngine initialization/key setting failed: {e}")
70
- # Don't stop, visual engine has fallbacks, but warn user
71
- st.warning("VisualEngine encountered an issue during setup. Some visual/audio features might use placeholders or be disabled.")
72
 
73
- st.session_state.services_initialized = True
74
- logger.info("Service initialization complete.")
75
-
76
- # Initialize other session state variables
77
  for key, default_val in [
78
  ('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
79
  ('video_path', None), ('character_definitions', {}), ('global_style_additions', ""),
80
  ('overall_narration_audio_path', None), ('narration_script_display', "")
81
  ]:
82
  if key not in st.session_state: st.session_state[key] = default_val
83
- # --- End State & API Key Setup ---
84
 
 
85
  def initialize_new_project():
86
- st.session_state.story_treatment_scenes = []
87
- st.session_state.scene_dalle_prompts = []
88
- st.session_state.generated_visual_paths = []
89
- st.session_state.video_path = None
90
- st.session_state.overall_narration_audio_path = None
91
- st.session_state.narration_script_display = ""
92
- logger.info("New project initialized, session state cleared.")
93
- # Optional: Clean up old media files
94
- # output_dir = st.session_state.visual_engine.output_dir
95
- # if os.path.exists(output_dir):
96
- # logger.info(f"Cleaning up old media in {output_dir}")
97
- # for f_name in os.listdir(output_dir):
98
- # try: os.remove(os.path.join(output_dir, f_name))
99
- # except Exception as e: logger.warning(f"Could not remove old file {f_name}: {e}")
100
-
101
 
102
  def generate_visual_for_scene_core(scene_index, scene_data, version=1):
103
- scene_num_log = scene_data.get('scene_number', scene_index + 1)
104
- logger.info(f"Generating DALL-E prompt for Scene {scene_num_log} (v{version}).")
105
- dalle_prompt = construct_dalle_prompt(
106
- scene_data,
107
- st.session_state.character_definitions,
108
- st.session_state.global_style_additions
109
- )
110
- if not dalle_prompt:
111
- logger.error(f"DALL-E prompt construction failed for Scene {scene_num_log}.")
112
- return False
113
-
114
  while len(st.session_state.scene_dalle_prompts) <= scene_index: st.session_state.scene_dalle_prompts.append("")
115
  while len(st.session_state.generated_visual_paths) <= scene_index: st.session_state.generated_visual_paths.append(None)
116
  st.session_state.scene_dalle_prompts[scene_index] = dalle_prompt
117
-
118
- filename = f"scene_{scene_num_log}_visual_v{version}.png"
119
- logger.info(f"Calling VisualEngine to generate visual for Scene {scene_num_log} with filename {filename}.")
120
  img_path = st.session_state.visual_engine.generate_image_visual(dalle_prompt, scene_data, filename)
121
-
122
  if img_path and os.path.exists(img_path):
123
- st.session_state.generated_visual_paths[scene_index] = img_path
124
- logger.info(f"Visual successfully generated for Scene {scene_num_log}: {img_path}")
125
- return True
126
  else:
127
- st.session_state.generated_visual_paths[scene_index] = None
128
- logger.warning(f"Visual generation FAILED for Scene {scene_num_log}. img_path was: {img_path}")
129
- return False
130
 
131
- # --- UI Sidebar ---
132
  with st.sidebar:
133
- st.title("🎬 CineGen AI Ultra+")
134
- st.markdown("### Creative Seed")
135
  user_idea = st.text_area("Core Story Idea / Theme:", "A lone wanderer searches for a mythical oasis in a vast, post-apocalyptic desert, haunted by mirages and mechanical scavengers.", height=120, key="user_idea_main")
136
  genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
137
  mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main")
138
- num_scenes = st.slider("Number of Key Scenes:", 1, 3, 1, key="num_scenes_main") # Default 1 for faster testing initially
139
-
140
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
141
  selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
142
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
143
-
144
  if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
145
  initialize_new_project()
146
  if not user_idea.strip(): st.warning("Please provide a story idea.")
147
  else:
148
  with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status:
149
  try:
150
- status.write("Phase 1: Gemini crafting cinematic treatment... πŸ“œ")
151
- logger.info("Initiating Phase 1: Cinematic Treatment Generation.")
152
  treatment_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance)
153
  treatment_result_json = st.session_state.gemini_handler.generate_story_breakdown(treatment_prompt)
154
- if not isinstance(treatment_result_json, list) or not treatment_result_json:
155
- raise ValueError("Gemini did not return a valid list of scenes for the treatment.")
156
- st.session_state.story_treatment_scenes = treatment_result_json
157
- num_gen_scenes = len(st.session_state.story_treatment_scenes)
158
- st.session_state.scene_dalle_prompts = [""] * num_gen_scenes
159
- st.session_state.generated_visual_paths = [None] * num_gen_scenes
160
- logger.info(f"Phase 1 complete. Generated {num_gen_scenes} scenes.")
161
  status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
162
-
163
- status.write("Phase 2: Creating visuals (DALL-E/Pexels)... πŸ–ΌοΈ (This may take time per scene)")
164
- logger.info("Initiating Phase 2: Visual Generation.")
165
  visual_successes = 0
166
- for i_scene, scene_content in enumerate(st.session_state.story_treatment_scenes):
167
- scene_num_log_ph2 = scene_content.get('scene_number', i_scene+1)
168
- status.write(f" Creating visual for Scene {scene_num_log_ph2}: {scene_content.get('scene_title','Untitled')}...")
169
- logger.info(f" Processing visual for Scene {scene_num_log_ph2}.")
170
- if generate_visual_for_scene_core(i_scene, scene_content, version=1): visual_successes += 1
171
-
172
- if visual_successes == 0 and num_gen_scenes > 0:
173
- logger.error("Visual generation failed for all scenes.")
174
- status.update(label="Visual generation failed for all scenes. Check logs & API status.", state="error", expanded=True); st.stop()
175
- elif visual_successes < num_gen_scenes:
176
- logger.warning(f"Visuals partially generated ({visual_successes}/{num_gen_scenes}).")
177
- status.update(label=f"Visuals ready ({visual_successes}/{num_gen_scenes} succeeded). Generating narration...", state="running")
178
- else:
179
- logger.info("All visuals generated successfully.")
180
- status.update(label="Visuals ready! Generating narration script...", state="running")
181
-
182
- # Narration Generation
183
- status.write("Phase 3: Generating narration script with Gemini... 🎀")
184
- logger.info("Initiating Phase 3: Narration Script Generation.")
185
- selected_voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer")
186
- narration_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, selected_voice_style)
187
- narr_script = st.session_state.gemini_handler.generate_image_prompt(narration_prompt)
188
- st.session_state.narration_script_display = narr_script
189
- logger.info("Narration script generated.")
190
- status.update(label="Narration script ready! Synthesizing voice...", state="running")
191
-
192
- status.write("Phase 4: Synthesizing voice with ElevenLabs... πŸ”Š")
193
- logger.info("Initiating Phase 4: Voice Synthesis.")
194
- st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(narr_script)
195
- if st.session_state.overall_narration_audio_path:
196
- logger.info("Voiceover generated successfully.")
197
- status.update(label="Voiceover ready! ✨ All components generated.", state="complete", expanded=False)
198
- else:
199
- logger.warning("Voiceover failed or was skipped.")
200
- status.update(label="Voiceover failed/skipped. Storyboard ready.", state="complete", expanded=False) # Still complete the overall process
201
-
202
- except ValueError as ve:
203
- logger.error(f"ValueError during generation: {ve}")
204
- status.update(label=f"Input or Gemini response error: {ve}", state="error", expanded=True);
205
- except Exception as e:
206
- logger.error(f"Unhandled Exception during generation: {e}", exc_info=True)
207
- status.update(label=f"An unexpected error occurred: {e}", state="error", expanded=True);
208
-
209
- st.markdown("---") # Advanced Options & Voice Customization (UI remains the same as last full app.py)
210
- # ... (Character Consistency expander UI - same) ...
211
  with st.expander("Define Characters", expanded=False):
212
- char_name_input = st.text_input("Character Name", key="char_name_adv_input_ultra")
213
- char_desc_input = st.text_area("Detailed Visual Description", key="char_desc_adv_input_ultra", height=100, placeholder="e.g., Jax: rugged male astronaut...")
214
- if st.button("Save Character", key="add_char_adv_btn_ultra"):
215
- if char_name_input and char_desc_input: st.session_state.character_definitions[char_name_input.strip().lower()] = char_desc_input.strip(); st.success(f"Character '{char_name_input.strip()}' saved.")
216
- else: st.warning("Provide name and description.")
217
- if st.session_state.character_definitions:
218
- st.caption("Current Characters:")
219
- for k,v in st.session_state.character_definitions.items(): st.markdown(f"**{k.title()}:** _{v}_")
220
-
221
  with st.expander("Global Style Overrides", expanded=False):
222
- predefined_styles = { "Default (Director's Choice)": "", "Hyper-Realistic Gritty Noir": "hyper-realistic gritty neo-noir...", "Surreal Dreamscape Fantasy": "surreal dreamscape...", "Vintage Analog Sci-Fi": "70s/80s analog sci-fi..."}
223
- selected_preset = st.selectbox("Base Style Preset:", options=list(predefined_styles.keys()), key="style_preset_select_adv_ultra")
224
- custom_keywords = st.text_area("Additional Custom Style Keywords:", key="custom_style_keywords_adv_input_ultra", height=80, placeholder="e.g., 'Dutch angle'")
225
- current_style_desc = st.session_state.global_style_additions
226
- if st.button("Apply Global Styles", key="apply_styles_adv_btn_ultra"):
227
- final_desc = predefined_styles[selected_preset];
228
- if custom_keywords.strip(): final_desc = f"{final_desc}, {custom_keywords.strip()}" if final_desc else custom_keywords.strip()
229
- st.session_state.global_style_additions = final_desc.strip(); current_style_desc = final_desc.strip()
230
- if current_style_desc: st.success("Global styles applied!")
231
  else: st.info("Global style additions cleared.")
232
- if current_style_desc: st.caption(f"Active global style additions: \"{current_style_desc}\"")
233
-
234
  with st.expander("Voice Customization (ElevenLabs)", expanded=False):
235
- elevenlabs_voices_conceptual = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi", "Fin", "Sarah", "Charlie", "Clyde", "Dorothy", "George"]
236
- engine_voice_id = "Rachel"
237
- if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine: engine_voice_id = st.session_state.visual_engine.elevenlabs_voice_id
238
- try: current_voice_index = elevenlabs_voices_conceptual.index(engine_voice_id)
239
- except ValueError: current_voice_index = 0
240
- selected_el_voice = st.selectbox("Narrator Voice:", elevenlabs_voices_conceptual, index=current_voice_index, key="el_voice_select_ultra")
241
- voice_styles_for_prompt = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
242
- selected_prompt_voice_style_key = st.selectbox("Narration Script Style:", list(voice_styles_for_prompt.keys()), key="narration_style_select")
243
  if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra"):
244
- if hasattr(st.session_state, 'visual_engine'): st.session_state.visual_engine.elevenlabs_voice_id = selected_el_voice
245
- st.session_state.selected_voice_style_for_generation = voice_styles_for_prompt[selected_prompt_voice_style_key] # Store for next full generation
246
- st.success(f"Narrator voice set to: {selected_el_voice}. Script style: {selected_prompt_voice_style_key}")
247
-
248
 
249
  # --- Main Content Area ---
250
  st.header("🎬 Cinematic Storyboard & Treatment")
251
-
252
  if st.session_state.narration_script_display:
253
- with st.expander("πŸ“œ View Full Narration Script", expanded=False):
254
- st.markdown(f"> _{st.session_state.narration_script_display}_")
255
-
256
- if not st.session_state.story_treatment_scenes:
257
- st.info("Use the sidebar to generate your cinematic treatment.")
258
  else:
259
- for i_main, scene_content_display in enumerate(st.session_state.story_treatment_scenes):
260
- scene_num = scene_content_display.get('scene_number', i_main + 1)
261
- scene_title = scene_content_display.get('scene_title', 'Untitled Scene')
262
- unique_key_base = f"scene_{scene_num}_{''.join(filter(str.isalnum, scene_title[:10]))}"
263
-
264
- if "director_note" in scene_content_display and scene_content_display['director_note']:
265
- st.info(f"🎬 Director's Note for Scene {scene_num}: {scene_content_display['director_note']}")
266
-
267
- st.subheader(f"SCENE {scene_num}: {scene_title.upper()}")
268
- col_details, col_visual = st.columns([0.45, 0.55])
269
-
270
- with col_details:
271
- with st.expander("πŸ“ Scene Treatment Details", expanded=True):
272
- st.markdown(f"**Emotional Beat:** {scene_content_display.get('emotional_beat', 'N/A')}")
273
- st.markdown(f"**Setting:** {scene_content_display.get('setting_description', 'N/A')}")
274
- st.markdown(f"**Characters:** {', '.join(scene_content_display.get('characters_involved', ['N/A']))}")
275
- st.markdown(f"**Character Focus Moment:** _{scene_content_display.get('character_focus_moment', 'N/A')}_")
276
- st.markdown(f"**Key Plot Beat:** {scene_content_display.get('key_plot_beat', 'N/A')}")
277
- st.markdown(f"**Dialogue Hook:** `\"{scene_content_display.get('suggested_dialogue_hook', '...')}\"`")
278
- st.markdown("---")
279
- st.markdown(f"**🎬 Director's Visual Style:** _{scene_content_display.get('PROACTIVE_visual_style_감독', 'N/A')}_")
280
- st.markdown(f"**πŸŽ₯ Director's Camera Work:** _{scene_content_display.get('PROACTIVE_camera_work_감독', 'N/A')}_")
281
- st.markdown(f"**πŸ”Š Director's Sound Design:** _{scene_content_display.get('PROACTIVE_sound_design_감독', 'N/A')}_")
282
-
283
- current_dalle_prompt = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else None
284
- if current_dalle_prompt:
285
- with st.popover("πŸ‘οΈ View DALL-E Prompt"):
286
- st.markdown(f"**Full DALL-E Prompt:**"); st.code(current_dalle_prompt, language='text')
287
-
288
- pexels_query_display = scene_content_display.get('pexels_search_query_감독', None)
289
- if pexels_query_display:
290
- st.caption(f"Suggested Pexels Query for fallback: `{pexels_query_display}`")
291
-
292
- with col_visual: # Edit Popovers (logic for regeneration calls remain largely the same)
293
- current_img_path = st.session_state.generated_visual_paths[i_main] if i_main < len(st.session_state.generated_visual_paths) else None
294
- if current_img_path and os.path.exists(current_img_path):
295
- st.image(current_img_path, caption=f"Visual Concept for Scene {scene_num}: {scene_title}", use_column_width='always')
296
  else:
297
- if st.session_state.story_treatment_scenes: st.caption("Visual for this scene is pending or failed.")
298
 
299
- # Edit Scene Treatment Popover
300
- with st.popover(f"✏️ Edit Scene {scene_num} Treatment"):
301
- feedback_script_edit = st.text_area("Describe changes to treatment details:", key=f"treat_feed_{unique_key_base}", height=150)
302
- if st.button(f"πŸ”„ Update Scene {scene_num} Treatment", key=f"regen_treat_btn_{unique_key_base}"):
303
- if feedback_script_edit:
304
- with st.status(f"Updating Scene {scene_num} Treatment...", expanded=True) as status_treat_regen:
305
- regen_prompt_text = create_scene_regeneration_prompt(scene_content_display, feedback_script_edit, st.session_state.story_treatment_scenes)
306
  try:
307
- updated_scene_data = st.session_state.gemini_handler.regenerate_scene_script_details(regen_prompt_text)
308
- st.session_state.story_treatment_scenes[i_main] = updated_scene_data
309
- status_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
310
- version_num = 1
311
- if current_img_path: try: base,_=os.path.splitext(os.path.basename(current_img_path)); version_num = int(base.split('_v')[-1])+1 if '_v' in base else 2 except: version_num=2
312
- if generate_visual_for_scene_core(i_main, updated_scene_data, version=version_num): status_treat_regen.update(label="Treatment & Visual Updated! πŸŽ‰", state="complete", expanded=False)
313
- else: status_treat_regen.update(label="Treatment updated, visual failed.", state="warning", expanded=False)
314
- st.rerun() # Rerun to refresh the whole UI with new data
315
- except Exception as e: status_treat_regen.update(label=f"Error: {e}", state="error")
316
- else: st.warning("Please provide feedback for treatment regeneration.")
 
 
 
 
317
 
318
- # Edit Visual Prompt Popover
319
- with st.popover(f"🎨 Edit Scene {scene_num} Visual Prompt"):
320
- dalle_prompt_to_edit = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else "No DALL-E prompt."
321
- st.caption("Current DALL-E Prompt:"); st.code(dalle_prompt_to_edit, language='text')
322
- feedback_visual_edit = st.text_area("Describe changes for the DALL-E prompt:", key=f"visual_feed_{unique_key_base}", height=150)
323
- if st.button(f"πŸ”„ Update Scene {scene_num} Visual Prompt & Image", key=f"regen_visual_btn_{unique_key_base}"):
324
- if feedback_visual_edit:
325
- with st.status(f"Refining DALL-E prompt & regenerating visual...", expanded=True) as status_visual_edit_regen:
326
- refinement_req_prompt = create_visual_regeneration_prompt(
327
- dalle_prompt_to_edit, feedback_visual_edit, scene_content_display,
328
- st.session_state.character_definitions, st.session_state.global_style_additions
329
- )
330
  try:
331
- refined_dalle_prompt = st.session_state.gemini_handler.generate_image_prompt(refinement_req_prompt)
332
- st.session_state.scene_dalle_prompts[i_main] = refined_dalle_prompt
333
- status_visual_edit_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
334
- version_num = 1
335
- if current_img_path: try: base,_=os.path.splitext(os.path.basename(current_img_path)); version_num = int(base.split('_v')[-1])+1 if '_v' in base else 2 except: version_num=2
336
- if generate_visual_for_scene_core(i_main, scene_content_display, version=version_num):
337
- status_visual_edit_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
338
- else: status_visual_edit_regen.update(label="Prompt refined, visual failed.", state="warning", expanded=False)
 
 
 
339
  st.rerun()
340
- except Exception as e: status_visual_edit_regen.update(label=f"Error: {e}", state="error")
341
- else: st.warning("Please provide feedback for visual prompt regeneration.")
342
  st.markdown("---")
343
 
344
- # Video Generation Button
345
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
346
  if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn", type="primary", use_container_width=True):
347
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
348
- image_data_for_vid = []
349
- for i_vid, scene_c in enumerate(st.session_state.story_treatment_scenes):
350
- img_p = st.session_state.generated_visual_paths[i_vid] if i_vid < len(st.session_state.generated_visual_paths) else None
351
- if img_p and os.path.exists(img_p):
352
- image_data_for_vid.append({
353
- 'path':img_p, 'scene_num':scene_c.get('scene_number',i_vid+1),
354
- 'key_action':scene_c.get('key_plot_beat','') # Using key_plot_beat for overlay now
355
- }); status_vid.write(f"Adding Scene {scene_c.get('scene_number', i_vid + 1)} to video.")
356
-
357
- if image_data_for_vid:
358
- status_vid.write("Calling video engine (with narration if available)...")
359
- st.session_state.video_path = st.session_state.visual_engine.create_video_from_images(
360
- image_data_for_vid,
361
- overall_narration_path=st.session_state.overall_narration_audio_path,
362
- output_filename="cinegen_ultra_animatic.mp4",
363
- duration_per_image=5, # Allow more time for narration per scene
364
- fps=24
365
- )
366
- if st.session_state.video_path and os.path.exists(st.session_state.video_path):
367
- status_vid.update(label="Ultra animatic assembled! πŸŽ‰", state="complete", expanded=False); st.balloons()
368
- else: status_vid.update(label="Video assembly failed. Check application logs.", state="error", expanded=False)
369
  else: status_vid.update(label="No valid images for video.", state="error", expanded=False)
370
  elif st.session_state.story_treatment_scenes: st.info("Generate visuals before assembling video.")
371
 
372
- # Video display and download
373
  if st.session_state.video_path and os.path.exists(st.session_state.video_path):
374
- st.header("🎬 Generated Cinematic Animatic")
375
  try:
376
  with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
377
  st.video(video_bytes, format="video/mp4")
378
  with open(st.session_state.video_path, "rb") as fp_dl:
379
- st.download_button(label="Download Ultra Animatic", data=fp_dl,
380
- file_name=os.path.basename(st.session_state.video_path), mime="video/mp4",
381
- use_container_width=True, key="download_ultra_video_btn" )
382
  except Exception as e: st.error(f"Error displaying video: {e}")
383
 
384
  # --- Footer ---
385
- st.sidebar.markdown("---")
386
- st.sidebar.caption("CineGen AI Ultra+ | Visionary Cinematic Pre-Production")
 
3
  from core.gemini_handler import GeminiHandler
4
  from core.visual_engine import VisualEngine
5
  from core.prompt_engineering import (
6
+ create_cinematic_treatment_prompt,
7
  construct_dalle_prompt,
8
  create_narration_script_prompt_enhanced,
9
  create_scene_regeneration_prompt,
10
  create_visual_regeneration_prompt
11
  )
12
  import os
13
+ import logging
14
 
15
  # --- Configuration & Initialization ---
16
  st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
 
 
17
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
18
  logger = logging.getLogger(__name__)
19
 
20
  # --- Global State Variables & API Key Setup ---
21
  def load_api_key(key_name_streamlit, key_name_env, service_name):
22
+ key = None; secrets_available = hasattr(st, 'secrets')
 
23
  try:
24
  if secrets_available and key_name_streamlit in st.secrets:
25
  key = st.secrets[key_name_streamlit]
26
  if key: logger.info(f"{service_name} API Key loaded from Streamlit secrets.")
27
+ except Exception as e: logger.warning(f"Could not access st.secrets for {key_name_streamlit}: {e}")
 
 
28
  if not key and key_name_env in os.environ:
29
  key = os.environ[key_name_env]
30
  if key: logger.info(f"{service_name} API Key loaded from environment variable.")
31
+ if not key: logger.warning(f"{service_name} API Key NOT FOUND.")
 
 
32
  return key
33
 
 
34
  if 'services_initialized' not in st.session_state:
35
+ logger.info("Initializing services and API keys...")
36
  st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY", "Gemini")
37
  st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
38
  st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
39
  st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
40
+ if not st.session_state.GEMINI_API_KEY: st.error("CRITICAL: Gemini API Key missing!"); st.stop()
 
 
 
 
 
41
  try:
42
  st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
43
+ logger.info("GeminiHandler initialized.")
44
+ except Exception as e: st.error(f"Failed to init GeminiHandler: {e}"); logger.error(f"GeminiHandler init failed: {e}"); st.stop()
 
 
 
 
45
  try:
46
  st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
47
  st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
 
49
  st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
50
  logger.info("VisualEngine initialized and API keys set.")
51
  except Exception as e:
52
+ st.error(f"Failed to init VisualEngine or set keys: {e}"); logger.error(f"VisualEngine init/key setting failed: {e}")
53
+ st.warning("VisualEngine issue. Features might use placeholders/be disabled.")
54
+ st.session_state.services_initialized = True; logger.info("Service initialization complete.")
 
55
 
 
 
 
 
56
  for key, default_val in [
57
  ('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
58
  ('video_path', None), ('character_definitions', {}), ('global_style_additions', ""),
59
  ('overall_narration_audio_path', None), ('narration_script_display', "")
60
  ]:
61
  if key not in st.session_state: st.session_state[key] = default_val
 
62
 
63
+ # --- Helper Functions --- (initialize_new_project, generate_visual_for_scene_core - same as previous)
64
  def initialize_new_project():
65
+ st.session_state.story_treatment_scenes, st.session_state.scene_dalle_prompts, st.session_state.generated_visual_paths = [], [], []
66
+ st.session_state.video_path, st.session_state.overall_narration_audio_path, st.session_state.narration_script_display = None, None, ""
67
+ logger.info("New project initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  def generate_visual_for_scene_core(scene_index, scene_data, version=1):
70
+ dalle_prompt = construct_dalle_prompt(scene_data, st.session_state.character_definitions, st.session_state.global_style_additions)
71
+ if not dalle_prompt: logger.error(f"DALL-E prompt construction failed for scene {scene_data.get('scene_number', scene_index+1)}"); return False
 
 
 
 
 
 
 
 
 
72
  while len(st.session_state.scene_dalle_prompts) <= scene_index: st.session_state.scene_dalle_prompts.append("")
73
  while len(st.session_state.generated_visual_paths) <= scene_index: st.session_state.generated_visual_paths.append(None)
74
  st.session_state.scene_dalle_prompts[scene_index] = dalle_prompt
75
+ filename = f"scene_{scene_data.get('scene_number', scene_index+1)}_visual_v{version}.png"
 
 
76
  img_path = st.session_state.visual_engine.generate_image_visual(dalle_prompt, scene_data, filename)
 
77
  if img_path and os.path.exists(img_path):
78
+ st.session_state.generated_visual_paths[scene_index] = img_path; logger.info(f"Visual generated for Scene {scene_data.get('scene_number', scene_index+1)}: {img_path}"); return True
 
 
79
  else:
80
+ st.session_state.generated_visual_paths[scene_index] = None; logger.warning(f"Visual generation FAILED for Scene {scene_data.get('scene_number', scene_index+1)}. img_path: {img_path}"); return False
 
 
81
 
82
+ # --- UI Sidebar --- (same as previous full app.py)
83
  with st.sidebar:
84
+ st.title("🎬 CineGen AI Ultra+"); st.markdown("### Creative Seed")
 
85
  user_idea = st.text_area("Core Story Idea / Theme:", "A lone wanderer searches for a mythical oasis in a vast, post-apocalyptic desert, haunted by mirages and mechanical scavengers.", height=120, key="user_idea_main")
86
  genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
87
  mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main")
88
+ num_scenes = st.slider("Number of Key Scenes:", 1, 3, 1, key="num_scenes_main")
 
89
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
90
  selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
91
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
 
92
  if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
93
  initialize_new_project()
94
  if not user_idea.strip(): st.warning("Please provide a story idea.")
95
  else:
96
  with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status:
97
  try:
98
+ status.write("Phase 1: Gemini crafting cinematic treatment... πŸ“œ"); logger.info("Phase 1: Cinematic Treatment Gen.")
 
99
  treatment_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance)
100
  treatment_result_json = st.session_state.gemini_handler.generate_story_breakdown(treatment_prompt)
101
+ if not isinstance(treatment_result_json, list) or not treatment_result_json: raise ValueError("Gemini returned invalid scene list.")
102
+ st.session_state.story_treatment_scenes = treatment_result_json; num_gen_scenes = len(st.session_state.story_treatment_scenes)
103
+ st.session_state.scene_dalle_prompts = [""]*num_gen_scenes; st.session_state.generated_visual_paths = [None]*num_gen_scenes
104
+ logger.info(f"Phase 1 complete. {num_gen_scenes} scenes.")
 
 
 
105
  status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
106
+ status.write("Phase 2: Creating visuals (DALL-E/Pexels)... πŸ–ΌοΈ"); logger.info("Phase 2: Visual Gen.")
 
 
107
  visual_successes = 0
108
+ for i, sc in enumerate(st.session_state.story_treatment_scenes):
109
+ status.write(f" Visual for Scene {sc.get('scene_number', i+1)}: {sc.get('scene_title','Untitled')}..."); logger.info(f" Processing visual for Scene {sc.get('scene_number', i+1)}.")
110
+ if generate_visual_for_scene_core(i, sc, version=1): visual_successes += 1
111
+ if visual_successes==0 and num_gen_scenes>0: logger.error("Visual gen failed all scenes."); status.update(label="Visual gen failed. Check logs & API.", state="error", expanded=True); st.stop()
112
+ elif visual_successes < num_gen_scenes: logger.warning(f"Visuals partial ({visual_successes}/{num_gen_scenes})."); status.update(label=f"Visuals ready ({visual_successes}/{num_gen_scenes}). Generating narration...", state="running")
113
+ else: logger.info("All visuals OK."); status.update(label="Visuals ready! Generating narration script...", state="running")
114
+ status.write("Phase 3: Generating narration script... 🎀"); logger.info("Phase 3: Narration Script Gen.")
115
+ voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer")
116
+ narr_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, voice_style)
117
+ st.session_state.narration_script_display = st.session_state.gemini_handler.generate_image_prompt(narr_prompt)
118
+ logger.info("Narration script generated."); status.update(label="Narration script ready! Synthesizing voice...", state="running")
119
+ status.write("Phase 4: Synthesizing voice (ElevenLabs)... πŸ”Š"); logger.info("Phase 4: Voice Synthesis.")
120
+ st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(st.session_state.narration_script_display)
121
+ if st.session_state.overall_narration_audio_path: logger.info("Voiceover OK."); status.update(label="Voiceover ready! ✨", state="running")
122
+ else: logger.warning("Voiceover failed/skipped."); status.update(label="Voiceover failed/skipped.", state="warning")
123
+ status.update(label="All components ready! View storyboard. πŸš€", state="complete", expanded=False)
124
+ except ValueError as ve: logger.error(f"ValueError: {ve}"); status.update(label=f"Input/Gemini response error: {ve}", state="error", expanded=True);
125
+ except Exception as e: logger.error(f"Unhandled Exception: {e}", exc_info=True); status.update(label=f"Error: {e}", state="error", expanded=True);
126
+ st.markdown("---"); st.markdown("### Fine-Tuning Options") # Advanced Options (same UI as previous)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  with st.expander("Define Characters", expanded=False):
128
+ char_name = st.text_input("Character Name", key="char_name_adv_ultra"); char_desc = st.text_area("Visual Description", key="char_desc_adv_ultra", height=100, placeholder="e.g., Jax: rugged male astronaut...")
129
+ if st.button("Save Character", key="add_char_adv_ultra"):
130
+ if char_name and char_desc: st.session_state.character_definitions[char_name.strip().lower()] = char_desc.strip(); st.success(f"Char '{char_name.strip()}' saved.")
131
+ else: st.warning("Name and description needed.")
132
+ if st.session_state.character_definitions: st.caption("Current Characters:"); [st.markdown(f"**{k.title()}:** _{v}_") for k,v in st.session_state.character_definitions.items()]
 
 
 
 
133
  with st.expander("Global Style Overrides", expanded=False):
134
+ presets = {"Default": "", "Gritty Noir": "gritty neo-noir...", "Epic Fantasy": "epic fantasy matte...", "Vintage Sci-Fi": "70s analog sci-fi..."}
135
+ sel_preset = st.selectbox("Base Style Preset:", options=list(presets.keys()), key="style_preset_adv_ultra")
136
+ custom_kw = st.text_area("Additional Custom Style Keywords:", key="custom_style_adv_ultra", height=80, placeholder="e.g., 'Dutch angle'")
137
+ cur_style = st.session_state.global_style_additions
138
+ if st.button("Apply Global Styles", key="apply_styles_adv_ultra"):
139
+ final_s = presets[sel_preset];
140
+ if custom_kw.strip(): final_s = f"{final_s}, {custom_kw.strip()}" if final_s else custom_kw.strip()
141
+ st.session_state.global_style_additions = final_s.strip(); cur_style = final_s.strip()
142
+ if cur_style: st.success("Global styles applied!")
143
  else: st.info("Global style additions cleared.")
144
+ if cur_style: st.caption(f"Active global styles: \"{cur_style}\"")
 
145
  with st.expander("Voice Customization (ElevenLabs)", expanded=False):
146
+ el_voices = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi"]
147
+ engine_v_id = "Rachel";
148
+ if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine: engine_v_id = st.session_state.visual_engine.elevenlabs_voice_id
149
+ try: cur_v_idx = el_voices.index(engine_v_id)
150
+ except ValueError: cur_v_idx = 0
151
+ sel_el_voice = st.selectbox("Narrator Voice:", el_voices, index=cur_v_idx, key="el_voice_sel_ultra")
152
+ prompt_v_styles = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
153
+ sel_prompt_v_style_key = st.selectbox("Narration Script Style:", list(prompt_v_styles.keys()), key="narr_style_sel")
154
  if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra"):
155
+ if hasattr(st.session_state, 'visual_engine'): st.session_state.visual_engine.elevenlabs_voice_id = sel_el_voice
156
+ st.session_state.selected_voice_style_for_generation = prompt_v_styles[sel_prompt_v_style_key]
157
+ st.success(f"Narrator: {sel_el_voice}. Script Style: {sel_prompt_v_style_key}")
 
158
 
159
  # --- Main Content Area ---
160
  st.header("🎬 Cinematic Storyboard & Treatment")
 
161
  if st.session_state.narration_script_display:
162
+ with st.expander("πŸ“œ View Full Narration Script", expanded=False): st.markdown(f"> _{st.session_state.narration_script_display}_")
163
+ if not st.session_state.story_treatment_scenes: st.info("Use sidebar to generate cinematic treatment.")
 
 
 
164
  else:
165
+ for i, scene_content in enumerate(st.session_state.story_treatment_scenes):
166
+ scene_n = scene_content.get('scene_number', i + 1); scene_t = scene_content.get('scene_title', 'Untitled')
167
+ key_base = f"s{scene_n}_{''.join(filter(str.isalnum, scene_t[:10]))}"
168
+ if "director_note" in scene_content and scene_content['director_note']: st.info(f"🎬 Director Note S{scene_n}: {scene_content['director_note']}")
169
+ st.subheader(f"SCENE {scene_n}: {scene_t.upper()}"); col_d, col_v = st.columns([0.45, 0.55])
170
+ with col_d:
171
+ with st.expander("πŸ“ Scene Treatment", expanded=True):
172
+ st.markdown(f"**Beat:** {scene_content.get('emotional_beat', 'N/A')}")
173
+ st.markdown(f"**Setting:** {scene_content.get('setting_description', 'N/A')}")
174
+ st.markdown(f"**Chars:** {', '.join(scene_content.get('characters_involved', ['N/A']))}")
175
+ st.markdown(f"**Focus Moment:** _{scene_content.get('character_focus_moment', 'N/A')}_")
176
+ st.markdown(f"**Plot Beat:** {scene_content.get('key_plot_beat', 'N/A')}")
177
+ st.markdown(f"**Dialogue Hook:** `\"{scene_content.get('suggested_dialogue_hook', '...')}\"`")
178
+ st.markdown("---"); st.markdown(f"**Dir. Visual Style:** _{scene_content.get('PROACTIVE_visual_style_감독', 'N/A')}_")
179
+ st.markdown(f"**Dir. Camera:** _{scene_content.get('PROACTIVE_camera_work_감독', 'N/A')}_")
180
+ st.markdown(f"**Dir. Sound:** _{scene_content.get('PROACTIVE_sound_design_감독', 'N/A')}_")
181
+ cur_d_prompt = st.session_state.scene_dalle_prompts[i] if i < len(st.session_state.scene_dalle_prompts) else None
182
+ if cur_d_prompt:
183
+ with st.popover("πŸ‘οΈ DALL-E Prompt"): st.markdown(f"**DALL-E Prompt:**"); st.code(cur_d_prompt, language='text')
184
+ pexels_q = scene_content.get('pexels_search_query_감독', None)
185
+ if pexels_q: st.caption(f"Pexels Fallback Query: `{pexels_q}`")
186
+ with col_v:
187
+ cur_img_p = st.session_state.generated_visual_paths[i] if i < len(st.session_state.generated_visual_paths) else None
188
+ if cur_img_p and os.path.exists(cur_img_p): st.image(cur_img_p, caption=f"Scene {scene_n}: {scene_t}", use_column_width='always')
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  else:
190
+ if st.session_state.story_treatment_scenes: st.caption("Visual pending/failed.")
191
 
192
+ with st.popover(f"✏️ Edit Scene {scene_n} Treatment"):
193
+ fb_script = st.text_area("Changes to treatment:", key=f"treat_fb_{key_base}", height=150)
194
+ if st.button(f"πŸ”„ Update Scene {scene_n} Treatment", key=f"regen_treat_btn_{key_base}"):
195
+ if fb_script:
196
+ with st.status(f"Updating Scene {scene_n}...", expanded=True) as s_treat_regen:
197
+ prompt_text = create_scene_regeneration_prompt(scene_content, fb_script, st.session_state.story_treatment_scenes)
 
198
  try:
199
+ updated_sc_data = st.session_state.gemini_handler.regenerate_scene_script_details(prompt_text)
200
+ st.session_state.story_treatment_scenes[i] = updated_sc_data
201
+ s_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
202
+ v_num = 1
203
+ if cur_img_p:
204
+ try: b,_=os.path.splitext(os.path.basename(cur_img_p)); v_num = int(b.split('_v')[-1])+1 if '_v' in b else 2
205
+ except: v_num = 2 # Default to v2 if parsing fails
206
+ else: v_num = 1 # First gen for this scene visual
207
+
208
+ if generate_visual_for_scene_core(i, updated_sc_data, version=v_num): s_treat_regen.update(label="Treatment & Visual Updated! πŸŽ‰", state="complete", expanded=False)
209
+ else: s_treat_regen.update(label="Treatment updated, visual failed.", state="warning", expanded=False)
210
+ st.rerun()
211
+ except Exception as e_regen: s_treat_regen.update(label=f"Error: {e_regen}", state="error")
212
+ else: st.warning("Please provide feedback.")
213
 
214
+ with st.popover(f"🎨 Edit Scene {scene_n} Visual Prompt"):
215
+ d_prompt_edit = st.session_state.scene_dalle_prompts[i] if i < len(st.session_state.scene_dalle_prompts) else "No DALL-E prompt."
216
+ st.caption("Current DALL-E Prompt:"); st.code(d_prompt_edit, language='text')
217
+ fb_visual = st.text_area("Changes for DALL-E prompt:", key=f"visual_fb_{key_base}", height=150)
218
+ if st.button(f"πŸ”„ Update Scene {scene_n} Visual", key=f"regen_visual_btn_{key_base}"):
219
+ if fb_visual:
220
+ with st.status(f"Refining prompt & visual for Scene {scene_n}...", expanded=True) as s_visual_regen:
221
+ ref_req_prompt = create_visual_regeneration_prompt(d_prompt_edit, fb_visual, scene_content,
222
+ st.session_state.character_definitions, st.session_state.global_style_additions)
 
 
 
223
  try:
224
+ refined_d_prompt = st.session_state.gemini_handler.generate_image_prompt(ref_req_prompt)
225
+ st.session_state.scene_dalle_prompts[i] = refined_d_prompt
226
+ s_visual_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
227
+ v_num = 1
228
+ if cur_img_p:
229
+ try: b,_=os.path.splitext(os.path.basename(cur_img_p)); v_num = int(b.split('_v')[-1])+1 if '_v' in b else 2
230
+ except: v_num=2
231
+ else: v_num = 1
232
+
233
+ if generate_visual_for_scene_core(i, scene_content, version=v_num): s_visual_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
234
+ else: s_visual_regen.update(label="Prompt refined, visual failed.", state="warning", expanded=False)
235
  st.rerun()
236
+ except Exception as e_regen_vis: s_visual_regen.update(label=f"Error: {e_regen_vis}", state="error")
237
+ else: st.warning("Please provide feedback.")
238
  st.markdown("---")
239
 
 
240
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
241
  if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn", type="primary", use_container_width=True):
242
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
243
+ img_data_vid = []
244
+ for i_v, sc_c in enumerate(st.session_state.story_treatment_scenes):
245
+ img_p_v = st.session_state.generated_visual_paths[i_v] if i_v < len(st.session_state.generated_visual_paths) else None
246
+ if img_p_v and os.path.exists(img_p_v):
247
+ img_data_vid.append({'path':img_p_v, 'scene_num':sc_c.get('scene_number',i_v+1), 'key_action':sc_c.get('key_plot_beat','')}); status_vid.write(f"Adding Scene {sc_c.get('scene_number', i_v+1)}.")
248
+ if img_data_vid:
249
+ status_vid.write("Calling video engine..."); st.session_state.video_path = st.session_state.visual_engine.create_video_from_images(
250
+ img_data_vid, overall_narration_path=st.session_state.overall_narration_audio_path,
251
+ output_filename="cinegen_ultra_animatic.mp4", duration_per_image=5, fps=24)
252
+ if st.session_state.video_path and os.path.exists(st.session_state.video_path): status_vid.update(label="Ultra animatic assembled! πŸŽ‰", state="complete", expanded=False); st.balloons()
253
+ else: status_vid.update(label="Video assembly failed. Check logs.", state="error", expanded=False)
 
 
 
 
 
 
 
 
 
 
254
  else: status_vid.update(label="No valid images for video.", state="error", expanded=False)
255
  elif st.session_state.story_treatment_scenes: st.info("Generate visuals before assembling video.")
256
 
 
257
  if st.session_state.video_path and os.path.exists(st.session_state.video_path):
258
+ st.header("🎬 Generated Cinematic Animatic");
259
  try:
260
  with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
261
  st.video(video_bytes, format="video/mp4")
262
  with open(st.session_state.video_path, "rb") as fp_dl:
263
+ st.download_button(label="Download Ultra Animatic", data=fp_dl, file_name=os.path.basename(st.session_state.video_path), mime="video/mp4", use_container_width=True, key="download_ultra_video_btn" )
 
 
264
  except Exception as e: st.error(f"Error displaying video: {e}")
265
 
266
  # --- Footer ---
267
+ st.sidebar.markdown("---"); st.sidebar.caption("CineGen AI Ultra+ | Visionary Cinematic Pre-Production")