mgbam commited on
Commit
972ddb9
Β·
verified Β·
1 Parent(s): 4c2220b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -75
app.py CHANGED
@@ -14,18 +14,17 @@ import logging
14
 
15
  # --- Configuration & Initialization ---
16
  st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
17
- # Setup logger for this app module
18
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
19
- logger = logging.getLogger(__name__) # Use __name__ for module-specific logger
20
 
21
  # --- Global State Variables & API Key Setup ---
22
- def load_api_key(key_name_streamlit, key_name_env, service_name):
23
  key = None; secrets_available = hasattr(st, 'secrets')
24
  try:
25
  if secrets_available and key_name_streamlit in st.secrets:
26
  key = st.secrets[key_name_streamlit]
27
  if key: logger.info(f"{service_name} API Key found in Streamlit secrets.")
28
- except Exception as e: logger.warning(f"Could not access st.secrets for {key_name_streamlit} (may be local dev or misconfiguration): {e}")
29
  if not key and key_name_env in os.environ:
30
  key = os.environ[key_name_env]
31
  if key: logger.info(f"{service_name} API Key found in environment variable.")
@@ -38,9 +37,11 @@ if 'services_initialized' not in st.session_state:
38
  st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
39
  st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
40
  st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
 
 
41
 
42
  if not st.session_state.GEMINI_API_KEY:
43
- st.error("CRITICAL: Gemini API Key is essential and missing! Please set it in secrets or environment variables."); logger.critical("Gemini API Key missing. Halting."); st.stop()
44
 
45
  try:
46
  st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
@@ -48,17 +49,26 @@ if 'services_initialized' not in st.session_state:
48
  except Exception as e: st.error(f"Failed to init GeminiHandler: {e}"); logger.critical(f"GeminiHandler init failed: {e}", exc_info=True); st.stop()
49
 
50
  try:
51
- st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
 
 
 
 
 
52
  st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
53
- st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY)
 
 
 
 
54
  st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
55
  logger.info("VisualEngine initialized and API keys set (or attempted).")
56
  except Exception as e:
57
  st.error(f"Failed to init VisualEngine or set its API keys: {e}"); logger.critical(f"VisualEngine init/key setting failed: {e}", exc_info=True)
58
- st.warning("VisualEngine critical setup issue. Some features will be disabled.") # App can continue with placeholders
59
  st.session_state.services_initialized = True; logger.info("Service initialization sequence complete.")
60
 
61
- # Initialize other session state variables
62
  for key, default_val in [
63
  ('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
64
  ('video_path', None), ('character_definitions', {}), ('global_style_additions', ""),
@@ -66,10 +76,11 @@ for key, default_val in [
66
  ]:
67
  if key not in st.session_state: st.session_state[key] = default_val
68
 
 
69
  def initialize_new_project(): # Same
70
  st.session_state.story_treatment_scenes, st.session_state.scene_dalle_prompts, st.session_state.generated_visual_paths = [], [], []
71
  st.session_state.video_path, st.session_state.overall_narration_audio_path, st.session_state.narration_script_display = None, None, ""
72
- logger.info("New project initialized (session state cleared).")
73
 
74
  def generate_visual_for_scene_core(scene_index, scene_data, version=1): # Same
75
  dalle_prompt = construct_dalle_prompt(scene_data, st.session_state.character_definitions, st.session_state.global_style_additions)
@@ -85,82 +96,75 @@ def generate_visual_for_scene_core(scene_index, scene_data, version=1): # Same
85
  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 was: {img_path}"); return False
86
 
87
  # --- UI Sidebar ---
88
- with st.sidebar:
89
  st.title("🎬 CineGen AI Ultra+")
90
  st.markdown("### Creative Seed")
91
- 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")
92
- genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
93
- mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main")
94
- num_scenes = st.slider("Number of Key Scenes:", 1, 3, 1, key="num_scenes_main")
 
95
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
96
- selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
97
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
98
 
99
- if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
 
100
  initialize_new_project()
101
  if not user_idea.strip(): st.warning("Please provide a story idea.")
102
  else:
103
  with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status:
104
  try:
105
- status.write("Phase 1: Gemini crafting cinematic treatment... πŸ“œ"); logger.info("Phase 1: Cinematic Treatment Gen.")
106
  treatment_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance)
107
  treatment_result_json = st.session_state.gemini_handler.generate_story_breakdown(treatment_prompt)
108
- if not isinstance(treatment_result_json, list) or not treatment_result_json: raise ValueError("Gemini returned invalid scene list for treatment.")
109
  st.session_state.story_treatment_scenes = treatment_result_json; num_gen_scenes = len(st.session_state.story_treatment_scenes)
110
  st.session_state.scene_dalle_prompts = [""]*num_gen_scenes; st.session_state.generated_visual_paths = [None]*num_gen_scenes
111
- logger.info(f"Phase 1 complete. Generated {num_gen_scenes} scenes.")
112
- status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
113
-
114
- status.write("Phase 2: Creating visuals (DALL-E/Pexels)... πŸ–ΌοΈ"); logger.info("Phase 2: Visual Gen.")
115
  visual_successes = 0
116
  for i, sc_data in enumerate(st.session_state.story_treatment_scenes):
117
  sc_num_log = sc_data.get('scene_number', i+1)
118
- status.write(f" Visual for Scene {sc_num_log}: {sc_data.get('scene_title','Untitled')}..."); logger.info(f" Processing visual for Scene {sc_num_log}.")
119
  if generate_visual_for_scene_core(i, sc_data, version=1): visual_successes += 1
120
-
121
- current_status_label_ph2 = "Visuals ready! "
122
- if visual_successes == 0 and num_gen_scenes > 0:
123
- logger.error("Visual gen failed all scenes."); current_status_label_ph2 = "Visual gen FAILED for all scenes.";
124
- status.update(label=current_status_label_ph2, state="error", expanded=True); st.stop()
125
- elif visual_successes < num_gen_scenes:
126
- logger.warning(f"Visuals partial ({visual_successes}/{num_gen_scenes})."); current_status_label_ph2 = f"Visuals partially generated ({visual_successes}/{num_gen_scenes}). "
127
  else: logger.info("All visuals OK.")
128
- status.update(label=f"{current_status_label_ph2}Generating narration script...", state="running")
129
-
130
- status.write("Phase 3: Generating narration script... 🎀"); logger.info("Phase 3: Narration Script Gen.")
 
131
  voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer")
132
  narr_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, voice_style)
133
  st.session_state.narration_script_display = st.session_state.gemini_handler.generate_image_prompt(narr_prompt)
134
  logger.info("Narration script generated."); status.update(label="Narration script ready! Synthesizing voice...", state="running")
135
-
136
- status.write("Phase 4: Synthesizing voice (ElevenLabs)... πŸ”Š"); logger.info("Phase 4: Voice Synthesis.")
137
  st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(st.session_state.narration_script_display)
138
-
139
- final_label = "All components ready! Storyboard below. πŸš€"
140
- final_state_val = "complete" # Default to complete
141
  if not st.session_state.overall_narration_audio_path:
142
- final_label = f"{current_status_label_ph2}Storyboard ready (Voiceover skipped or failed)." # Adjust label if voice failed
143
- logger.warning("Voiceover was skipped or failed, but other components might be ready.")
144
  else: logger.info("Voiceover generated successfully.")
145
- status.update(label=final_label, state=final_state_val, expanded=False) # Keep state complete, label reflects issues
146
-
147
- except ValueError as ve: logger.error(f"ValueError: {ve}", exc_info=True); status.update(label=f"Input or Gemini response error: {ve}", state="error", expanded=True);
148
  except Exception as e: logger.error(f"Unhandled Exception: {e}", exc_info=True); status.update(label=f"An unexpected error occurred: {e}", state="error", expanded=True);
149
 
150
- st.markdown("---"); st.markdown("### Fine-Tuning Options") # UI for these remain same as previous full app.py
151
- with st.expander("Define Characters", expanded=False):
152
- char_name = st.text_input("Character Name", key="char_name_adv_ultra_v3"); char_desc = st.text_area("Visual Description", key="char_desc_adv_ultra_v3", height=100, placeholder="e.g., Jax: rugged male astronaut...")
153
- if st.button("Save Character", key="add_char_adv_ultra_v3"):
154
  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.")
155
  else: st.warning("Name and description needed.")
156
  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()]
157
 
158
- with st.expander("Global Style Overrides", expanded=False):
159
  presets = { "Default (Director's Choice)": "", "Hyper-Realistic Gritty Noir": "hyper-realistic gritty neo-noir...", "Surreal Dreamscape Fantasy": "surreal dreamscape...", "Vintage Analog Sci-Fi": "70s analog sci-fi..."}
160
- sel_preset = st.selectbox("Base Style Preset:", options=list(presets.keys()), key="style_preset_adv_ultra_v3")
161
- custom_kw = st.text_area("Additional Custom Style Keywords:", key="custom_style_adv_ultra_v3", height=80, placeholder="e.g., 'Dutch angle'")
162
  cur_style = st.session_state.global_style_additions
163
- if st.button("Apply Global Styles", key="apply_styles_adv_ultra_v3"):
164
  final_s = presets[sel_preset];
165
  if custom_kw.strip(): final_s = f"{final_s}, {custom_kw.strip()}" if final_s else custom_kw.strip()
166
  st.session_state.global_style_additions = final_s.strip(); cur_style = final_s.strip()
@@ -168,21 +172,30 @@ with st.sidebar:
168
  else: st.info("Global style additions cleared.")
169
  if cur_style: st.caption(f"Active global styles: \"{cur_style}\"")
170
 
171
- with st.expander("Voice Customization (ElevenLabs)", expanded=False):
172
- el_voices_conceptual = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi", "Fin", "Sarah", "Charlie", "Clyde", "Dorothy", "George"]
173
- engine_v_id = "Rachel";
174
- if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine: engine_v_id = st.session_state.visual_engine.elevenlabs_voice_id
175
- try: cur_v_idx = el_voices_conceptual.index(engine_v_id)
176
- except ValueError: cur_v_idx = 0
177
- sel_el_voice = st.selectbox("Narrator Voice:", el_voices_conceptual, index=cur_v_idx, key="el_voice_sel_ultra_v3")
 
 
 
 
178
  prompt_v_styles = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
179
- sel_prompt_v_style_key = st.selectbox("Narration Script Style:", list(prompt_v_styles.keys()), key="narr_style_sel_v3")
180
- if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra_v3"):
181
- if hasattr(st.session_state, 'visual_engine'): st.session_state.visual_engine.elevenlabs_voice_id = sel_el_voice
 
 
 
182
  st.session_state.selected_voice_style_for_generation = prompt_v_styles[sel_prompt_v_style_key]
183
- st.success(f"Narrator: {sel_el_voice}. Script Style: {sel_prompt_v_style_key}")
 
 
184
 
185
- # --- Main Content Area ---
186
  st.header("🎬 Cinematic Storyboard & Treatment")
187
  if st.session_state.narration_script_display:
188
  with st.expander("πŸ“œ View Full Narration Script", expanded=False): st.markdown(f"> _{st.session_state.narration_script_display}_")
@@ -191,10 +204,10 @@ if not st.session_state.story_treatment_scenes: st.info("Use the sidebar to gene
191
  else:
192
  for i_main, scene_content_display in enumerate(st.session_state.story_treatment_scenes):
193
  scene_n = scene_content_display.get('scene_number', i_main + 1); scene_t = scene_content_display.get('scene_title', 'Untitled')
194
- key_base = f"s{scene_n}_{''.join(filter(str.isalnum, scene_t[:10]))}_v3"
195
  if "director_note" in scene_content_display and scene_content_display['director_note']: st.info(f"🎬 Director Note S{scene_n}: {scene_content_display['director_note']}")
196
  st.subheader(f"SCENE {scene_n}: {scene_t.upper()}"); col_d, col_v = st.columns([0.45, 0.55])
197
- with col_d:
198
  with st.expander("πŸ“ Scene Treatment", expanded=True):
199
  st.markdown(f"**Beat:** {scene_content_display.get('emotional_beat', 'N/A')}"); st.markdown(f"**Setting:** {scene_content_display.get('setting_description', 'N/A')}"); st.markdown(f"**Chars:** {', '.join(scene_content_display.get('characters_involved', ['N/A']))}"); st.markdown(f"**Focus Moment:** _{scene_content_display.get('character_focus_moment', 'N/A')}_"); st.markdown(f"**Plot Beat:** {scene_content_display.get('key_plot_beat', 'N/A')}"); st.markdown(f"**Dialogue Hook:** `\"{scene_content_display.get('suggested_dialogue_hook', '...')}\"`"); st.markdown("---"); st.markdown(f"**Dir. Visual Style:** _{scene_content_display.get('PROACTIVE_visual_style_감독', 'N/A')}_"); st.markdown(f"**Dir. Camera:** _{scene_content_display.get('PROACTIVE_camera_work_감독', 'N/A')}_"); st.markdown(f"**Dir. Sound:** _{scene_content_display.get('PROACTIVE_sound_design_감독', 'N/A')}_")
200
  cur_d_prompt = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else None
@@ -202,7 +215,7 @@ else:
202
  with st.popover("πŸ‘οΈ DALL-E Prompt"): st.markdown(f"**DALL-E Prompt:**"); st.code(cur_d_prompt, language='text')
203
  pexels_q = scene_content_display.get('pexels_search_query_감독', None)
204
  if pexels_q: st.caption(f"Pexels Fallback Query: `{pexels_q}`")
205
- with col_v:
206
  cur_img_p = st.session_state.generated_visual_paths[i_main] if i_main < len(st.session_state.generated_visual_paths) else None
207
  if cur_img_p and os.path.exists(cur_img_p): st.image(cur_img_p, caption=f"Scene {scene_n}: {scene_t}") # Removed use_column_width
208
  else:
@@ -219,9 +232,7 @@ else:
219
  st.session_state.story_treatment_scenes[i_main] = updated_sc_data
220
  s_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
221
  v_num = 1
222
- if cur_img_p and os.path.exists(cur_img_p):
223
- 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
224
- except: v_num = 2
225
  else: v_num = 1
226
  if generate_visual_for_scene_core(i_main, updated_sc_data, version=v_num): s_treat_regen.update(label="Treatment & Visual Updated! πŸŽ‰", state="complete", expanded=False)
227
  else: s_treat_regen.update(label="Treatment updated, visual failed.", state="complete", expanded=False)
@@ -243,9 +254,7 @@ else:
243
  st.session_state.scene_dalle_prompts[i_main] = refined_d_prompt
244
  s_visual_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
245
  v_num = 1
246
- if cur_img_p and os.path.exists(cur_img_p):
247
- 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
248
- except: v_num=2
249
  else: v_num = 1
250
  if generate_visual_for_scene_core(i_main, scene_content_display, version=v_num): s_visual_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
251
  else: s_visual_regen.update(label="Prompt refined, visual failed.", state="complete", expanded=False)
@@ -254,8 +263,9 @@ else:
254
  else: st.warning("Please provide feedback.")
255
  st.markdown("---")
256
 
 
257
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
258
- if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn_v3", type="primary", use_container_width=True):
259
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
260
  img_data_vid = []
261
  for i_v, sc_c in enumerate(st.session_state.story_treatment_scenes):
@@ -277,7 +287,7 @@ else:
277
  with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
278
  st.video(video_bytes, format="video/mp4")
279
  with open(st.session_state.video_path, "rb") as fp_dl:
280
- 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_v3" )
281
  except Exception as e: st.error(f"Error displaying video: {e}"); logger.error(f"Error displaying video: {e}", exc_info=True)
282
 
283
  # --- Footer ---
 
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 - %(name)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): # Same
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 found in 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 found in environment variable.")
 
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
+ # --- NEW: Load ElevenLabs Voice ID ---
41
+ st.session_state.ELEVENLABS_VOICE_ID_CONFIG = load_api_key("ELEVENLABS_VOICE_ID", "ELEVENLABS_VOICE_ID", "ElevenLabs Voice ID")
42
 
43
  if not st.session_state.GEMINI_API_KEY:
44
+ st.error("CRITICAL: Gemini API Key is essential and missing!"); logger.critical("Gemini API Key missing. Halting."); st.stop()
45
 
46
  try:
47
  st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
 
49
  except Exception as e: st.error(f"Failed to init GeminiHandler: {e}"); logger.critical(f"GeminiHandler init failed: {e}", exc_info=True); st.stop()
50
 
51
  try:
52
+ # Pass default voice ID to VisualEngine, it will be overridden if ELEVENLABS_VOICE_ID_CONFIG is found
53
+ default_voice = "Rachel" # A common default
54
+ st.session_state.visual_engine = VisualEngine(
55
+ output_dir="temp_cinegen_media",
56
+ default_elevenlabs_voice_id=st.session_state.ELEVENLABS_VOICE_ID_CONFIG or default_voice
57
+ )
58
  st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
59
+ # Pass the API key AND the configured voice ID (if any) to set_elevenlabs_api_key
60
+ st.session_state.visual_engine.set_elevenlabs_api_key(
61
+ st.session_state.ELEVENLABS_API_KEY,
62
+ voice_id_from_secret=st.session_state.ELEVENLABS_VOICE_ID_CONFIG # This might be None
63
+ )
64
  st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
65
  logger.info("VisualEngine initialized and API keys set (or attempted).")
66
  except Exception as e:
67
  st.error(f"Failed to init VisualEngine or set its API keys: {e}"); logger.critical(f"VisualEngine init/key setting failed: {e}", exc_info=True)
68
+ st.warning("VisualEngine critical setup issue. Some features will be disabled.")
69
  st.session_state.services_initialized = True; logger.info("Service initialization sequence complete.")
70
 
71
+ # Initialize other session state variables (same)
72
  for key, default_val in [
73
  ('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
74
  ('video_path', None), ('character_definitions', {}), ('global_style_additions', ""),
 
76
  ]:
77
  if key not in st.session_state: st.session_state[key] = default_val
78
 
79
+ # --- Helper Functions --- (initialize_new_project, generate_visual_for_scene_core - same)
80
  def initialize_new_project(): # Same
81
  st.session_state.story_treatment_scenes, st.session_state.scene_dalle_prompts, st.session_state.generated_visual_paths = [], [], []
82
  st.session_state.video_path, st.session_state.overall_narration_audio_path, st.session_state.narration_script_display = None, None, ""
83
+ logger.info("New project initialized.")
84
 
85
  def generate_visual_for_scene_core(scene_index, scene_data, version=1): # Same
86
  dalle_prompt = construct_dalle_prompt(scene_data, st.session_state.character_definitions, st.session_state.global_style_additions)
 
96
  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 was: {img_path}"); return False
97
 
98
  # --- UI Sidebar ---
99
+ with st.sidebar: # Voice Customization UI slightly changed
100
  st.title("🎬 CineGen AI Ultra+")
101
  st.markdown("### Creative Seed")
102
+ # ... (user_idea, genre, mood, num_scenes, creative_guidance - same) ...
103
+ user_idea = st.text_area("Core Story Idea / Theme:", "A lone wanderer searches for a mythical oasis...", height=120, key="user_idea_main_v4")
104
+ genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic"], index=6, key="genre_main_v4")
105
+ mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense"], index=0, key="mood_main_v4")
106
+ num_scenes = st.slider("Number of Key Scenes:", 1, 3, 1, key="num_scenes_main_v4")
107
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
108
+ selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select_v4")
109
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
110
 
111
+ if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn_v4", use_container_width=True):
112
+ # ... (Main generation flow - same logic for calling phases) ...
113
  initialize_new_project()
114
  if not user_idea.strip(): st.warning("Please provide a story idea.")
115
  else:
116
  with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status:
117
  try:
118
+ status.write("Phase 1: Gemini crafting cinematic treatment..."); logger.info("Phase 1: Treatment Gen.")
119
  treatment_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance)
120
  treatment_result_json = st.session_state.gemini_handler.generate_story_breakdown(treatment_prompt)
121
+ if not isinstance(treatment_result_json, list) or not treatment_result_json: raise ValueError("Gemini invalid scene list.")
122
  st.session_state.story_treatment_scenes = treatment_result_json; num_gen_scenes = len(st.session_state.story_treatment_scenes)
123
  st.session_state.scene_dalle_prompts = [""]*num_gen_scenes; st.session_state.generated_visual_paths = [None]*num_gen_scenes
124
+ logger.info(f"Phase 1 complete. {num_gen_scenes} scenes."); status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
125
+ status.write("Phase 2: Creating visuals (DALL-E/Pexels)..."); logger.info("Phase 2: Visual Gen.")
 
 
126
  visual_successes = 0
127
  for i, sc_data in enumerate(st.session_state.story_treatment_scenes):
128
  sc_num_log = sc_data.get('scene_number', i+1)
129
+ status.write(f" Visual for Scene {sc_num_log}..."); logger.info(f" Processing visual for Scene {sc_num_log}.")
130
  if generate_visual_for_scene_core(i, sc_data, version=1): visual_successes += 1
131
+ current_status_label_ph2 = "Visuals ready! "; next_step_state = "running"
132
+ if visual_successes == 0 and num_gen_scenes > 0: logger.error("Visual gen failed all scenes."); current_status_label_ph2 = "Visual gen FAILED for all scenes."; next_step_state="error"; status.update(label=current_status_label_ph2, state=next_step_state, expanded=True); st.stop()
133
+ elif visual_successes < num_gen_scenes: logger.warning(f"Visuals partial ({visual_successes}/{num_gen_scenes})."); current_status_label_ph2 = f"Visuals partially generated ({visual_successes}/{num_gen_scenes}). "
 
 
 
 
134
  else: logger.info("All visuals OK.")
135
+ status.update(label=f"{current_status_label_ph2}Generating narration script...", state=next_step_state if next_step_state=="error" else "running") # Propagate error state
136
+ if next_step_state == "error": st.stop()
137
+
138
+ status.write("Phase 3: Generating narration script..."); logger.info("Phase 3: Narration Script Gen.")
139
  voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer")
140
  narr_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, voice_style)
141
  st.session_state.narration_script_display = st.session_state.gemini_handler.generate_image_prompt(narr_prompt)
142
  logger.info("Narration script generated."); status.update(label="Narration script ready! Synthesizing voice...", state="running")
143
+ status.write("Phase 4: Synthesizing voice (ElevenLabs)..."); logger.info("Phase 4: Voice Synthesis.")
 
144
  st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(st.session_state.narration_script_display)
145
+ final_label = "All components ready! Storyboard below. πŸš€"; final_state_val = "complete"
 
 
146
  if not st.session_state.overall_narration_audio_path:
147
+ final_label = f"{current_status_label_ph2}Storyboard ready (Voiceover skipped/failed)."
148
+ logger.warning("Voiceover skipped/failed.")
149
  else: logger.info("Voiceover generated successfully.")
150
+ status.update(label=final_label, state=final_state_val, expanded=False)
151
+ except ValueError as ve: logger.error(f"ValueError: {ve}", exc_info=True); status.update(label=f"Input/Gemini response error: {ve}", state="error", expanded=True);
 
152
  except Exception as e: logger.error(f"Unhandled Exception: {e}", exc_info=True); status.update(label=f"An unexpected error occurred: {e}", state="error", expanded=True);
153
 
154
+ st.markdown("---"); st.markdown("### Fine-Tuning Options")
155
+ with st.expander("Define Characters", expanded=False): # Same
156
+ char_name = st.text_input("Character Name", key="char_name_adv_ultra_v4"); char_desc = st.text_area("Visual Description", key="char_desc_adv_ultra_v4", height=100, placeholder="e.g., Jax: rugged male astronaut...")
157
+ if st.button("Save Character", key="add_char_adv_ultra_v4"):
158
  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.")
159
  else: st.warning("Name and description needed.")
160
  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()]
161
 
162
+ with st.expander("Global Style Overrides", expanded=False): # Same
163
  presets = { "Default (Director's Choice)": "", "Hyper-Realistic Gritty Noir": "hyper-realistic gritty neo-noir...", "Surreal Dreamscape Fantasy": "surreal dreamscape...", "Vintage Analog Sci-Fi": "70s analog sci-fi..."}
164
+ sel_preset = st.selectbox("Base Style Preset:", options=list(presets.keys()), key="style_preset_adv_ultra_v4")
165
+ custom_kw = st.text_area("Additional Custom Style Keywords:", key="custom_style_adv_ultra_v4", height=80, placeholder="e.g., 'Dutch angle'")
166
  cur_style = st.session_state.global_style_additions
167
+ if st.button("Apply Global Styles", key="apply_styles_adv_ultra_v4"):
168
  final_s = presets[sel_preset];
169
  if custom_kw.strip(): final_s = f"{final_s}, {custom_kw.strip()}" if final_s else custom_kw.strip()
170
  st.session_state.global_style_additions = final_s.strip(); cur_style = final_s.strip()
 
172
  else: st.info("Global style additions cleared.")
173
  if cur_style: st.caption(f"Active global styles: \"{cur_style}\"")
174
 
175
+ with st.expander("Voice & Narration Style", expanded=False):
176
+ # Allow user to input a specific Voice ID if they know one, otherwise use the configured one.
177
+ default_voice_from_secret = st.session_state.get("ELEVENLABS_VOICE_ID_CONFIG", "Rachel") # Default if secret not set
178
+
179
+ user_voice_id_override = st.text_input(
180
+ "ElevenLabs Voice ID (optional - override secret/default):",
181
+ value=default_voice_from_secret, # Pre-fill with secret or default
182
+ key="el_voice_id_override_v4",
183
+ help="Enter a specific Voice ID from your ElevenLabs account. If empty, uses the one from secrets or a default."
184
+ )
185
+
186
  prompt_v_styles = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
187
+ sel_prompt_v_style_key = st.selectbox("Narration Script Style:", list(prompt_v_styles.keys()), key="narr_style_sel_v4", index=0) # Default to cinematic
188
+
189
+ if st.button("Set Voice & Narration Style", key="set_voice_btn_ultra_v4"):
190
+ final_voice_id_to_use = user_voice_id_override.strip() or default_voice_from_secret
191
+ if hasattr(st.session_state, 'visual_engine'):
192
+ st.session_state.visual_engine.elevenlabs_voice_id = final_voice_id_to_use
193
  st.session_state.selected_voice_style_for_generation = prompt_v_styles[sel_prompt_v_style_key]
194
+ st.success(f"Narrator Voice ID set to: {final_voice_id_to_use}. Script Style: {sel_prompt_v_style_key}")
195
+ logger.info(f"User updated ElevenLabs Voice ID to: {final_voice_id_to_use} and Script Style to: {sel_prompt_v_style_key}")
196
+
197
 
198
+ # --- Main Content Area --- (Scene Display & Edit Popovers - mostly same structure, use_column_width removed from st.image)
199
  st.header("🎬 Cinematic Storyboard & Treatment")
200
  if st.session_state.narration_script_display:
201
  with st.expander("πŸ“œ View Full Narration Script", expanded=False): st.markdown(f"> _{st.session_state.narration_script_display}_")
 
204
  else:
205
  for i_main, scene_content_display in enumerate(st.session_state.story_treatment_scenes):
206
  scene_n = scene_content_display.get('scene_number', i_main + 1); scene_t = scene_content_display.get('scene_title', 'Untitled')
207
+ key_base = f"s{scene_n}_{''.join(filter(str.isalnum, scene_t[:10]))}_v4"
208
  if "director_note" in scene_content_display and scene_content_display['director_note']: st.info(f"🎬 Director Note S{scene_n}: {scene_content_display['director_note']}")
209
  st.subheader(f"SCENE {scene_n}: {scene_t.upper()}"); col_d, col_v = st.columns([0.45, 0.55])
210
+ with col_d: # Scene Details (same display logic)
211
  with st.expander("πŸ“ Scene Treatment", expanded=True):
212
  st.markdown(f"**Beat:** {scene_content_display.get('emotional_beat', 'N/A')}"); st.markdown(f"**Setting:** {scene_content_display.get('setting_description', 'N/A')}"); st.markdown(f"**Chars:** {', '.join(scene_content_display.get('characters_involved', ['N/A']))}"); st.markdown(f"**Focus Moment:** _{scene_content_display.get('character_focus_moment', 'N/A')}_"); st.markdown(f"**Plot Beat:** {scene_content_display.get('key_plot_beat', 'N/A')}"); st.markdown(f"**Dialogue Hook:** `\"{scene_content_display.get('suggested_dialogue_hook', '...')}\"`"); st.markdown("---"); st.markdown(f"**Dir. Visual Style:** _{scene_content_display.get('PROACTIVE_visual_style_감독', 'N/A')}_"); st.markdown(f"**Dir. Camera:** _{scene_content_display.get('PROACTIVE_camera_work_감독', 'N/A')}_"); st.markdown(f"**Dir. Sound:** _{scene_content_display.get('PROACTIVE_sound_design_감독', 'N/A')}_")
213
  cur_d_prompt = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else None
 
215
  with st.popover("πŸ‘οΈ DALL-E Prompt"): st.markdown(f"**DALL-E Prompt:**"); st.code(cur_d_prompt, language='text')
216
  pexels_q = scene_content_display.get('pexels_search_query_감독', None)
217
  if pexels_q: st.caption(f"Pexels Fallback Query: `{pexels_q}`")
218
+ with col_v: # Image Display and Edit Popovers
219
  cur_img_p = st.session_state.generated_visual_paths[i_main] if i_main < len(st.session_state.generated_visual_paths) else None
220
  if cur_img_p and os.path.exists(cur_img_p): st.image(cur_img_p, caption=f"Scene {scene_n}: {scene_t}") # Removed use_column_width
221
  else:
 
232
  st.session_state.story_treatment_scenes[i_main] = updated_sc_data
233
  s_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
234
  v_num = 1
235
+ if cur_img_p and os.path.exists(cur_img_p): 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 except: v_num = 2
 
 
236
  else: v_num = 1
237
  if generate_visual_for_scene_core(i_main, updated_sc_data, version=v_num): s_treat_regen.update(label="Treatment & Visual Updated! πŸŽ‰", state="complete", expanded=False)
238
  else: s_treat_regen.update(label="Treatment updated, visual failed.", state="complete", expanded=False)
 
254
  st.session_state.scene_dalle_prompts[i_main] = refined_d_prompt
255
  s_visual_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
256
  v_num = 1
257
+ if cur_img_p and os.path.exists(cur_img_p): 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 except: v_num=2
 
 
258
  else: v_num = 1
259
  if generate_visual_for_scene_core(i_main, scene_content_display, version=v_num): s_visual_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
260
  else: s_visual_regen.update(label="Prompt refined, visual failed.", state="complete", expanded=False)
 
263
  else: st.warning("Please provide feedback.")
264
  st.markdown("---")
265
 
266
+ # Video Generation Button & Display (same logic)
267
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
268
+ if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn_v4", type="primary", use_container_width=True):
269
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
270
  img_data_vid = []
271
  for i_v, sc_c in enumerate(st.session_state.story_treatment_scenes):
 
287
  with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
288
  st.video(video_bytes, format="video/mp4")
289
  with open(st.session_state.video_path, "rb") as fp_dl:
290
+ 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_v4" )
291
  except Exception as e: st.error(f"Error displaying video: {e}"); logger.error(f"Error displaying video: {e}", exc_info=True)
292
 
293
  # --- Footer ---