rairo commited on
Commit
27839ea
·
verified ·
1 Parent(s): 1952ce6

Update sozo_gen.py

Browse files
Files changed (1) hide show
  1. sozo_gen.py +26 -7
sozo_gen.py CHANGED
@@ -27,7 +27,7 @@ import requests
27
  # In sozo_gen.py, near the other google imports
28
  from google.genai import types as genai_types
29
  import math # Add this import at the top of your sozo_gen.py file
30
-
31
  # --- Configuration ---
32
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s')
33
  FPS, WIDTH, HEIGHT = 24, 1280, 720
@@ -612,6 +612,7 @@ def generate_video_from_project(df: pd.DataFrame, raw_md: str, data_context: Dic
612
  video_dur = audio_dur + 1.5
613
 
614
  try:
 
615
  chart_descs = extract_chart_tags(sc)
616
  pexels_descs = extract_pexels_tags(sc)
617
  is_conclusion_scene = any(k in narrative.lower() for k in ["conclusion", "summary", "in closing", "final thoughts"])
@@ -627,13 +628,28 @@ def generate_video_from_project(df: pd.DataFrame, raw_md: str, data_context: Dic
627
  conclusion_video_path = video_path
628
  elif chart_descs:
629
  logging.info(f"Scene {i+1}: Primary attempt with animated chart.")
 
630
  safe_chart(chart_descs[0], df, video_dur, mp4, data_context)
631
  video_parts.append(str(mp4))
632
  else:
633
- raise ValueError("No visual tag found in scene.")
634
  except Exception as e:
635
- logging.warning(f"Scene {i+1}: Primary visual failed ({e}). Marking for fallback.")
636
- video_parts.append("FALLBACK_NEEDED")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
637
 
638
  temps.append(mp4)
639
 
@@ -647,10 +663,13 @@ def generate_video_from_project(df: pd.DataFrame, raw_md: str, data_context: Dic
647
  for part in video_parts:
648
  if part == "FALLBACK_NEEDED":
649
  if conclusion_video_path:
650
- logging.info("Applying conclusion video as fallback for a failed scene.")
651
- final_video_parts.append(conclusion_video_path)
 
 
 
652
  else:
653
- logging.error("Cannot apply fallback; no conclusion video available.")
654
  else:
655
  final_video_parts.append(part)
656
 
 
27
  # In sozo_gen.py, near the other google imports
28
  from google.genai import types as genai_types
29
  import math # Add this import at the top of your sozo_gen.py file
30
+ import shutil
31
  # --- Configuration ---
32
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s')
33
  FPS, WIDTH, HEIGHT = 24, 1280, 720
 
612
  video_dur = audio_dur + 1.5
613
 
614
  try:
615
+ # --- Primary Visual Generation ---
616
  chart_descs = extract_chart_tags(sc)
617
  pexels_descs = extract_pexels_tags(sc)
618
  is_conclusion_scene = any(k in narrative.lower() for k in ["conclusion", "summary", "in closing", "final thoughts"])
 
628
  conclusion_video_path = video_path
629
  elif chart_descs:
630
  logging.info(f"Scene {i+1}: Primary attempt with animated chart.")
631
+ if not chart_descs: raise ValueError("AI script failed to provide a chart tag for this scene.")
632
  safe_chart(chart_descs[0], df, video_dur, mp4, data_context)
633
  video_parts.append(str(mp4))
634
  else:
635
+ raise ValueError("No visual tag found in scene script.")
636
  except Exception as e:
637
+ logging.warning(f"Scene {i+1}: Primary visual failed ({e}). Triggering Fallback Tier 1.")
638
+ # --- Fallback Tier 1: Context-Aware Pexels Replacement ---
639
+ try:
640
+ fallback_keywords = extract_keywords_for_query(narrative, llm)
641
+ final_fallback_query = f"{fallback_keywords} {domain}"
642
+ logging.info(f"Fallback Tier 1: Searching Pexels with query: '{final_fallback_query}'")
643
+
644
+ video_path = search_and_download_pexels_video(final_fallback_query, video_dur, mp4)
645
+ if not video_path: raise ValueError("Fallback Pexels search returned no results.")
646
+
647
+ video_parts.append(video_path)
648
+ logging.info(f"Scene {i+1}: Successfully recovered with a relevant Pexels video.")
649
+ except Exception as fallback_e:
650
+ # --- Fallback Tier 2: Looping Conclusion Failsafe ---
651
+ logging.error(f"Scene {i+1}: Fallback Tier 1 also failed ({fallback_e}). Marking for final failsafe.")
652
+ video_parts.append("FALLBACK_NEEDED")
653
 
654
  temps.append(mp4)
655
 
 
663
  for part in video_parts:
664
  if part == "FALLBACK_NEEDED":
665
  if conclusion_video_path:
666
+ fallback_copy_path = Path(tempfile.gettempdir()) / f"fallback_{uuid.uuid4().hex}.mp4"
667
+ shutil.copy(conclusion_video_path, fallback_copy_path)
668
+ temps.append(fallback_copy_path)
669
+ final_video_parts.append(str(fallback_copy_path))
670
+ logging.info(f"Applying unique copy of conclusion video as fallback for a failed scene.")
671
  else:
672
+ logging.error("Cannot apply fallback; no conclusion video available. A scene will be missing.")
673
  else:
674
  final_video_parts.append(part)
675