mgbam commited on
Commit
1f0a7de
Β·
verified Β·
1 Parent(s): 200c5c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -83
app.py CHANGED
@@ -10,11 +10,11 @@ from core.prompt_engineering import (
10
  create_visual_regeneration_prompt
11
  )
12
  import os
13
- import logging
14
 
15
  # --- Configuration & Initialization ---
16
  st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
17
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
18
  logger = logging.getLogger(__name__)
19
 
20
  # --- Global State Variables & API Key Setup ---
@@ -23,35 +23,39 @@ def load_api_key(key_name_streamlit, key_name_env, service_name):
23
  try:
24
  if secrets_available and key_name_streamlit in st.secrets:
25
  key = st.secrets[key_name_streamlit]
26
- if key: logger.info(f"{service_name} API Key loaded from Streamlit secrets.")
27
  except Exception as e: logger.warning(f"Could not access st.secrets for {key_name_streamlit}: {e}")
28
  if not key and key_name_env in os.environ:
29
  key = os.environ[key_name_env]
30
- if key: logger.info(f"{service_name} API Key loaded from environment variable.")
31
- if not key: logger.warning(f"{service_name} API Key NOT FOUND.")
32
  return key
33
 
34
  if 'services_initialized' not in st.session_state:
35
- logger.info("Initializing services and API keys...")
36
  st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY", "Gemini")
37
  st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
38
  st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
39
  st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
40
- if not st.session_state.GEMINI_API_KEY: st.error("CRITICAL: Gemini API Key missing!"); st.stop()
 
 
 
41
  try:
42
  st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
43
- logger.info("GeminiHandler initialized.")
44
- except Exception as e: st.error(f"Failed to init GeminiHandler: {e}"); logger.error(f"GeminiHandler init failed: {e}"); st.stop()
 
45
  try:
46
  st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
47
  st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
48
  st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY)
49
  st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
50
- logger.info("VisualEngine initialized and API keys set.")
51
  except Exception as e:
52
- st.error(f"Failed to init VisualEngine or set keys: {e}"); logger.error(f"VisualEngine init/key setting failed: {e}")
53
- st.warning("VisualEngine issue. Features might use placeholders/be disabled.")
54
- st.session_state.services_initialized = True; logger.info("Service initialization complete.")
55
 
56
  for key, default_val in [
57
  ('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
@@ -60,13 +64,13 @@ for key, default_val in [
60
  ]:
61
  if key not in st.session_state: st.session_state[key] = default_val
62
 
63
- # --- Helper Functions --- (initialize_new_project, generate_visual_for_scene_core - same as previous)
64
- def initialize_new_project():
65
  st.session_state.story_treatment_scenes, st.session_state.scene_dalle_prompts, st.session_state.generated_visual_paths = [], [], []
66
  st.session_state.video_path, st.session_state.overall_narration_audio_path, st.session_state.narration_script_display = None, None, ""
67
- logger.info("New project initialized.")
68
 
69
- def generate_visual_for_scene_core(scene_index, scene_data, version=1):
70
  dalle_prompt = construct_dalle_prompt(scene_data, st.session_state.character_definitions, st.session_state.global_style_additions)
71
  if not dalle_prompt: logger.error(f"DALL-E prompt construction failed for scene {scene_data.get('scene_number', scene_index+1)}"); return False
72
  while len(st.session_state.scene_dalle_prompts) <= scene_index: st.session_state.scene_dalle_prompts.append("")
@@ -75,13 +79,14 @@ def generate_visual_for_scene_core(scene_index, scene_data, version=1):
75
  filename = f"scene_{scene_data.get('scene_number', scene_index+1)}_visual_v{version}.png"
76
  img_path = st.session_state.visual_engine.generate_image_visual(dalle_prompt, scene_data, filename)
77
  if img_path and os.path.exists(img_path):
78
- st.session_state.generated_visual_paths[scene_index] = img_path; logger.info(f"Visual generated for Scene {scene_data.get('scene_number', scene_index+1)}: {img_path}"); return True
79
  else:
80
- st.session_state.generated_visual_paths[scene_index] = None; logger.warning(f"Visual generation FAILED for Scene {scene_data.get('scene_number', scene_index+1)}. img_path: {img_path}"); return False
81
 
82
- # --- UI Sidebar --- (same as previous full app.py)
83
  with st.sidebar:
84
- st.title("🎬 CineGen AI Ultra+"); st.markdown("### Creative Seed")
 
85
  user_idea = st.text_area("Core Story Idea / Theme:", "A lone wanderer searches for a mythical oasis in a vast, post-apocalyptic desert, haunted by mirages and mechanical scavengers.", height=120, key="user_idea_main")
86
  genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
87
  mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main")
@@ -89,6 +94,7 @@ with st.sidebar:
89
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
90
  selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
91
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
 
92
  if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
93
  initialize_new_project()
94
  if not user_idea.strip(): st.warning("Please provide a story idea.")
@@ -103,89 +109,104 @@ with st.sidebar:
103
  st.session_state.scene_dalle_prompts = [""]*num_gen_scenes; st.session_state.generated_visual_paths = [None]*num_gen_scenes
104
  logger.info(f"Phase 1 complete. {num_gen_scenes} scenes.")
105
  status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
 
106
  status.write("Phase 2: Creating visuals (DALL-E/Pexels)... πŸ–ΌοΈ"); logger.info("Phase 2: Visual Gen.")
107
  visual_successes = 0
108
- for i, sc in enumerate(st.session_state.story_treatment_scenes):
109
- status.write(f" Visual for Scene {sc.get('scene_number', i+1)}: {sc.get('scene_title','Untitled')}..."); logger.info(f" Processing visual for Scene {sc.get('scene_number', i+1)}.")
110
- if generate_visual_for_scene_core(i, sc, version=1): visual_successes += 1
111
- if visual_successes==0 and num_gen_scenes>0: logger.error("Visual gen failed all scenes."); status.update(label="Visual gen failed. Check logs & API.", state="error", expanded=True); st.stop()
112
- elif visual_successes < num_gen_scenes: logger.warning(f"Visuals partial ({visual_successes}/{num_gen_scenes})."); status.update(label=f"Visuals ready ({visual_successes}/{num_gen_scenes}). Generating narration...", state="running")
113
- else: logger.info("All visuals OK."); status.update(label="Visuals ready! Generating narration script...", state="running")
 
 
 
 
 
 
 
 
 
114
  status.write("Phase 3: Generating narration script... 🎀"); logger.info("Phase 3: Narration Script Gen.")
115
  voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer")
116
  narr_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, voice_style)
117
  st.session_state.narration_script_display = st.session_state.gemini_handler.generate_image_prompt(narr_prompt)
118
  logger.info("Narration script generated."); status.update(label="Narration script ready! Synthesizing voice...", state="running")
 
119
  status.write("Phase 4: Synthesizing voice (ElevenLabs)... πŸ”Š"); logger.info("Phase 4: Voice Synthesis.")
120
  st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(st.session_state.narration_script_display)
121
- if st.session_state.overall_narration_audio_path: logger.info("Voiceover OK."); status.update(label="Voiceover ready! ✨", state="running")
122
- else: logger.warning("Voiceover failed/skipped."); status.update(label="Voiceover failed/skipped.", state="warning")
123
- status.update(label="All components ready! View storyboard. πŸš€", state="complete", expanded=False)
124
- except ValueError as ve: logger.error(f"ValueError: {ve}"); status.update(label=f"Input/Gemini response error: {ve}", state="error", expanded=True);
125
- except Exception as e: logger.error(f"Unhandled Exception: {e}", exc_info=True); status.update(label=f"Error: {e}", state="error", expanded=True);
126
- st.markdown("---"); st.markdown("### Fine-Tuning Options") # Advanced Options (same UI as previous)
 
 
 
 
 
 
 
127
  with st.expander("Define Characters", expanded=False):
128
- char_name = st.text_input("Character Name", key="char_name_adv_ultra"); char_desc = st.text_area("Visual Description", key="char_desc_adv_ultra", height=100, placeholder="e.g., Jax: rugged male astronaut...")
129
- if st.button("Save Character", key="add_char_adv_ultra"):
130
  if char_name and char_desc: st.session_state.character_definitions[char_name.strip().lower()] = char_desc.strip(); st.success(f"Char '{char_name.strip()}' saved.")
131
  else: st.warning("Name and description needed.")
132
  if st.session_state.character_definitions: st.caption("Current Characters:"); [st.markdown(f"**{k.title()}:** _{v}_") for k,v in st.session_state.character_definitions.items()]
 
133
  with st.expander("Global Style Overrides", expanded=False):
134
- presets = {"Default": "", "Gritty Noir": "gritty neo-noir...", "Epic Fantasy": "epic fantasy matte...", "Vintage Sci-Fi": "70s analog sci-fi..."}
135
- sel_preset = st.selectbox("Base Style Preset:", options=list(presets.keys()), key="style_preset_adv_ultra")
136
- custom_kw = st.text_area("Additional Custom Style Keywords:", key="custom_style_adv_ultra", height=80, placeholder="e.g., 'Dutch angle'")
137
  cur_style = st.session_state.global_style_additions
138
- if st.button("Apply Global Styles", key="apply_styles_adv_ultra"):
139
  final_s = presets[sel_preset];
140
  if custom_kw.strip(): final_s = f"{final_s}, {custom_kw.strip()}" if final_s else custom_kw.strip()
141
  st.session_state.global_style_additions = final_s.strip(); cur_style = final_s.strip()
142
  if cur_style: st.success("Global styles applied!")
143
  else: st.info("Global style additions cleared.")
144
  if cur_style: st.caption(f"Active global styles: \"{cur_style}\"")
 
145
  with st.expander("Voice Customization (ElevenLabs)", expanded=False):
146
  el_voices = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi"]
147
  engine_v_id = "Rachel";
148
  if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine: engine_v_id = st.session_state.visual_engine.elevenlabs_voice_id
149
  try: cur_v_idx = el_voices.index(engine_v_id)
150
  except ValueError: cur_v_idx = 0
151
- sel_el_voice = st.selectbox("Narrator Voice:", el_voices, index=cur_v_idx, key="el_voice_sel_ultra")
152
  prompt_v_styles = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
153
- sel_prompt_v_style_key = st.selectbox("Narration Script Style:", list(prompt_v_styles.keys()), key="narr_style_sel")
154
- if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra"):
155
  if hasattr(st.session_state, 'visual_engine'): st.session_state.visual_engine.elevenlabs_voice_id = sel_el_voice
156
  st.session_state.selected_voice_style_for_generation = prompt_v_styles[sel_prompt_v_style_key]
157
  st.success(f"Narrator: {sel_el_voice}. Script Style: {sel_prompt_v_style_key}")
158
 
 
159
  # --- Main Content Area ---
160
  st.header("🎬 Cinematic Storyboard & Treatment")
161
  if st.session_state.narration_script_display:
162
  with st.expander("πŸ“œ View Full Narration Script", expanded=False): st.markdown(f"> _{st.session_state.narration_script_display}_")
163
- if not st.session_state.story_treatment_scenes: st.info("Use sidebar to generate cinematic treatment.")
 
164
  else:
165
- for i, scene_content in enumerate(st.session_state.story_treatment_scenes):
166
- scene_n = scene_content.get('scene_number', i + 1); scene_t = scene_content.get('scene_title', 'Untitled')
167
- key_base = f"s{scene_n}_{''.join(filter(str.isalnum, scene_t[:10]))}"
168
- if "director_note" in scene_content and scene_content['director_note']: st.info(f"🎬 Director Note S{scene_n}: {scene_content['director_note']}")
169
  st.subheader(f"SCENE {scene_n}: {scene_t.upper()}"); col_d, col_v = st.columns([0.45, 0.55])
170
  with col_d:
171
  with st.expander("πŸ“ Scene Treatment", expanded=True):
172
- st.markdown(f"**Beat:** {scene_content.get('emotional_beat', 'N/A')}")
173
- st.markdown(f"**Setting:** {scene_content.get('setting_description', 'N/A')}")
174
- st.markdown(f"**Chars:** {', '.join(scene_content.get('characters_involved', ['N/A']))}")
175
- st.markdown(f"**Focus Moment:** _{scene_content.get('character_focus_moment', 'N/A')}_")
176
- st.markdown(f"**Plot Beat:** {scene_content.get('key_plot_beat', 'N/A')}")
177
- st.markdown(f"**Dialogue Hook:** `\"{scene_content.get('suggested_dialogue_hook', '...')}\"`")
178
- st.markdown("---"); st.markdown(f"**Dir. Visual Style:** _{scene_content.get('PROACTIVE_visual_style_감독', 'N/A')}_")
179
- st.markdown(f"**Dir. Camera:** _{scene_content.get('PROACTIVE_camera_work_감독', 'N/A')}_")
180
- st.markdown(f"**Dir. Sound:** _{scene_content.get('PROACTIVE_sound_design_감독', 'N/A')}_")
181
- cur_d_prompt = st.session_state.scene_dalle_prompts[i] if i < len(st.session_state.scene_dalle_prompts) else None
182
  if cur_d_prompt:
183
  with st.popover("πŸ‘οΈ DALL-E Prompt"): st.markdown(f"**DALL-E Prompt:**"); st.code(cur_d_prompt, language='text')
184
- pexels_q = scene_content.get('pexels_search_query_감독', None)
185
  if pexels_q: st.caption(f"Pexels Fallback Query: `{pexels_q}`")
186
  with col_v:
187
- cur_img_p = st.session_state.generated_visual_paths[i] if i < len(st.session_state.generated_visual_paths) else None
188
- if cur_img_p and os.path.exists(cur_img_p): st.image(cur_img_p, caption=f"Scene {scene_n}: {scene_t}", use_column_width='always')
189
  else:
190
  if st.session_state.story_treatment_scenes: st.caption("Visual pending/failed.")
191
 
@@ -194,64 +215,63 @@ else:
194
  if st.button(f"πŸ”„ Update Scene {scene_n} Treatment", key=f"regen_treat_btn_{key_base}"):
195
  if fb_script:
196
  with st.status(f"Updating Scene {scene_n}...", expanded=True) as s_treat_regen:
197
- prompt_text = create_scene_regeneration_prompt(scene_content, fb_script, st.session_state.story_treatment_scenes)
198
  try:
199
  updated_sc_data = st.session_state.gemini_handler.regenerate_scene_script_details(prompt_text)
200
- st.session_state.story_treatment_scenes[i] = updated_sc_data
201
  s_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
202
  v_num = 1
203
- if cur_img_p:
204
  try: b,_=os.path.splitext(os.path.basename(cur_img_p)); v_num = int(b.split('_v')[-1])+1 if '_v' in b else 2
205
- except: v_num = 2 # Default to v2 if parsing fails
206
- else: v_num = 1 # First gen for this scene visual
207
-
208
- if generate_visual_for_scene_core(i, updated_sc_data, version=v_num): s_treat_regen.update(label="Treatment & Visual Updated! πŸŽ‰", state="complete", expanded=False)
209
- else: s_treat_regen.update(label="Treatment updated, visual failed.", state="warning", expanded=False)
210
  st.rerun()
211
- except Exception as e_regen: s_treat_regen.update(label=f"Error: {e_regen}", state="error")
212
  else: st.warning("Please provide feedback.")
213
 
214
  with st.popover(f"🎨 Edit Scene {scene_n} Visual Prompt"):
215
- d_prompt_edit = st.session_state.scene_dalle_prompts[i] if i < len(st.session_state.scene_dalle_prompts) else "No DALL-E prompt."
216
  st.caption("Current DALL-E Prompt:"); st.code(d_prompt_edit, language='text')
217
  fb_visual = st.text_area("Changes for DALL-E prompt:", key=f"visual_fb_{key_base}", height=150)
218
  if st.button(f"πŸ”„ Update Scene {scene_n} Visual", key=f"regen_visual_btn_{key_base}"):
219
  if fb_visual:
220
  with st.status(f"Refining prompt & visual for Scene {scene_n}...", expanded=True) as s_visual_regen:
221
- ref_req_prompt = create_visual_regeneration_prompt(d_prompt_edit, fb_visual, scene_content,
222
  st.session_state.character_definitions, st.session_state.global_style_additions)
223
  try:
224
  refined_d_prompt = st.session_state.gemini_handler.generate_image_prompt(ref_req_prompt)
225
- st.session_state.scene_dalle_prompts[i] = refined_d_prompt
226
  s_visual_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
227
  v_num = 1
228
- if cur_img_p:
229
  try: b,_=os.path.splitext(os.path.basename(cur_img_p)); v_num = int(b.split('_v')[-1])+1 if '_v' in b else 2
230
  except: v_num=2
231
  else: v_num = 1
232
-
233
- if generate_visual_for_scene_core(i, scene_content, version=v_num): s_visual_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
234
- else: s_visual_regen.update(label="Prompt refined, visual failed.", state="warning", expanded=False)
235
  st.rerun()
236
- except Exception as e_regen_vis: s_visual_regen.update(label=f"Error: {e_regen_vis}", state="error")
237
  else: st.warning("Please provide feedback.")
238
  st.markdown("---")
239
 
 
240
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
241
- if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn", type="primary", use_container_width=True):
242
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
243
  img_data_vid = []
244
  for i_v, sc_c in enumerate(st.session_state.story_treatment_scenes):
245
  img_p_v = st.session_state.generated_visual_paths[i_v] if i_v < len(st.session_state.generated_visual_paths) else None
246
  if img_p_v and os.path.exists(img_p_v):
247
- img_data_vid.append({'path':img_p_v, 'scene_num':sc_c.get('scene_number',i_v+1), 'key_action':sc_c.get('key_plot_beat','')}); status_vid.write(f"Adding Scene {sc_c.get('scene_number', i_v+1)}.")
248
  if img_data_vid:
249
  status_vid.write("Calling video engine..."); st.session_state.video_path = st.session_state.visual_engine.create_video_from_images(
250
  img_data_vid, overall_narration_path=st.session_state.overall_narration_audio_path,
251
  output_filename="cinegen_ultra_animatic.mp4", duration_per_image=5, fps=24)
252
  if st.session_state.video_path and os.path.exists(st.session_state.video_path): status_vid.update(label="Ultra animatic assembled! πŸŽ‰", state="complete", expanded=False); st.balloons()
253
- else: status_vid.update(label="Video assembly failed. Check logs.", state="error", expanded=False)
254
- else: status_vid.update(label="No valid images for video.", state="error", expanded=False)
255
  elif st.session_state.story_treatment_scenes: st.info("Generate visuals before assembling video.")
256
 
257
  if st.session_state.video_path and os.path.exists(st.session_state.video_path):
@@ -260,8 +280,8 @@ else:
260
  with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
261
  st.video(video_bytes, format="video/mp4")
262
  with open(st.session_state.video_path, "rb") as fp_dl:
263
- st.download_button(label="Download Ultra Animatic", data=fp_dl, file_name=os.path.basename(st.session_state.video_path), mime="video/mp4", use_container_width=True, key="download_ultra_video_btn" )
264
- except Exception as e: st.error(f"Error displaying video: {e}")
265
 
266
  # --- Footer ---
267
  st.sidebar.markdown("---"); st.sidebar.caption("CineGen AI Ultra+ | Visionary Cinematic Pre-Production")
 
10
  create_visual_regeneration_prompt
11
  )
12
  import os
13
+ import logging # For better logging
14
 
15
  # --- Configuration & Initialization ---
16
  st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
17
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18
  logger = logging.getLogger(__name__)
19
 
20
  # --- Global State Variables & API Key Setup ---
 
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.")
31
+ if not key: logger.warning(f"{service_name} API Key NOT FOUND. Related features may be disabled or use fallbacks.")
32
  return key
33
 
34
  if 'services_initialized' not in st.session_state:
35
+ logger.info("Initializing services and API keys for the first time this session...")
36
  st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY", "Gemini")
37
  st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
38
  st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
39
  st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
40
+
41
+ if not st.session_state.GEMINI_API_KEY:
42
+ st.error("CRITICAL: Gemini API Key is essential and missing! Application cannot proceed."); logger.critical("Gemini API Key missing. Halting."); st.stop()
43
+
44
  try:
45
  st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
46
+ logger.info("GeminiHandler initialized successfully.")
47
+ except Exception as e: st.error(f"Failed to init GeminiHandler: {e}"); logger.critical(f"GeminiHandler init failed: {e}", exc_info=True); st.stop()
48
+
49
  try:
50
  st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
51
  st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
52
  st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY)
53
  st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
54
+ logger.info("VisualEngine initialized and API keys set (or attempted).")
55
  except Exception as e:
56
+ 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)
57
+ st.warning("VisualEngine critical setup issue. Some features will be disabled.")
58
+ st.session_state.services_initialized = True; logger.info("Service initialization sequence complete.")
59
 
60
  for key, default_val in [
61
  ('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
 
64
  ]:
65
  if key not in st.session_state: st.session_state[key] = default_val
66
 
67
+ # --- Helper Functions ---
68
+ def initialize_new_project(): # Same
69
  st.session_state.story_treatment_scenes, st.session_state.scene_dalle_prompts, st.session_state.generated_visual_paths = [], [], []
70
  st.session_state.video_path, st.session_state.overall_narration_audio_path, st.session_state.narration_script_display = None, None, ""
71
+ logger.info("New project initialized (session state cleared).")
72
 
73
+ def generate_visual_for_scene_core(scene_index, scene_data, version=1): # Same
74
  dalle_prompt = construct_dalle_prompt(scene_data, st.session_state.character_definitions, st.session_state.global_style_additions)
75
  if not dalle_prompt: logger.error(f"DALL-E prompt construction failed for scene {scene_data.get('scene_number', scene_index+1)}"); return False
76
  while len(st.session_state.scene_dalle_prompts) <= scene_index: st.session_state.scene_dalle_prompts.append("")
 
79
  filename = f"scene_{scene_data.get('scene_number', scene_index+1)}_visual_v{version}.png"
80
  img_path = st.session_state.visual_engine.generate_image_visual(dalle_prompt, scene_data, filename)
81
  if img_path and os.path.exists(img_path):
82
+ st.session_state.generated_visual_paths[scene_index] = img_path; logger.info(f"Visual generated for Scene {scene_data.get('scene_number', scene_index+1)}: {os.path.basename(img_path)}"); return True
83
  else:
84
+ 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
85
 
86
+ # --- UI Sidebar ---
87
  with st.sidebar:
88
+ st.title("🎬 CineGen AI Ultra+")
89
+ st.markdown("### Creative Seed")
90
  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")
91
  genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
92
  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
  creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
95
  selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
96
  actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
97
+
98
  if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
99
  initialize_new_project()
100
  if not user_idea.strip(): st.warning("Please provide a story idea.")
 
109
  st.session_state.scene_dalle_prompts = [""]*num_gen_scenes; st.session_state.generated_visual_paths = [None]*num_gen_scenes
110
  logger.info(f"Phase 1 complete. {num_gen_scenes} scenes.")
111
  status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
112
+
113
  status.write("Phase 2: Creating visuals (DALL-E/Pexels)... πŸ–ΌοΈ"); logger.info("Phase 2: Visual Gen.")
114
  visual_successes = 0
115
+ for i, sc_data in enumerate(st.session_state.story_treatment_scenes):
116
+ sc_num_log = sc_data.get('scene_number', i+1)
117
+ 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}.")
118
+ if generate_visual_for_scene_core(i, sc_data, version=1): visual_successes += 1
119
+
120
+ current_status_label_ph2 = "Visuals ready! "
121
+ next_step_state = "running"
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."; next_step_state="error";
124
+ status.update(label=current_status_label_ph2, state=next_step_state, 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"
141
+ if not st.session_state.overall_narration_audio_path:
142
+ final_label = f"{current_status_label_ph2}Storyboard ready (Voiceover skipped/failed)."
143
+ logger.warning("Voiceover was skipped or failed.")
144
+ else: logger.info("Voiceover generated successfully.")
145
+ status.update(label=final_label, state=final_state_val, expanded=False)
146
+
147
+ 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);
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")
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()
167
  if cur_style: st.success("Global styles applied!")
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 = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi"]
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.index(engine_v_id)
176
  except ValueError: cur_v_idx = 0
177
+ sel_el_voice = st.selectbox("Narrator Voice:", el_voices, 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
+
186
  # --- Main Content Area ---
187
  st.header("🎬 Cinematic Storyboard & Treatment")
188
  if st.session_state.narration_script_display:
189
  with st.expander("πŸ“œ View Full Narration Script", expanded=False): st.markdown(f"> _{st.session_state.narration_script_display}_")
190
+
191
+ if not st.session_state.story_treatment_scenes: st.info("Use the sidebar to generate your cinematic treatment.")
192
  else:
193
+ for i_main, scene_content_display in enumerate(st.session_state.story_treatment_scenes):
194
+ scene_n = scene_content_display.get('scene_number', i_main + 1); scene_t = scene_content_display.get('scene_title', 'Untitled')
195
+ key_base = f"s{scene_n}_{''.join(filter(str.isalnum, scene_t[:10]))}_v3" # Added version to key_base for uniqueness
196
+ 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']}")
197
  st.subheader(f"SCENE {scene_n}: {scene_t.upper()}"); col_d, col_v = st.columns([0.45, 0.55])
198
  with col_d:
199
  with st.expander("πŸ“ Scene Treatment", expanded=True):
200
+ # ... (markdown display of scene details, same as before) ...
201
+ 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')}_")
202
+ cur_d_prompt = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else None
 
 
 
 
 
 
 
203
  if cur_d_prompt:
204
  with st.popover("πŸ‘οΈ DALL-E Prompt"): st.markdown(f"**DALL-E Prompt:**"); st.code(cur_d_prompt, language='text')
205
+ pexels_q = scene_content_display.get('pexels_search_query_감독', None)
206
  if pexels_q: st.caption(f"Pexels Fallback Query: `{pexels_q}`")
207
  with col_v:
208
+ cur_img_p = st.session_state.generated_visual_paths[i_main] if i_main < len(st.session_state.generated_visual_paths) else None
209
+ if cur_img_p and os.path.exists(cur_img_p): st.image(cur_img_p, caption=f"Scene {scene_n}: {scene_t}", use_column_width='always')
210
  else:
211
  if st.session_state.story_treatment_scenes: st.caption("Visual pending/failed.")
212
 
 
215
  if st.button(f"πŸ”„ Update Scene {scene_n} Treatment", key=f"regen_treat_btn_{key_base}"):
216
  if fb_script:
217
  with st.status(f"Updating Scene {scene_n}...", expanded=True) as s_treat_regen:
218
+ prompt_text = create_scene_regeneration_prompt(scene_content_display, fb_script, st.session_state.story_treatment_scenes)
219
  try:
220
  updated_sc_data = st.session_state.gemini_handler.regenerate_scene_script_details(prompt_text)
221
+ st.session_state.story_treatment_scenes[i_main] = updated_sc_data
222
  s_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
223
  v_num = 1
224
+ if cur_img_p and os.path.exists(cur_img_p):
225
  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
226
+ except: v_num = 2
227
+ else: v_num = 1
228
+ 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)
229
+ else: s_treat_regen.update(label="Treatment updated, visual failed.", state="complete", expanded=False)
 
230
  st.rerun()
231
+ except Exception as e_regen: s_treat_regen.update(label=f"Error: {e_regen}", state="error"); logger.error(f"Scene treatment regen error: {e_regen}", exc_info=True)
232
  else: st.warning("Please provide feedback.")
233
 
234
  with st.popover(f"🎨 Edit Scene {scene_n} Visual Prompt"):
235
+ d_prompt_edit = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else "No DALL-E prompt."
236
  st.caption("Current DALL-E Prompt:"); st.code(d_prompt_edit, language='text')
237
  fb_visual = st.text_area("Changes for DALL-E prompt:", key=f"visual_fb_{key_base}", height=150)
238
  if st.button(f"πŸ”„ Update Scene {scene_n} Visual", key=f"regen_visual_btn_{key_base}"):
239
  if fb_visual:
240
  with st.status(f"Refining prompt & visual for Scene {scene_n}...", expanded=True) as s_visual_regen:
241
+ ref_req_prompt = create_visual_regeneration_prompt(d_prompt_edit, fb_visual, scene_content_display,
242
  st.session_state.character_definitions, st.session_state.global_style_additions)
243
  try:
244
  refined_d_prompt = st.session_state.gemini_handler.generate_image_prompt(ref_req_prompt)
245
+ st.session_state.scene_dalle_prompts[i_main] = refined_d_prompt
246
  s_visual_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
247
  v_num = 1
248
+ if cur_img_p and os.path.exists(cur_img_p):
249
  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
250
  except: v_num=2
251
  else: v_num = 1
252
+ 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)
253
+ else: s_visual_regen.update(label="Prompt refined, visual failed.", state="complete", expanded=False)
 
254
  st.rerun()
255
+ except Exception as e_regen_vis: s_visual_regen.update(label=f"Error: {e_regen_vis}", state="error"); logger.error(f"Visual prompt regen error: {e_regen_vis}", exc_info=True)
256
  else: st.warning("Please provide feedback.")
257
  st.markdown("---")
258
 
259
+ # Video Generation Button & Display (same as previous)
260
  if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
261
+ if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn_v3", type="primary", use_container_width=True):
262
  with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
263
  img_data_vid = []
264
  for i_v, sc_c in enumerate(st.session_state.story_treatment_scenes):
265
  img_p_v = st.session_state.generated_visual_paths[i_v] if i_v < len(st.session_state.generated_visual_paths) else None
266
  if img_p_v and os.path.exists(img_p_v):
267
+ img_data_vid.append({'path':img_p_v, 'scene_num':sc_c.get('scene_number',i_v+1), 'key_action':sc_c.get('key_plot_beat','')}); status_vid.write(f"Adding Scene {sc_c.get('scene_number', i_v + 1)} to video.")
268
  if img_data_vid:
269
  status_vid.write("Calling video engine..."); st.session_state.video_path = st.session_state.visual_engine.create_video_from_images(
270
  img_data_vid, overall_narration_path=st.session_state.overall_narration_audio_path,
271
  output_filename="cinegen_ultra_animatic.mp4", duration_per_image=5, fps=24)
272
  if st.session_state.video_path and os.path.exists(st.session_state.video_path): status_vid.update(label="Ultra animatic assembled! πŸŽ‰", state="complete", expanded=False); st.balloons()
273
+ else: status_vid.update(label="Video assembly failed. Check logs.", state="error", expanded=False); logger.error("Video assembly returned None or file does not exist.")
274
+ else: status_vid.update(label="No valid images for video.", state="error", expanded=False); logger.warning("No valid images found for video assembly.")
275
  elif st.session_state.story_treatment_scenes: st.info("Generate visuals before assembling video.")
276
 
277
  if st.session_state.video_path and os.path.exists(st.session_state.video_path):
 
280
  with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
281
  st.video(video_bytes, format="video/mp4")
282
  with open(st.session_state.video_path, "rb") as fp_dl:
283
+ 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" )
284
+ except Exception as e: st.error(f"Error displaying video: {e}"); logger.error(f"Error displaying video: {e}", exc_info=True)
285
 
286
  # --- Footer ---
287
  st.sidebar.markdown("---"); st.sidebar.caption("CineGen AI Ultra+ | Visionary Cinematic Pre-Production")