File size: 41,333 Bytes
a48cea9
 
 
 
 
 
 
 
 
 
 
 
de2fdbb
857e0f9
de2fdbb
a48cea9
 
 
 
 
de2fdbb
a48cea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20b484
a48cea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a6537e
a48cea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7374a3
a48cea9
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# app.py
import streamlit as st
from core.gemini_handler import GeminiHandler
from core.visual_engine import VisualEngine
from core.prompt_engineering import (
    create_cinematic_treatment_prompt,
    construct_dalle_prompt,
    construct_text_to_video_prompt_for_gen4, # <<< USE THIS FOR RUNWAY
    create_narration_script_prompt_enhanced,
    create_scene_regeneration_prompt,
    create_visual_regeneration_prompt # This is for DALL-E image prompt refinement
)
import os
import logging

# --- Configuration & Initialization ---
st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
# Configure logging to be more verbose for debugging if needed
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__) # Get logger for this module

# --- Global Definitions ---
SHOT_TYPES_OPTIONS = [ "Director's Choice", "Establishing Shot", "Long Shot", "Full Shot", "Medium Long Shot (Cowboy)", "Medium Shot", "Medium Close-up", "Close-up", "Extreme Close-up", "Point of View (POV)", "Over the Shoulder", "Tracking Shot", "Dolly Zoom", "Crane Shot", "Aerial Shot", "Static Shot", "Dutch Angle", "Whip Pan"]
DEFAULT_SCENE_DURATION_SECS = 5
DEFAULT_SHOT_TYPE = "Director's Choice"
ASSET_TYPE_OPTIONS = ["Auto (Director's Choice)", "Image", "Video Clip"]


# --- Global State Variables & API Key Setup ---
def load_api_key(key_name_streamlit, key_name_env, service_name):
    # (Keep this function as it was - robust)
    key = None; secrets_available = hasattr(st, 'secrets')
    try:
        if secrets_available and key_name_streamlit in st.secrets:
            key = st.secrets[key_name_streamlit]
            if key: logger.info(f"{service_name} API Key found in Streamlit secrets.")
    except Exception as e: logger.warning(f"Could not access st.secrets for {key_name_streamlit}: {e}")
    if not key and key_name_env in os.environ:
        key = os.environ[key_name_env]
        if key: logger.info(f"{service_name} API Key found in environment variable.")
    if not key: logger.warning(f"{service_name} API Key NOT FOUND. Related features may be disabled or use fallbacks.")
    return key

if 'services_initialized' not in st.session_state:
    logger.info("Initializing services and API keys for the first time this session...")
    st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY", "Gemini")
    st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
    st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
    st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
    st.session_state.ELEVENLABS_VOICE_ID_CONFIG = load_api_key("ELEVENLABS_VOICE_ID", "ELEVENLABS_VOICE_ID", "ElevenLabs Voice ID")
    st.session_state.RUNWAY_API_KEY = load_api_key("RUNWAY_API_KEY", "RUNWAY_API_KEY", "RunwayML")

    if not st.session_state.GEMINI_API_KEY: st.error("CRITICAL: Gemini API Key missing!"); logger.critical("Gemini API Key missing."); st.stop()
    
    try: st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY); logger.info("GeminiHandler initialized.")
    except Exception as e: st.error(f"Failed to init GeminiHandler: {e}"); logger.critical(f"GeminiHandler init failed: {e}", exc_info=True); st.stop()
    
    try:
        default_voice_id_el = "Rachel" # Fallback
        configured_voice_id_el = st.session_state.ELEVENLABS_VOICE_ID_CONFIG or default_voice_id_el
        st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media", default_elevenlabs_voice_id=configured_voice_id_el)
        st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
        st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY, voice_id_from_secret=st.session_state.ELEVENLABS_VOICE_ID_CONFIG)
        st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
        st.session_state.visual_engine.set_runway_api_key(st.session_state.RUNWAY_API_KEY) # Pass Runway key
        logger.info("VisualEngine initialized and API keys set.")
    except Exception as e: st.error(f"Failed to init VisualEngine: {e}"); logger.critical(f"VisualEngine init/key setting failed: {e}", exc_info=True); st.warning("VisualEngine critical setup issue.")
    st.session_state.services_initialized = True; logger.info("Service initialization complete.")

# Initialize other session state variables
for key_ss, default_val_ss in [ # Renamed loop vars
    ('story_treatment_scenes', []), ('scene_generation_prompts', []), ('generated_scene_assets_info', []), # Stores full asset info dicts
    ('video_path', None), ('character_definitions', {}), ('global_style_additions', ""),
    ('overall_narration_audio_path', None), ('narration_script_display', "")
]:
    if key_ss not in st.session_state: st.session_state[key_ss] = default_val_ss

def initialize_new_project_state(): # Renamed
    st.session_state.story_treatment_scenes = []
    st.session_state.scene_generation_prompts = [] # Stores the prompt used for DALL-E or Runway
    st.session_state.generated_scene_assets_info = [] # Stores dicts {'path':..., 'type':..., 'error':..., 'prompt_used':...}
    st.session_state.video_path, st.session_state.overall_narration_audio_path, st.session_state.narration_script_display = None, None, ""
    logger.info("New project state initialized.")

def generate_asset_for_scene_wrapper(scene_idx, scene_dict_data, version_num=1, user_selected_asset_type_override="Auto (Director's Choice)"): # Renamed
    # Determine if video clip is desired based on user override or Gemini's suggestion
    generate_as_video_clip_final = False
    gemini_suggested_asset_type = scene_dict_data.get('suggested_asset_type_감독', 'image').lower()

    if user_selected_asset_type_override == "Image":
        generate_as_video_clip_final = False
    elif user_selected_asset_type_override == "Video Clip":
        generate_as_video_clip_final = True
    elif user_selected_asset_type_override == "Auto (Director's Choice)": # Default
        generate_as_video_clip_final = (gemini_suggested_asset_type == "video_clip")
    
    # Prompt for base image generation (DALL-E or Pexels fallback)
    image_gen_prompt_text = construct_dalle_prompt(scene_dict_data, st.session_state.character_definitions, st.session_state.global_style_additions)
    
    # Prompt for video motion (Runway Gen-4) - only if generating video
    motion_gen_prompt_text = ""
    if generate_as_video_clip_final:
        motion_gen_prompt_text = construct_text_to_video_prompt_for_gen4(scene_dict_data, st.session_state.global_style_additions)
        if not motion_gen_prompt_text: # Fallback if specific motion prompt is empty
            logger.warning(f"S{scene_dict_data.get('scene_number', scene_idx+1)}: Motion prompt empty, using generic for Runway.")
            motion_gen_prompt_text = scene_dict_data.get('video_clip_motion_description_감독', "subtle ambient motion")


    if not image_gen_prompt_text: # Base image prompt is always needed
        logger.error(f"Base image prompt construction failed for S{scene_dict_data.get('scene_number', scene_idx+1)}"); return False

    # Ensure session state lists are adequate
    while len(st.session_state.scene_generation_prompts) <= scene_idx: st.session_state.scene_generation_prompts.append("")
    while len(st.session_state.generated_scene_assets_info) <= scene_idx: st.session_state.generated_scene_assets_info.append(None)

    # Store the relevant prompt (DALL-E for image, motion for video)
    # The generate_scene_asset method will return the actual prompt it used if different internally.
    st.session_state.scene_generation_prompts[scene_idx] = motion_gen_prompt_text if generate_as_video_clip_final else image_gen_prompt_text
    
    filename_base_for_asset = f"scene_{scene_dict_data.get('scene_number', scene_idx+1)}_asset_v{version_num}" # Renamed
    runway_dur_for_scene = scene_dict_data.get('video_clip_duration_estimate_secs_감독', scene_dict_data.get('user_scene_duration_secs', DEFAULT_SCENE_DURATION_SECS))
    if runway_dur_for_scene <= 0 : runway_dur_for_scene = DEFAULT_SCENE_DURATION_SECS

    asset_result_dict = st.session_state.visual_engine.generate_scene_asset(
        image_generation_prompt_text=image_gen_prompt_text, # For base DALL-E/Pexels image
        motion_prompt_text_for_video=motion_gen_prompt_text, # For Runway motion
        scene_data=scene_dict_data,
        scene_identifier_filename_base=filename_base_for_asset,
        generate_as_video_clip=generate_as_video_clip_final,
        runway_target_duration=runway_dur_for_scene
    )
    
    st.session_state.generated_scene_assets_info[scene_idx] = asset_result_dict
    # Update the stored prompt with what was actually used by the engine, if available from result
    if asset_result_dict and asset_result_dict.get('prompt_used'):
        st.session_state.scene_generation_prompts[scene_idx] = asset_result_dict['prompt_used']


    if asset_result_dict and not asset_result_dict['error'] and asset_result_dict.get('path') and os.path.exists(asset_result_dict['path']):
        logger.info(f"Asset ({asset_result_dict.get('type')}) generated for S{scene_dict_data.get('scene_number', scene_idx+1)}: {os.path.basename(asset_result_dict['path'])}")
        return True
    else:
        err_msg_asset = asset_result_dict.get('error_message', 'Unknown error') if asset_result_dict else 'Asset result is None'
        logger.warning(f"Asset gen FAILED for S{scene_dict_data.get('scene_number', scene_idx+1)}. Type attempted: {'Video' if generate_as_video_clip_final else 'Image'}. Error: {err_msg_asset}")
        # Store a more detailed failure state if not already
        if not st.session_state.generated_scene_assets_info[scene_idx] or not st.session_state.generated_scene_assets_info[scene_idx]['error']:
             st.session_state.generated_scene_assets_info[scene_idx] = {'path': None, 'type': 'none', 'error': True, 'error_message': err_msg_asset, 'prompt_used': st.session_state.scene_generation_prompts[scene_idx]}
        return False

# --- UI Sidebar ---
with st.sidebar:
    # ... (Sidebar UI code as before, no changes needed for this fix) ...
    st.title("🎬 CineGen AI Ultra+")
    st.markdown("### Creative Seed")
    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_v5")
    genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main_v5")
    mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main_v5")
    num_scenes = st.slider("Number of Key Scenes:", 1, 10, 2, key="num_scenes_main_v5")
    creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
    selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select_v5")
    actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]

    if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn_v5", use_container_width=True):
        initialize_new_project_state() # Use renamed function
        if not user_idea.strip(): st.warning("Please provide a story idea.")
        else:
            with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status_op: # Renamed
                try:
                    status_op.write("Phase 1: Gemini crafting cinematic treatment... πŸ“œ"); logger.info("Phase 1: Cinematic Treatment Gen.")
                    treatment_gen_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance) # Renamed
                    raw_treatment_result = st.session_state.gemini_handler.generate_story_breakdown(treatment_gen_prompt) # Renamed
                    if not isinstance(raw_treatment_result, list) or not raw_treatment_result: raise ValueError("Gemini returned invalid scene list format.")

                    processed_scene_list = [] # Renamed
                    for scene_from_gemini in raw_treatment_result: # Renamed
                        scene_from_gemini['user_shot_type'] = scene_from_gemini.get('PROACTIVE_camera_work_감독', DEFAULT_SHOT_TYPE)
                        # Use Gemini's video duration estimate if available for video clips, else default scene duration
                        gemini_dur_est = scene_from_gemini.get('video_clip_duration_estimate_secs_감독', 0)
                        scene_from_gemini['user_scene_duration_secs'] = gemini_dur_est if gemini_dur_est > 0 else DEFAULT_SCENE_DURATION_SECS
                        scene_from_gemini['user_selected_asset_type'] = "Auto (Director's Choice)" # UI default
                        processed_scene_list.append(scene_from_gemini)
                    st.session_state.story_treatment_scenes = processed_scene_list

                    num_generated_scenes = len(st.session_state.story_treatment_scenes) # Renamed
                    st.session_state.scene_generation_prompts = [""]*num_generated_scenes
                    st.session_state.generated_scene_assets_info = [None]*num_generated_scenes
                    logger.info(f"Phase 1 complete. {num_generated_scenes} scenes."); status_op.update(label="Treatment complete! βœ… Generating visuals...", state="running")

                    status_op.write("Phase 2: Creating visual assets (Image/Video)... πŸ–ΌοΈπŸŽ¬"); logger.info("Phase 2: Visual Asset Gen.")
                    successful_asset_count = 0 # Renamed
                    for i_scene, scene_data_item in enumerate(st.session_state.story_treatment_scenes): # Renamed
                        scene_num_display = scene_data_item.get('scene_number', i_scene+1) # Renamed
                        status_op.write(f"  Asset for Scene {scene_num_display}..."); logger.info(f"  Processing asset for Scene {scene_num_display}.")
                        if generate_asset_for_scene_wrapper(i_scene, scene_data_item, version_num=1): # Pass default 'Auto' for initial gen
                            successful_asset_count += 1
                        
                    status_label_phase2 = "Visual assets ready! " # Renamed
                    next_op_state = "running" # Renamed
                    if successful_asset_count == 0 and num_generated_scenes > 0:
                        logger.error("Asset gen failed for all scenes."); status_label_phase2 = "Asset gen FAILED for all scenes."; next_op_state="error";
                        status_op.update(label=status_label_phase2, state=next_op_state, expanded=True); st.stop()
                    elif successful_asset_count < num_generated_scenes:
                        logger.warning(f"Assets partially generated ({successful_asset_count}/{num_generated_scenes})."); status_label_phase2 = f"Assets partially generated ({successful_asset_count}/{num_generated_scenes}). "
                    status_op.update(label=f"{status_label_phase2}Generating narration script...", state=next_op_state)
                    if next_op_state == "error": st.stop()

                    status_op.write("Phase 3: Generating narration script..."); logger.info("Phase 3: Narration Script Gen.")
                    voice_style_for_narration_prompt = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer") # Renamed
                    narration_gen_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, voice_style_for_narration_prompt) # Renamed
                    st.session_state.narration_script_display = st.session_state.gemini_handler.generate_image_prompt(narration_gen_prompt) # generate_image_prompt returns string
                    logger.info("Narration script generated."); status_op.update(label="Narration script ready! Synthesizing voice...", state="running")
                    
                    status_op.write("Phase 4: Synthesizing voice (ElevenLabs)... πŸ”Š"); logger.info("Phase 4: Voice Synthesis.")
                    st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(st.session_state.narration_script_display)
                    
                    final_status_label = "All components ready! Storyboard below. πŸš€" # Renamed
                    final_op_state = "complete"  # Renamed
                    if not st.session_state.overall_narration_audio_path:
                        final_status_label = f"{status_label_phase2}Storyboard ready (Voiceover skipped or failed)."
                        logger.warning("Voiceover generation was skipped or failed.")
                    else: logger.info("Voiceover generated successfully.")
                    status_op.update(label=final_status_label, state=final_op_state, expanded=False)

                except ValueError as ve_err: logger.error(f"ValueError in main generation: {ve_err}", exc_info=True); status_op.update(label=f"Input or Gemini response error: {ve_err}", state="error", expanded=True); # Renamed
                except Exception as e_unhandled: logger.error(f"Unhandled Exception in main generation: {e_unhandled}", exc_info=True); status_op.update(label=f"An unexpected error: {e_unhandled}", state="error", expanded=True); # Renamed
    
    # --- Sidebar Fine-Tuning Options (Characters, Global Style, Voice) ---
    # (Keep these sections as they were in the previous correct version)
    with st.expander("Define Characters", expanded=False):
        char_name_input = st.text_input("Character Name", key="char_name_adv_ultra_v5_sb"); char_desc_input = st.text_area("Visual Description", key="char_desc_adv_ultra_v5_sb", height=100, placeholder="e.g., Jax: rugged male astronaut...")
        if st.button("Save Character", key="add_char_adv_ultra_v5_sb"):
            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"Char '{char_name_input.strip()}' saved.")
            else: st.warning("Name and description needed.")
        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()]
    
    with st.expander("Global Style Overrides", expanded=False):
        style_presets_dict = { "Default (Director's Choice)": "", "Hyper-Realistic Gritty Noir": "hyper-realistic gritty neo-noir...", "Surreal Dreamscape Fantasy": "surreal dreamscape, epic fantasy...", "Vintage Analog Sci-Fi": "70s/80s analog sci-fi..."} # Truncated for brevity
        selected_style_preset_key = st.selectbox("Base Style Preset:", options=list(style_presets_dict.keys()), key="style_preset_adv_ultra_v5_sb")
        custom_style_keywords_input = st.text_area("Additional Custom Style Keywords:", key="custom_style_adv_ultra_v5_sb", height=80, placeholder="e.g., 'Dutch angle'")
        current_global_style = st.session_state.global_style_additions
        if st.button("Apply Global Styles", key="apply_styles_adv_ultra_v5_sb"):
            final_style_str = style_presets_dict[selected_style_preset_key]; 
            if custom_style_keywords_input.strip(): final_style_str = f"{final_style_str}, {custom_style_keywords_input.strip()}" if final_style_str else custom_style_keywords_input.strip()
            st.session_state.global_style_additions = final_style_str.strip(); current_global_style = final_style_str.strip() # Update local var for immediate display
            if current_global_style: st.success("Global styles applied!")
            else: st.info("Global style additions cleared.")
        if current_global_style: st.caption(f"Active global styles: \"{current_global_style}\"")

    with st.expander("Voice & Narration Style", expanded=False):
        engine_default_voice = "Rachel"
        if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine: engine_default_voice = st.session_state.visual_engine.elevenlabs_voice_id
        user_voice_id_input = st.text_input("ElevenLabs Voice ID (override):", value=engine_default_voice, key="el_voice_id_override_v5_sb", help=f"Defaulting to '{engine_default_voice}'.")
        narration_prompt_styles_dict = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
        selected_narration_style_key = st.selectbox("Narration Script Style:", list(narration_prompt_styles_dict.keys()), key="narr_style_sel_v5_sb", index=0)
        if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra_v5_sb"):
            final_voice_id_to_use_el = user_voice_id_input.strip() or st.session_state.get("ELEVENLABS_VOICE_ID_CONFIG", "Rachel") # Fallback
            if hasattr(st.session_state, 'visual_engine'): st.session_state.visual_engine.elevenlabs_voice_id = final_voice_id_to_use_el
            st.session_state.selected_voice_style_for_generation = narration_prompt_styles_dict[selected_narration_style_key]
            st.success(f"Narrator Voice ID: {final_voice_id_to_use_el}. Script Style: {selected_narration_style_key}")
            logger.info(f"User updated 11L Voice ID: {final_voice_id_to_use_el}, Script Style: {selected_narration_style_key}")


# --- Main Content Area ---
st.header("🎬 Cinematic Storyboard & Treatment")
if st.session_state.narration_script_display:
    with st.expander("πŸ“œ View Full Narration Script", expanded=False): st.markdown(f"> _{st.session_state.narration_script_display}_")

if not st.session_state.story_treatment_scenes: st.info("Use the sidebar to generate your cinematic treatment.")
else:
    for i_main_loop, scene_content_item in enumerate(st.session_state.story_treatment_scenes): # Renamed
        scene_num_val = scene_content_item.get('scene_number', i_main_loop + 1) # Renamed
        scene_title_val = scene_content_item.get('scene_title', 'Untitled Scene') # Renamed
        # Ensure unique keys for widgets within the loop
        key_base_for_scene = f"s{scene_num_val}_{''.join(filter(str.isalnum, scene_title_val[:10]))}_main_{i_main_loop}" # Renamed

        if "director_note" in scene_content_item and scene_content_item['director_note']: st.info(f"🎬 Director Note S{scene_num_val}: {scene_content_item['director_note']}")
        st.subheader(f"SCENE {scene_num_val}: {scene_title_val.upper()}"); col_treatment, col_visual = st.columns([0.45, 0.55]) # Renamed

        with col_treatment: # Treatment and Controls Column
            with st.expander("πŸ“ Scene Treatment & Controls", expanded=True):
                # ... (Display textual scene details - beat, setting, chars, etc. - as before) ...
                st.markdown(f"**Beat:** {scene_content_item.get('emotional_beat', 'N/A')}"); st.markdown(f"**Setting:** {scene_content_item.get('setting_description', 'N/A')}"); st.markdown(f"**Chars:** {', '.join(scene_content_item.get('characters_involved', ['N/A']))}"); st.markdown(f"**Focus Moment:** _{scene_content_item.get('character_focus_moment', 'N/A')}_"); st.markdown(f"**Plot Beat:** {scene_content_item.get('key_plot_beat', 'N/A')}"); st.markdown(f"**Dialogue Hook:** `\"{scene_content_item.get('suggested_dialogue_hook', '...')}\"`"); st.markdown("---"); st.markdown(f"**Dir. Visual Style:** _{scene_content_item.get('PROACTIVE_visual_style_감독', 'N/A')}_"); st.markdown(f"**Dir. Camera:** _{scene_content_item.get('PROACTIVE_camera_work_감독', 'N/A')}_"); st.markdown(f"**Dir. Sound:** _{scene_content_item.get('PROACTIVE_sound_design_감독', 'N/A')}_"); st.markdown("---")
                
                st.markdown("##### Shot, Pacing & Asset Controls")
                # User Shot Type (Camera Angle)
                current_ui_shot_type = st.session_state.story_treatment_scenes[i_main_loop].get('user_shot_type', DEFAULT_SHOT_TYPE) # Renamed
                try: shot_type_idx_val = SHOT_TYPES_OPTIONS.index(current_ui_shot_type) # Renamed
                except ValueError: shot_type_idx_val = SHOT_TYPES_OPTIONS.index(DEFAULT_SHOT_TYPE)
                new_ui_shot_type = st.selectbox("Dominant Shot Type:", options=SHOT_TYPES_OPTIONS, index=shot_type_idx_val, key=f"shot_type_widget_{key_base_for_scene}") # Renamed
                if new_ui_shot_type != current_ui_shot_type: st.session_state.story_treatment_scenes[i_main_loop]['user_shot_type'] = new_ui_shot_type

                # User Scene Duration
                current_ui_duration = st.session_state.story_treatment_scenes[i_main_loop].get('user_scene_duration_secs', DEFAULT_SCENE_DURATION_SECS) # Renamed
                new_ui_duration = st.number_input("Scene Duration (seconds):", min_value=1, max_value=300, value=current_ui_duration, step=1, key=f"duration_widget_{key_base_for_scene}") # Renamed
                if new_ui_duration != current_ui_duration: st.session_state.story_treatment_scenes[i_main_loop]['user_scene_duration_secs'] = new_ui_duration

                # User Asset Type Selection
                current_ui_asset_type = st.session_state.story_treatment_scenes[i_main_loop].get('user_selected_asset_type', "Auto (Director's Choice)") # Renamed
                try: asset_type_idx_val = ASSET_TYPE_OPTIONS.index(current_ui_asset_type) # Renamed
                except ValueError: asset_type_idx_val = 0
                new_ui_asset_type = st.selectbox("Asset Type Override:", ASSET_TYPE_OPTIONS, index=asset_type_idx_val, key=f"asset_type_sel_{key_base_for_scene}", help="Choose 'Image' or 'Video Clip'. 'Auto' uses Gemini's suggestion.") # Renamed
                if new_ui_asset_type != current_ui_asset_type: st.session_state.story_treatment_scenes[i_main_loop]['user_selected_asset_type'] = new_ui_asset_type
                st.markdown("---")
                
                # Display generated prompt for the asset
                prompt_for_current_asset = st.session_state.scene_generation_prompts[i_main_loop] if i_main_loop < len(st.session_state.scene_generation_prompts) else None # Renamed
                if prompt_for_current_asset:
                     with st.popover("πŸ‘οΈ View Asset Generation Prompt"):
                        st.markdown(f"**Prompt used for current asset:**"); st.code(prompt_for_current_asset, language='text')
                
                pexels_query_val = scene_content_item.get('pexels_search_query_감독', None) # Renamed
                if pexels_query_val: st.caption(f"Pexels Fallback Query: `{pexels_query_val}`")

        with col_visual: # Visuals Column
            asset_info_for_scene = st.session_state.generated_scene_assets_info[i_main_loop] if i_main_loop < len(st.session_state.generated_scene_assets_info) else None # Renamed
            if asset_info_for_scene and not asset_info_for_scene.get('error') and asset_info_for_scene.get('path') and os.path.exists(asset_info_for_scene['path']):
                path_to_asset_file = asset_info_for_scene['path'] # Renamed
                type_of_asset_file = asset_info_for_scene.get('type', 'image') # Renamed
                if type_of_asset_file == 'image': st.image(path_to_asset_file, caption=f"Scene {scene_num_val} ({type_of_asset_file}): {scene_title_val}")
                elif type_of_asset_file == 'video':
                    try:
                        with open(path_to_asset_file, 'rb') as vf_read: video_bytes_data = vf_read.read() # Renamed
                        st.video(video_bytes_data, format="video/mp4", start_time=0); st.caption(f"Scene {scene_num_val} ({type_of_asset_file}): {scene_title_val}")
                    except Exception as e_vid_display: st.error(f"Error displaying video {path_to_asset_file}: {e_vid_display}"); logger.error(f"Error displaying video: {e_vid_display}", exc_info=True) # Renamed
                else: st.warning(f"Unknown asset type '{type_of_asset_file}' for Scene {scene_num_val}.")
            else:
                if st.session_state.story_treatment_scenes:
                    error_message_display = asset_info_for_scene.get('error_message', 'Visual pending or failed.') if asset_info_for_scene else 'Visual pending or failed.' # Renamed
                    st.caption(error_message_display)
            
            # --- Popovers for Editing Scene Treatment & Visual Prompt ---
            with st.popover(f"✏️ Edit S{scene_num_val} Treatment"):
                feedback_for_treatment = st.text_area("Changes to treatment:", key=f"treat_fb_{key_base_for_scene}", height=150) # Renamed
                if st.button(f"πŸ”„ Update S{scene_num_val} Treatment", key=f"regen_treat_btn_{key_base_for_scene}"):
                    if feedback_for_treatment:
                        with st.status(f"Updating S{scene_num_val} Treatment & Asset...", expanded=True) as status_treatment_regen: # Renamed
                            user_shot_type_pref = st.session_state.story_treatment_scenes[i_main_loop]['user_shot_type'] # Renamed
                            user_duration_pref = st.session_state.story_treatment_scenes[i_main_loop]['user_scene_duration_secs'] # Renamed
                            user_asset_type_pref = st.session_state.story_treatment_scenes[i_main_loop]['user_selected_asset_type'] # Renamed
                            
                            regen_prompt_for_gemini = create_scene_regeneration_prompt(scene_content_item, feedback_for_treatment, st.session_state.story_treatment_scenes) # Renamed
                            try:
                                updated_scene_data_gemini = st.session_state.gemini_handler.regenerate_scene_script_details(regen_prompt_for_gemini) # Renamed
                                final_updated_scene_data = {**updated_scene_data_gemini} # Renamed
                                final_updated_scene_data['user_shot_type'] = user_shot_type_pref
                                final_updated_scene_data['user_scene_duration_secs'] = user_duration_pref
                                final_updated_scene_data['user_selected_asset_type'] = user_asset_type_pref
                                st.session_state.story_treatment_scenes[i_main_loop] = final_updated_scene_data
                                status_treatment_regen.update(label="Treatment updated! Regenerating asset...", state="running")

                                version_num_asset = 1 # Renamed
                                if asset_info_for_scene and asset_info_for_scene.get('path') and os.path.exists(asset_info_for_scene['path']):
                                    try: base_fn_asset,_=os.path.splitext(os.path.basename(asset_info_for_scene['path'])); version_num_asset = int(base_fn_asset.split('_v')[-1])+1 if '_v' in base_fn_asset else 2 # Renamed
                                    except: version_num_asset = 2 
                                
                                if generate_asset_for_scene_wrapper(i_main_loop, final_updated_scene_data, version_num=version_num_asset, user_selected_asset_type_override=user_asset_type_pref):
                                    status_treatment_regen.update(label="Treatment & Asset Updated! πŸŽ‰", state="complete", expanded=False)
                                else: status_treatment_regen.update(label="Treatment updated, asset failed.", state="complete", expanded=False)
                                st.rerun()
                            except Exception as e_treat_regen_main: status_treatment_regen.update(label=f"Error: {e_treat_regen_main}", state="error"); logger.error(f"Scene treatment regen error: {e_treat_regen_main}", exc_info=True) # Renamed
                    else: st.warning("Please provide feedback for treatment.")

            with st.popover(f"🎨 Edit S{scene_num_val} Visual Prompt/Asset"):
                current_gen_prompt_display = st.session_state.scene_generation_prompts[i_main_loop] if i_main_loop < len(st.session_state.scene_generation_prompts) else "No prompt generated yet." # Renamed
                st.caption("Current Asset Generation Prompt:"); st.code(current_gen_prompt_display, language='text')
                feedback_for_visual = st.text_area("Describe changes for the visual asset:", key=f"visual_fb_{key_base_for_scene}", height=150) # Renamed
                if st.button(f"πŸ”„ Update S{scene_num_val} Asset", key=f"regen_visual_btn_{key_base_for_scene}"):
                    if feedback_for_visual:
                        with st.status(f"Refining prompt & regenerating asset for S{scene_num_val}...", expanded=True) as status_visual_regen: # Renamed
                            user_asset_type_choice_visual = st.session_state.story_treatment_scenes[i_main_loop]['user_selected_asset_type'] # Renamed
                            is_video_asset_type = (user_asset_type_choice_visual == "Video Clip") or \
                                              (user_asset_type_choice_visual == "Auto (Director's Choice)" and scene_content_item.get('suggested_asset_type_감독') == 'video_clip')

                            newly_constructed_asset_prompt = "" # Renamed
                            if not is_video_asset_type: # Refining an IMAGE prompt
                                gemini_refinement_prompt = create_visual_regeneration_prompt(current_gen_prompt_display, feedback_for_visual, scene_content_item, st.session_state.character_definitions, st.session_state.global_style_additions) # Renamed
                                try:
                                    newly_constructed_asset_prompt = st.session_state.gemini_handler.refine_image_prompt_from_feedback(gemini_refinement_prompt)
                                    st.session_state.scene_generation_prompts[i_main_loop] = newly_constructed_asset_prompt
                                    status_visual_regen.update(label="Image prompt refined by Gemini! Regenerating asset...", state="running")
                                except Exception as e_gemini_prompt_refine: status_visual_regen.update(label=f"Error refining prompt: {e_gemini_prompt_refine}", state="error"); logger.error(f"Visual prompt refinement error: {e_gemini_prompt_refine}", exc_info=True); continue # Skip asset gen
                            else: # For VIDEO, reconstruct the motion prompt based on current scene data and feedback (feedback isn't directly used by construct_text_to_video_prompt_for_gen4 here, but scene_data might have changed)
                                # For video, feedback should ideally modify scene_content_item's motion description first, then reconstruct.
                                # Simple reconstruction for now:
                                logger.info(f"Reconstructing video motion prompt for S{scene_num_val} based on feedback (indirectly via scene_data). Feedback was: {feedback_for_visual}")
                                newly_constructed_asset_prompt = construct_text_to_video_prompt_for_gen4(scene_content_item, st.session_state.global_style_additions)
                                st.session_state.scene_generation_prompts[i_main_loop] = newly_constructed_asset_prompt
                                status_visual_regen.update(label="Video motion prompt reconstructed! Regenerating asset...", state="running")
                            
                            if not newly_constructed_asset_prompt: status_visual_regen.update(label="Prompt construction failed.", state="error"); continue

                            version_num_visual_asset = 1 # Renamed
                            if asset_info_for_scene and asset_info_for_scene.get('path') and os.path.exists(asset_info_for_scene['path']):
                                try: base_fn_viz_asset,_=os.path.splitext(os.path.basename(asset_info_for_scene['path'])); version_num_visual_asset = int(base_fn_viz_asset.split('_v')[-1])+1 if '_v' in base_fn_viz_asset else 2 # Renamed
                                except: version_num_visual_asset = 2
                            
                            if generate_asset_for_scene_wrapper(i_main_loop, st.session_state.story_treatment_scenes[i_main_loop], version_num=version_num_visual_asset, user_selected_asset_type_override=user_asset_type_choice_visual):
                                status_visual_regen.update(label="Asset Updated! πŸŽ‰", state="complete", expanded=False)
                            else: status_visual_regen.update(label="Prompt updated, asset regeneration failed.", state="complete", expanded=False)
                            st.rerun()
                    else: st.warning("Please provide feedback for visual asset.")
        st.markdown("---")
    
    # Video Assembly Button
    if st.session_state.story_treatment_scenes and any(asset_info_item_loop and not asset_info_item_loop.get('error') and asset_info_item_loop.get('path') for asset_info_item_loop in st.session_state.generated_scene_assets_info if asset_info_item_loop is not None):
        if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn_v5_main", type="primary", use_container_width=True): # Unique key
            with st.status("Assembling Ultra Animatic...", expanded=True) as status_video_assembly: # Renamed
                assets_for_final_video = [] # Renamed
                for i_vid_assembly, scene_data_for_vid in enumerate(st.session_state.story_treatment_scenes): # Renamed
                    asset_info_current_scene = st.session_state.generated_scene_assets_info[i_vid_assembly] if i_vid_assembly < len(st.session_state.generated_scene_assets_info) else None # Renamed
                    if asset_info_current_scene and not asset_info_current_scene.get('error') and asset_info_current_scene.get('path') and os.path.exists(asset_info_current_scene['path']):
                        assets_for_final_video.append({
                            'path': asset_info_current_scene['path'],
                            'type': asset_info_current_scene.get('type', 'image'),
                            'scene_num': scene_data_for_vid.get('scene_number', i_vid_assembly + 1),
                            'key_action': scene_data_for_vid.get('key_plot_beat', ''),
                            'duration': scene_data_for_vid.get('user_scene_duration_secs', DEFAULT_SCENE_DURATION_SECS)
                        })
                        status_video_assembly.write(f"Adding S{scene_data_for_vid.get('scene_number', i_vid_assembly + 1)} ({asset_info_current_scene.get('type')}).")
                    else: logger.warning(f"Skipping S{scene_data_for_vid.get('scene_number', i_vid_assembly+1)} for video: No valid asset.")

                if assets_for_final_video:
                    status_video_assembly.write("Calling video engine...");
                    st.session_state.video_path = st.session_state.visual_engine.assemble_animatic_from_assets(
                        asset_data_list=assets_for_final_video,
                        overall_narration_path=st.session_state.overall_narration_audio_path,
                        output_filename="cinegen_ultra_animatic.mp4", fps=24
                    )
                    if st.session_state.video_path and os.path.exists(st.session_state.video_path):
                        status_video_assembly.update(label="Ultra animatic assembled! πŸŽ‰", state="complete", expanded=False); st.balloons()
                    else: status_video_assembly.update(label="Video assembly failed. Check logs.", state="error", expanded=False); logger.error("Video assembly returned None or file does not exist.")
                else: status_video_assembly.update(label="No valid assets for video assembly.", state="error", expanded=False); logger.warning("No valid assets found for video assembly.")
    elif st.session_state.story_treatment_scenes: st.info("Generate visual assets before assembling the animatic.")

    if st.session_state.video_path and os.path.exists(st.session_state.video_path):
        st.header("🎬 Generated Cinematic Animatic"); 
        try:
            with open(st.session_state.video_path, 'rb') as vf_obj_read: video_bytes_content = vf_obj_read.read() # Renamed
            st.video(video_bytes_content, format="video/mp4") 
            st.download_button(label="Download Ultra Animatic", data=video_bytes_content, file_name=os.path.basename(st.session_state.video_path), mime="video/mp4", use_container_width=True, key="download_ultra_video_btn_v5_main_dl" ) # Unique key
        except Exception as e_vid_final_display: st.error(f"Error displaying final video: {e_vid_final_display}"); logger.error(f"Error displaying final video: {e_vid_final_display}", exc_info=True) # Renamed

# --- Footer ---
st.sidebar.markdown("---"); st.sidebar.caption("CineGen AI Ultra+ | Visionary Cinematic Pre-Production")