CingenAI / app.py
mgbam's picture
Update app.py
ad99fb7 verified
raw
history blame
28.2 kB
# app.py
import streamlit as st
from core.gemini_handler import GeminiHandler
from core.visual_engine import VisualEngine
from core.prompt_engineering import (
create_cinematic_treatment_prompt,
construct_dalle_prompt,
create_narration_script_prompt_enhanced,
create_scene_regeneration_prompt,
create_visual_regeneration_prompt
)
import os
# --- Configuration & Initialization ---
st.set_page_config(page_title="CineGen AI Ultra+", layout="wide", initial_sidebar_state="expanded")
# For robust logging, especially on deployed environments
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# --- Global State Variables & API Key Setup ---
def load_api_key(key_name_streamlit, key_name_env, service_name):
key = None
secrets_available = hasattr(st, 'secrets')
try:
if secrets_available and key_name_streamlit in st.secrets:
key = st.secrets[key_name_streamlit]
if key: logger.info(f"{service_name} API Key loaded from Streamlit secrets.")
except Exception as e:
logger.warning(f"Could not access st.secrets for {key_name_streamlit} (may be local dev or misconfiguration): {e}")
if not key and key_name_env in os.environ:
key = os.environ[key_name_env]
if key: logger.info(f"{service_name} API Key loaded from environment variable.")
if not key:
logger.warning(f"{service_name} API Key NOT FOUND in secrets or environment variables.")
return key
# Initialize API Keys and handlers once using session state
if 'services_initialized' not in st.session_state:
logger.info("Initializing services and API keys for the first time...")
st.session_state.GEMINI_API_KEY = load_api_key("GEMINI_API_KEY", "GEMINI_API_KEY", "Gemini")
st.session_state.OPENAI_API_KEY = load_api_key("OPENAI_API_KEY", "OPENAI_API_KEY", "OpenAI/DALL-E")
st.session_state.ELEVENLABS_API_KEY = load_api_key("ELEVENLABS_API_KEY", "ELEVENLABS_API_KEY", "ElevenLabs")
st.session_state.PEXELS_API_KEY = load_api_key("PEXELS_API_KEY", "PEXELS_API_KEY", "Pexels")
if not st.session_state.GEMINI_API_KEY:
st.error("CRITICAL: Gemini API Key is essential and missing! Application cannot proceed.")
logger.error("Gemini API Key missing. Halting application.")
st.stop()
try:
st.session_state.gemini_handler = GeminiHandler(api_key=st.session_state.GEMINI_API_KEY)
logger.info("GeminiHandler initialized successfully.")
except Exception as e:
st.error(f"Failed to initialize GeminiHandler: {e}")
logger.error(f"GeminiHandler initialization failed: {e}")
st.stop()
try:
st.session_state.visual_engine = VisualEngine(output_dir="temp_cinegen_media")
st.session_state.visual_engine.set_openai_api_key(st.session_state.OPENAI_API_KEY)
st.session_state.visual_engine.set_elevenlabs_api_key(st.session_state.ELEVENLABS_API_KEY)
st.session_state.visual_engine.set_pexels_api_key(st.session_state.PEXELS_API_KEY)
logger.info("VisualEngine initialized and API keys set.")
except Exception as e:
st.error(f"Failed to initialize VisualEngine or set its API keys: {e}")
logger.error(f"VisualEngine initialization/key setting failed: {e}")
# Don't stop, visual engine has fallbacks, but warn user
st.warning("VisualEngine encountered an issue during setup. Some visual/audio features might use placeholders or be disabled.")
st.session_state.services_initialized = True
logger.info("Service initialization complete.")
# Initialize other session state variables
for key, default_val in [
('story_treatment_scenes', []), ('scene_dalle_prompts', []), ('generated_visual_paths', []),
('video_path', None), ('character_definitions', {}), ('global_style_additions', ""),
('overall_narration_audio_path', None), ('narration_script_display', "")
]:
if key not in st.session_state: st.session_state[key] = default_val
# --- End State & API Key Setup ---
def initialize_new_project():
st.session_state.story_treatment_scenes = []
st.session_state.scene_dalle_prompts = []
st.session_state.generated_visual_paths = []
st.session_state.video_path = None
st.session_state.overall_narration_audio_path = None
st.session_state.narration_script_display = ""
logger.info("New project initialized, session state cleared.")
# Optional: Clean up old media files
# output_dir = st.session_state.visual_engine.output_dir
# if os.path.exists(output_dir):
# logger.info(f"Cleaning up old media in {output_dir}")
# for f_name in os.listdir(output_dir):
# try: os.remove(os.path.join(output_dir, f_name))
# except Exception as e: logger.warning(f"Could not remove old file {f_name}: {e}")
def generate_visual_for_scene_core(scene_index, scene_data, version=1):
scene_num_log = scene_data.get('scene_number', scene_index + 1)
logger.info(f"Generating DALL-E prompt for Scene {scene_num_log} (v{version}).")
dalle_prompt = construct_dalle_prompt(
scene_data,
st.session_state.character_definitions,
st.session_state.global_style_additions
)
if not dalle_prompt:
logger.error(f"DALL-E prompt construction failed for Scene {scene_num_log}.")
return False
while len(st.session_state.scene_dalle_prompts) <= scene_index: st.session_state.scene_dalle_prompts.append("")
while len(st.session_state.generated_visual_paths) <= scene_index: st.session_state.generated_visual_paths.append(None)
st.session_state.scene_dalle_prompts[scene_index] = dalle_prompt
filename = f"scene_{scene_num_log}_visual_v{version}.png"
logger.info(f"Calling VisualEngine to generate visual for Scene {scene_num_log} with filename {filename}.")
img_path = st.session_state.visual_engine.generate_image_visual(dalle_prompt, scene_data, filename)
if img_path and os.path.exists(img_path):
st.session_state.generated_visual_paths[scene_index] = img_path
logger.info(f"Visual successfully generated for Scene {scene_num_log}: {img_path}")
return True
else:
st.session_state.generated_visual_paths[scene_index] = None
logger.warning(f"Visual generation FAILED for Scene {scene_num_log}. img_path was: {img_path}")
return False
# --- UI Sidebar ---
with st.sidebar:
st.title("🎬 CineGen AI Ultra+")
st.markdown("### Creative Seed")
user_idea = st.text_area("Core Story Idea / Theme:", "A lone wanderer searches for a mythical oasis in a vast, post-apocalyptic desert, haunted by mirages and mechanical scavengers.", height=120, key="user_idea_main")
genre = st.selectbox("Primary Genre:", ["Cyberpunk", "Sci-Fi", "Fantasy", "Noir", "Thriller", "Western", "Post-Apocalyptic", "Historical Drama", "Surreal"], index=6, key="genre_main")
mood = st.selectbox("Overall Mood:", ["Hopeful yet Desperate", "Mysterious & Eerie", "Gritty & Tense", "Epic & Awe-Inspiring", "Melancholy & Reflective", "Whimsical & Lighthearted"], index=0, key="mood_main")
num_scenes = st.slider("Number of Key Scenes:", 1, 3, 1, key="num_scenes_main") # Default 1 for faster testing initially
creative_guidance_options = {"Standard Director": "standard", "Artistic Visionary": "more_artistic", "Experimental Storyteller": "experimental_narrative"}
selected_creative_guidance_key = st.selectbox("AI Creative Director Style:", options=list(creative_guidance_options.keys()), key="creative_guidance_select")
actual_creative_guidance = creative_guidance_options[selected_creative_guidance_key]
if st.button("🌌 Generate Cinematic Treatment", type="primary", key="generate_treatment_btn", use_container_width=True):
initialize_new_project()
if not user_idea.strip(): st.warning("Please provide a story idea.")
else:
with st.status("AI Director is envisioning your masterpiece...", expanded=True) as status:
try:
status.write("Phase 1: Gemini crafting cinematic treatment... πŸ“œ")
logger.info("Initiating Phase 1: Cinematic Treatment Generation.")
treatment_prompt = create_cinematic_treatment_prompt(user_idea, genre, mood, num_scenes, actual_creative_guidance)
treatment_result_json = st.session_state.gemini_handler.generate_story_breakdown(treatment_prompt)
if not isinstance(treatment_result_json, list) or not treatment_result_json:
raise ValueError("Gemini did not return a valid list of scenes for the treatment.")
st.session_state.story_treatment_scenes = treatment_result_json
num_gen_scenes = len(st.session_state.story_treatment_scenes)
st.session_state.scene_dalle_prompts = [""] * num_gen_scenes
st.session_state.generated_visual_paths = [None] * num_gen_scenes
logger.info(f"Phase 1 complete. Generated {num_gen_scenes} scenes.")
status.update(label="Treatment complete! βœ… Generating visuals...", state="running")
status.write("Phase 2: Creating visuals (DALL-E/Pexels)... πŸ–ΌοΈ (This may take time per scene)")
logger.info("Initiating Phase 2: Visual Generation.")
visual_successes = 0
for i_scene, scene_content in enumerate(st.session_state.story_treatment_scenes):
scene_num_log_ph2 = scene_content.get('scene_number', i_scene+1)
status.write(f" Creating visual for Scene {scene_num_log_ph2}: {scene_content.get('scene_title','Untitled')}...")
logger.info(f" Processing visual for Scene {scene_num_log_ph2}.")
if generate_visual_for_scene_core(i_scene, scene_content, version=1): visual_successes += 1
if visual_successes == 0 and num_gen_scenes > 0:
logger.error("Visual generation failed for all scenes.")
status.update(label="Visual generation failed for all scenes. Check logs & API status.", state="error", expanded=True); st.stop()
elif visual_successes < num_gen_scenes:
logger.warning(f"Visuals partially generated ({visual_successes}/{num_gen_scenes}).")
status.update(label=f"Visuals ready ({visual_successes}/{num_gen_scenes} succeeded). Generating narration...", state="running")
else:
logger.info("All visuals generated successfully.")
status.update(label="Visuals ready! Generating narration script...", state="running")
# Narration Generation
status.write("Phase 3: Generating narration script with Gemini... 🎀")
logger.info("Initiating Phase 3: Narration Script Generation.")
selected_voice_style = st.session_state.get("selected_voice_style_for_generation", "cinematic_trailer")
narration_prompt = create_narration_script_prompt_enhanced(st.session_state.story_treatment_scenes, mood, genre, selected_voice_style)
narr_script = st.session_state.gemini_handler.generate_image_prompt(narration_prompt)
st.session_state.narration_script_display = narr_script
logger.info("Narration script generated.")
status.update(label="Narration script ready! Synthesizing voice...", state="running")
status.write("Phase 4: Synthesizing voice with ElevenLabs... πŸ”Š")
logger.info("Initiating Phase 4: Voice Synthesis.")
st.session_state.overall_narration_audio_path = st.session_state.visual_engine.generate_narration_audio(narr_script)
if st.session_state.overall_narration_audio_path:
logger.info("Voiceover generated successfully.")
status.update(label="Voiceover ready! ✨ All components generated.", state="complete", expanded=False)
else:
logger.warning("Voiceover failed or was skipped.")
status.update(label="Voiceover failed/skipped. Storyboard ready.", state="complete", expanded=False) # Still complete the overall process
except ValueError as ve:
logger.error(f"ValueError during generation: {ve}")
status.update(label=f"Input or Gemini response error: {ve}", state="error", expanded=True);
except Exception as e:
logger.error(f"Unhandled Exception during generation: {e}", exc_info=True)
status.update(label=f"An unexpected error occurred: {e}", state="error", expanded=True);
st.markdown("---") # Advanced Options & Voice Customization (UI remains the same as last full app.py)
# ... (Character Consistency expander UI - same) ...
with st.expander("Define Characters", expanded=False):
char_name_input = st.text_input("Character Name", key="char_name_adv_input_ultra")
char_desc_input = st.text_area("Detailed Visual Description", key="char_desc_adv_input_ultra", height=100, placeholder="e.g., Jax: rugged male astronaut...")
if st.button("Save Character", key="add_char_adv_btn_ultra"):
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.")
else: st.warning("Provide name and description.")
if st.session_state.character_definitions:
st.caption("Current Characters:")
for k,v in st.session_state.character_definitions.items(): st.markdown(f"**{k.title()}:** _{v}_")
with st.expander("Global Style Overrides", expanded=False):
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..."}
selected_preset = st.selectbox("Base Style Preset:", options=list(predefined_styles.keys()), key="style_preset_select_adv_ultra")
custom_keywords = st.text_area("Additional Custom Style Keywords:", key="custom_style_keywords_adv_input_ultra", height=80, placeholder="e.g., 'Dutch angle'")
current_style_desc = st.session_state.global_style_additions
if st.button("Apply Global Styles", key="apply_styles_adv_btn_ultra"):
final_desc = predefined_styles[selected_preset];
if custom_keywords.strip(): final_desc = f"{final_desc}, {custom_keywords.strip()}" if final_desc else custom_keywords.strip()
st.session_state.global_style_additions = final_desc.strip(); current_style_desc = final_desc.strip()
if current_style_desc: st.success("Global styles applied!")
else: st.info("Global style additions cleared.")
if current_style_desc: st.caption(f"Active global style additions: \"{current_style_desc}\"")
with st.expander("Voice Customization (ElevenLabs)", expanded=False):
elevenlabs_voices_conceptual = ["Rachel", "Adam", "Bella", "Antoni", "Elli", "Josh", "Arnold", "Domi", "Fin", "Sarah", "Charlie", "Clyde", "Dorothy", "George"]
engine_voice_id = "Rachel"
if hasattr(st.session_state, 'visual_engine') and st.session_state.visual_engine: engine_voice_id = st.session_state.visual_engine.elevenlabs_voice_id
try: current_voice_index = elevenlabs_voices_conceptual.index(engine_voice_id)
except ValueError: current_voice_index = 0
selected_el_voice = st.selectbox("Narrator Voice:", elevenlabs_voices_conceptual, index=current_voice_index, key="el_voice_select_ultra")
voice_styles_for_prompt = {"Cinematic Trailer": "cinematic_trailer", "Neutral Documentary": "documentary_neutral", "Character Introspection": "introspective_character"}
selected_prompt_voice_style_key = st.selectbox("Narration Script Style:", list(voice_styles_for_prompt.keys()), key="narration_style_select")
if st.button("Set Narrator Voice & Style", key="set_voice_btn_ultra"):
if hasattr(st.session_state, 'visual_engine'): st.session_state.visual_engine.elevenlabs_voice_id = selected_el_voice
st.session_state.selected_voice_style_for_generation = voice_styles_for_prompt[selected_prompt_voice_style_key] # Store for next full generation
st.success(f"Narrator voice set to: {selected_el_voice}. Script style: {selected_prompt_voice_style_key}")
# --- Main Content Area ---
st.header("🎬 Cinematic Storyboard & Treatment")
if st.session_state.narration_script_display:
with st.expander("πŸ“œ View Full Narration Script", expanded=False):
st.markdown(f"> _{st.session_state.narration_script_display}_")
if not st.session_state.story_treatment_scenes:
st.info("Use the sidebar to generate your cinematic treatment.")
else:
for i_main, scene_content_display in enumerate(st.session_state.story_treatment_scenes):
scene_num = scene_content_display.get('scene_number', i_main + 1)
scene_title = scene_content_display.get('scene_title', 'Untitled Scene')
unique_key_base = f"scene_{scene_num}_{''.join(filter(str.isalnum, scene_title[:10]))}"
if "director_note" in scene_content_display and scene_content_display['director_note']:
st.info(f"🎬 Director's Note for Scene {scene_num}: {scene_content_display['director_note']}")
st.subheader(f"SCENE {scene_num}: {scene_title.upper()}")
col_details, col_visual = st.columns([0.45, 0.55])
with col_details:
with st.expander("πŸ“ Scene Treatment Details", expanded=True):
st.markdown(f"**Emotional Beat:** {scene_content_display.get('emotional_beat', 'N/A')}")
st.markdown(f"**Setting:** {scene_content_display.get('setting_description', 'N/A')}")
st.markdown(f"**Characters:** {', '.join(scene_content_display.get('characters_involved', ['N/A']))}")
st.markdown(f"**Character Focus Moment:** _{scene_content_display.get('character_focus_moment', 'N/A')}_")
st.markdown(f"**Key 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"**🎬 Director's Visual Style:** _{scene_content_display.get('PROACTIVE_visual_style_감독', 'N/A')}_")
st.markdown(f"**πŸŽ₯ Director's Camera Work:** _{scene_content_display.get('PROACTIVE_camera_work_감독', 'N/A')}_")
st.markdown(f"**πŸ”Š Director's Sound Design:** _{scene_content_display.get('PROACTIVE_sound_design_감독', 'N/A')}_")
current_dalle_prompt = st.session_state.scene_dalle_prompts[i_main] if i_main < len(st.session_state.scene_dalle_prompts) else None
if current_dalle_prompt:
with st.popover("πŸ‘οΈ View DALL-E Prompt"):
st.markdown(f"**Full DALL-E Prompt:**"); st.code(current_dalle_prompt, language='text')
pexels_query_display = scene_content_display.get('pexels_search_query_감독', None)
if pexels_query_display:
st.caption(f"Suggested Pexels Query for fallback: `{pexels_query_display}`")
with col_visual: # Edit Popovers (logic for regeneration calls remain largely the same)
current_img_path = st.session_state.generated_visual_paths[i_main] if i_main < len(st.session_state.generated_visual_paths) else None
if current_img_path and os.path.exists(current_img_path):
st.image(current_img_path, caption=f"Visual Concept for Scene {scene_num}: {scene_title}", use_column_width='always')
else:
if st.session_state.story_treatment_scenes: st.caption("Visual for this scene is pending or failed.")
# Edit Scene Treatment Popover
with st.popover(f"✏️ Edit Scene {scene_num} Treatment"):
feedback_script_edit = st.text_area("Describe changes to treatment details:", key=f"treat_feed_{unique_key_base}", height=150)
if st.button(f"πŸ”„ Update Scene {scene_num} Treatment", key=f"regen_treat_btn_{unique_key_base}"):
if feedback_script_edit:
with st.status(f"Updating Scene {scene_num} Treatment...", expanded=True) as status_treat_regen:
regen_prompt_text = create_scene_regeneration_prompt(scene_content_display, feedback_script_edit, st.session_state.story_treatment_scenes)
try:
updated_scene_data = st.session_state.gemini_handler.regenerate_scene_script_details(regen_prompt_text)
st.session_state.story_treatment_scenes[i_main] = updated_scene_data
status_treat_regen.update(label="Treatment updated! Regenerating visual...", state="running")
version_num = 1
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
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)
else: status_treat_regen.update(label="Treatment updated, visual failed.", state="warning", expanded=False)
st.rerun() # Rerun to refresh the whole UI with new data
except Exception as e: status_treat_regen.update(label=f"Error: {e}", state="error")
else: st.warning("Please provide feedback for treatment regeneration.")
# Edit Visual Prompt Popover
with st.popover(f"🎨 Edit Scene {scene_num} Visual Prompt"):
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."
st.caption("Current DALL-E Prompt:"); st.code(dalle_prompt_to_edit, language='text')
feedback_visual_edit = st.text_area("Describe changes for the DALL-E prompt:", key=f"visual_feed_{unique_key_base}", height=150)
if st.button(f"πŸ”„ Update Scene {scene_num} Visual Prompt & Image", key=f"regen_visual_btn_{unique_key_base}"):
if feedback_visual_edit:
with st.status(f"Refining DALL-E prompt & regenerating visual...", expanded=True) as status_visual_edit_regen:
refinement_req_prompt = create_visual_regeneration_prompt(
dalle_prompt_to_edit, feedback_visual_edit, scene_content_display,
st.session_state.character_definitions, st.session_state.global_style_additions
)
try:
refined_dalle_prompt = st.session_state.gemini_handler.generate_image_prompt(refinement_req_prompt)
st.session_state.scene_dalle_prompts[i_main] = refined_dalle_prompt
status_visual_edit_regen.update(label="DALL-E prompt refined! Regenerating visual...", state="running")
version_num = 1
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
if generate_visual_for_scene_core(i_main, scene_content_display, version=version_num):
status_visual_edit_regen.update(label="Visual Updated! πŸŽ‰", state="complete", expanded=False)
else: status_visual_edit_regen.update(label="Prompt refined, visual failed.", state="warning", expanded=False)
st.rerun()
except Exception as e: status_visual_edit_regen.update(label=f"Error: {e}", state="error")
else: st.warning("Please provide feedback for visual prompt regeneration.")
st.markdown("---")
# Video Generation Button
if st.session_state.story_treatment_scenes and any(p for p in st.session_state.generated_visual_paths if p is not None):
if st.button("🎬 Assemble Narrated Cinematic Animatic", key="assemble_ultra_video_btn", type="primary", use_container_width=True):
with st.status("Assembling Ultra Animatic...", expanded=True) as status_vid:
image_data_for_vid = []
for i_vid, scene_c in enumerate(st.session_state.story_treatment_scenes):
img_p = st.session_state.generated_visual_paths[i_vid] if i_vid < len(st.session_state.generated_visual_paths) else None
if img_p and os.path.exists(img_p):
image_data_for_vid.append({
'path':img_p, 'scene_num':scene_c.get('scene_number',i_vid+1),
'key_action':scene_c.get('key_plot_beat','') # Using key_plot_beat for overlay now
}); status_vid.write(f"Adding Scene {scene_c.get('scene_number', i_vid + 1)} to video.")
if image_data_for_vid:
status_vid.write("Calling video engine (with narration if available)...")
st.session_state.video_path = st.session_state.visual_engine.create_video_from_images(
image_data_for_vid,
overall_narration_path=st.session_state.overall_narration_audio_path,
output_filename="cinegen_ultra_animatic.mp4",
duration_per_image=5, # Allow more time for narration per scene
fps=24
)
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()
else: status_vid.update(label="Video assembly failed. Check application logs.", state="error", expanded=False)
else: status_vid.update(label="No valid images for video.", state="error", expanded=False)
elif st.session_state.story_treatment_scenes: st.info("Generate visuals before assembling video.")
# Video display and download
if st.session_state.video_path and os.path.exists(st.session_state.video_path):
st.header("🎬 Generated Cinematic Animatic")
try:
with open(st.session_state.video_path, 'rb') as vf_obj: video_bytes = vf_obj.read()
st.video(video_bytes, format="video/mp4")
with open(st.session_state.video_path, "rb") as fp_dl:
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" )
except Exception as e: st.error(f"Error displaying video: {e}")
# --- Footer ---
st.sidebar.markdown("---")
st.sidebar.caption("CineGen AI Ultra+ | Visionary Cinematic Pre-Production")