mgbam commited on
Commit
ad99fb7
Β·
verified Β·
1 Parent(s): 870979c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -97
app.py CHANGED
@@ -3,7 +3,7 @@ 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, # Using the new "Ultra" prompt
7
  construct_dalle_prompt,
8
  create_narration_script_prompt_enhanced,
9
  create_scene_regeneration_prompt,
@@ -13,43 +13,65 @@ import os
13
 
14
  # --- Configuration & Initialization ---
15
  st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
 
 
 
 
16
 
17
  # --- Global State Variables & API Key Setup ---
18
- def load_api_key(key_name_streamlit, key_name_env):
19
  key = None
20
- secrets_available = hasattr(st, 'secrets') # Check if st.secrets exists
21
  try:
22
  if secrets_available and key_name_streamlit in st.secrets:
23
  key = st.secrets[key_name_streamlit]
24
- except Exception as e: # Catch any error from accessing st.secrets
25
- print(f"Note: Could not access st.secrets for {key_name_streamlit} (may be local dev): {e}")
 
26
 
27
- if not key and key_name_env in os.environ: # Fallback to environment variable
28
  key = os.environ[key_name_env]
 
 
 
 
29
  return key
30
 
31
  # Initialize API Keys and handlers once using session state
32
- if 'keys_loaded' not in st.session_state:
33
- st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY")
34
- st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY")
35
- st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY")
36
- st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY")
 
37
 
38
  if not st.session_state.GEMINI_API_KEY:
39
- st.error("Gemini API Key is essential and missing! Please set it in secrets or environment variables.")
 
40
  st.stop()
41
 
42
- st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
 
 
 
 
 
 
43
 
44
- # Initialize VisualEngine and set API keys
45
- if 'visual_engine' not in st.session_state: # Ensure VE is also session-scoped if needed elsewhere before full init
46
  st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
47
-
48
- st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
49
- st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY)
50
- st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
51
-
52
- st.session_state.keys_loaded = True # Mark keys as loaded
 
 
 
 
 
 
53
 
54
  # Initialize other session state variables
55
  for key, default_val in [
@@ -60,7 +82,6 @@ for key, default_val in [
60
  if key not in st.session_state: st.session_state[key] = default_val
61
  # --- End State & API Key Setup ---
62
 
63
-
64
  def initialize_new_project():
65
  st.session_state.story_treatment_scenes = []
66
  st.session_state.scene_dalle_prompts = []
@@ -68,41 +89,43 @@ def initialize_new_project():
68
  st.session_state.video_path = None
69
  st.session_state.overall_narration_audio_path = None
70
  st.session_state.narration_script_display = ""
71
- # Clean up old media files (optional, good for development)
 
72
  # output_dir = st.session_state.visual_engine.output_dir
73
  # if os.path.exists(output_dir):
 
74
  # for f_name in os.listdir(output_dir):
75
  # try: os.remove(os.path.join(output_dir, f_name))
76
- # except Exception as e: print(f"Could not remove old file {f_name}: {e}")
77
 
78
 
79
  def generate_visual_for_scene_core(scene_index, scene_data, version=1):
80
- # scene_data here is one scene from story_treatment_scenes
81
- dalle_prompt = construct_dalle_prompt( # Use the new prompt constructor
 
82
  scene_data,
83
  st.session_state.character_definitions,
84
  st.session_state.global_style_additions
85
  )
86
  if not dalle_prompt:
87
- print(f"ERROR: DALL-E prompt construction failed for scene {scene_data.get('scene_number', scene_index+1)}")
88
  return False
89
 
90
- # Ensure lists are long enough (should be pre-initialized)
91
  while len(st.session_state.scene_dalle_prompts) <= scene_index: st.session_state.scene_dalle_prompts.append("")
92
  while len(st.session_state.generated_visual_paths) <= scene_index: st.session_state.generated_visual_paths.append(None)
93
-
94
- st.session_state.scene_dalle_prompts[scene_index] = dalle_prompt # Store the generated DALL-E prompt
95
 
96
- filename = f"scene_{scene_data.get('scene_number', scene_index+1)}_visual_v{version}.png"
97
- # Pass the full scene_data to visual_engine for Pexels query construction if DALL-E fails
98
  img_path = st.session_state.visual_engine.generate_image_visual(dalle_prompt, scene_data, filename)
99
 
100
  if img_path and os.path.exists(img_path):
101
  st.session_state.generated_visual_paths[scene_index] = img_path
 
102
  return True
103
  else:
104
  st.session_state.generated_visual_paths[scene_index] = None
105
- print(f"WARNING: Visual generation ultimately failed for scene {scene_data.get('scene_number', scene_index+1)}")
106
  return False
107
 
108
  # --- UI Sidebar ---
@@ -112,68 +135,82 @@ with st.sidebar:
112
  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")
113
  genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
114
  mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main")
115
- num_scenes = st.slider("Number of Key Scenes:", 1, 3, 2, key="num_scenes_main") # Max 3 for API cost/time
116
 
117
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
118
  selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
119
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
120
 
121
-
122
  if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
123
  initialize_new_project()
124
  if not user_idea.strip(): st.warning("Please provide a story idea.")
125
  else:
126
  with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status:
127
- status.write("Phase 1: Gemini crafting cinematic treatment (scenes, style, camera, sound)... πŸ“œ")
128
- treatment_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance)
129
  try:
130
- treatment_result_json = st.session_state.gemini_handler.generate_story_breakdown(treatment_prompt) # Re-use for JSON list
131
- if not isinstance(treatment_result_json, list): # Basic validation
 
 
 
132
  raise ValueError("Gemini did not return a valid list of scenes for the treatment.")
133
  st.session_state.story_treatment_scenes = treatment_result_json
134
-
135
  num_gen_scenes = len(st.session_state.story_treatment_scenes)
136
- if num_gen_scenes == 0: raise ValueError("Gemini returned an empty scene list.")
137
-
138
  st.session_state.scene_dalle_prompts = [""] * num_gen_scenes
139
  st.session_state.generated_visual_paths = [None] * num_gen_scenes
140
- status.update(label="Treatment complete! βœ… Generating visuals...", state="running", expanded=True)
 
141
 
 
 
142
  visual_successes = 0
143
  for i_scene, scene_content in enumerate(st.session_state.story_treatment_scenes):
144
- status.write(f" Creating visual for Scene {scene_content.get('scene_number', i_scene+1)}: {scene_content.get('scene_title','Untitled')}...")
 
 
145
  if generate_visual_for_scene_core(i_scene, scene_content, version=1): visual_successes += 1
146
 
147
  if visual_successes == 0 and num_gen_scenes > 0:
148
- status.update(label="Visual generation failed for all scenes. Check DALL-E/Pexels API keys, quota, or try different prompts.", state="error", expanded=True); st.stop()
 
149
  elif visual_successes < num_gen_scenes:
150
- status.update(label=f"Visuals ready ({visual_successes}/{num_gen_scenes} succeeded). Generating narration script...", state="running", expanded=True)
 
151
  else:
152
- status.update(label="Visuals ready! Generating narration script...", state="running", expanded=True)
153
-
 
154
  # Narration Generation
155
- selected_voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer") # Get from session state if set by UI
 
 
156
  narration_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, selected_voice_style)
157
  narr_script = st.session_state.gemini_handler.generate_image_prompt(narration_prompt)
158
  st.session_state.narration_script_display = narr_script
 
159
  status.update(label="Narration script ready! Synthesizing voice...", state="running")
160
 
 
 
161
  st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(narr_script)
162
- if st.session_state.overall_narration_audio_path: status.update(label="Voiceover ready! ✨", state="running")
163
- else: status.update(label="Voiceover failed or skipped. Video will be silent.", state="warning")
164
-
165
- status.update(label="All components ready! View storyboard below. πŸš€", state="complete", expanded=False)
 
 
166
 
167
- except ValueError as ve: # Catch our own validation errors
168
- status.update(label=f"Treatment generation error: {ve}", state="error", expanded=True); st.stop()
 
169
  except Exception as e:
170
- status.update(label=f"Error during generation: {e}", state="error", expanded=True); st.stop()
 
171
 
172
- st.markdown("---")
173
- st.markdown("### Fine-Tuning Options")
174
  with st.expander("Define Characters", expanded=False):
175
  char_name_input = st.text_input("Character Name", key="char_name_adv_input_ultra")
176
- char_desc_input = st.text_area("Detailed Visual Description", key="char_desc_adv_input_ultra", height=100, placeholder="e.g., Jax: rugged male astronaut, mid-40s, salt-and-pepper short hair...")
177
  if st.button("Save Character", key="add_char_adv_btn_ultra"):
178
  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.")
179
  else: st.warning("Provide name and description.")
@@ -182,9 +219,9 @@ with st.sidebar:
182
  for k,v in st.session_state.character_definitions.items(): st.markdown(f"**{k.title()}:** _{v}_")
183
 
184
  with st.expander("Global Style Overrides", expanded=False):
185
- predefined_styles = { "Default (Director's Choice)": "", "Hyper-Realistic Gritty Noir": "hyper-realistic gritty neo-noir...", "Surreal Dreamscape Fantasy": "surreal dreamscape, epic fantasy elements...", "Vintage Analog Sci-Fi": "70s/80s analog sci-fi film aesthetic..."} # Shortened for brevity
186
  selected_preset = st.selectbox("Base Style Preset:", options=list(predefined_styles.keys()), key="style_preset_select_adv_ultra")
187
- custom_keywords = st.text_area("Additional Custom Style Keywords:", key="custom_style_keywords_adv_input_ultra", height=80, placeholder="e.g., 'shot with Arri Alexa, shallow depth of field'")
188
  current_style_desc = st.session_state.global_style_additions
189
  if st.button("Apply Global Styles", key="apply_styles_adv_btn_ultra"):
190
  final_desc = predefined_styles[selected_preset];
@@ -195,28 +232,17 @@ with st.sidebar:
195
  if current_style_desc: st.caption(f"Active global style additions: \"{current_style_desc}\"")
196
 
197
  with st.expander("Voice Customization (ElevenLabs)", expanded=False):
198
- elevenlabs_voices_conceptual = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi", "Fin", "Sarah", "Charlie", "Clyde", "Dorothy", "George"] # More options
199
- # Get current voice from visual_engine, default if not set
200
  engine_voice_id = "Rachel"
201
- if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine:
202
- engine_voice_id = st.session_state.visual_engine.elevenlabs_voice_id
203
-
204
- try:
205
- current_voice_index = elevenlabs_voices_conceptual.index(engine_voice_id)
206
- except ValueError:
207
- current_voice_index = 0 # Default to Rachel if current ID not in list
208
-
209
- selected_el_voice = st.selectbox("Narrator Voice:", elevenlabs_voices_conceptual,
210
- index=current_voice_index,
211
- key="el_voice_select_ultra")
212
- # Store voice style for narration prompt
213
  voice_styles_for_prompt = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
214
  selected_prompt_voice_style_key = st.selectbox("Narration Script Style:", list(voice_styles_for_prompt.keys()), key="narration_style_select")
215
- st.session_state.selected_voice_style_for_generation = voice_styles_for_prompt[selected_prompt_voice_style_key]
216
-
217
  if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra"):
218
- if hasattr(st.session_state, 'visual_engine'):
219
- st.session_state.visual_engine.elevenlabs_voice_id = selected_el_voice
220
  st.success(f"Narrator voice set to: {selected_el_voice}. Script style: {selected_prompt_voice_style_key}")
221
 
222
 
@@ -263,13 +289,14 @@ else:
263
  if pexels_query_display:
264
  st.caption(f"Suggested Pexels Query for fallback: `{pexels_query_display}`")
265
 
266
- with col_visual:
267
  current_img_path = st.session_state.generated_visual_paths[i_main] if i_main < len(st.session_state.generated_visual_paths) else None
268
  if current_img_path and os.path.exists(current_img_path):
269
  st.image(current_img_path, caption=f"Visual Concept for Scene {scene_num}: {scene_title}", use_column_width='always')
270
  else:
271
- if st.session_state.story_treatment_scenes: st.caption("Visual concept for this scene is pending or failed.")
272
-
 
273
  with st.popover(f"✏️ Edit Scene {scene_num} Treatment"):
274
  feedback_script_edit = st.text_area("Describe changes to treatment details:", key=f"treat_feed_{unique_key_base}", height=150)
275
  if st.button(f"πŸ”„ Update Scene {scene_num} Treatment", key=f"regen_treat_btn_{unique_key_base}"):
@@ -281,16 +308,14 @@ else:
281
  st.session_state.story_treatment_scenes[i_main] = updated_scene_data
282
  status_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
283
  version_num = 1
284
- if current_img_path:
285
- 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
286
- except: version_num=2
287
- if generate_visual_for_scene_core(i_main, updated_scene_data, version=version_num):
288
- status_treat_regen.update(label="Scene Treatment & Visual Updated! πŸŽ‰", state="complete", expanded=False)
289
  else: status_treat_regen.update(label="Treatment updated, visual failed.", state="warning", expanded=False)
290
- st.rerun()
291
  except Exception as e: status_treat_regen.update(label=f"Error: {e}", state="error")
292
  else: st.warning("Please provide feedback for treatment regeneration.")
293
 
 
294
  with st.popover(f"🎨 Edit Scene {scene_num} Visual Prompt"):
295
  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."
296
  st.caption("Current DALL-E Prompt:"); st.code(dalle_prompt_to_edit, language='text')
@@ -307,11 +332,7 @@ else:
307
  st.session_state.scene_dalle_prompts[i_main] = refined_dalle_prompt
308
  status_visual_edit_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
309
  version_num = 1
310
- if current_img_path:
311
- 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
312
- except: version_num=2
313
-
314
- # Pass current scene_content_display for Pexels fallback context
315
  if generate_visual_for_scene_core(i_main, scene_content_display, version=version_num):
316
  status_visual_edit_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
317
  else: status_visual_edit_regen.update(label="Prompt refined, visual failed.", state="warning", expanded=False)
@@ -320,6 +341,7 @@ else:
320
  else: st.warning("Please provide feedback for visual prompt regeneration.")
321
  st.markdown("---")
322
 
 
323
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
324
  if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn", type="primary", use_container_width=True):
325
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
@@ -329,24 +351,25 @@ else:
329
  if img_p and os.path.exists(img_p):
330
  image_data_for_vid.append({
331
  'path':img_p, 'scene_num':scene_c.get('scene_number',i_vid+1),
332
- 'key_action':scene_c.get('key_plot_beat','')
333
  }); status_vid.write(f"Adding Scene {scene_c.get('scene_number', i_vid + 1)} to video.")
334
 
335
  if image_data_for_vid:
336
- status_vid.write("Calling video engine...")
337
  st.session_state.video_path = st.session_state.visual_engine.create_video_from_images(
338
  image_data_for_vid,
339
  overall_narration_path=st.session_state.overall_narration_audio_path,
340
  output_filename="cinegen_ultra_animatic.mp4",
341
- duration_per_image=5,
342
  fps=24
343
  )
344
  if st.session_state.video_path and os.path.exists(st.session_state.video_path):
345
  status_vid.update(label="Ultra animatic assembled! πŸŽ‰", state="complete", expanded=False); st.balloons()
346
- else: status_vid.update(label="Video assembly failed. Check logs.", state="error", expanded=False)
347
  else: status_vid.update(label="No valid images for video.", state="error", expanded=False)
348
  elif st.session_state.story_treatment_scenes: st.info("Generate visuals before assembling video.")
349
 
 
350
  if st.session_state.video_path and os.path.exists(st.session_state.video_path):
351
  st.header("🎬 Generated Cinematic Animatic")
352
  try:
 
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,
 
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)
64
+ st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY)
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 [
 
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 = []
 
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 ---
 
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.")
 
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];
 
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
 
 
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}"):
 
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')
 
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)
 
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:
 
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: