Update sozo_gen.py
Browse files- sozo_gen.py +83 -126
sozo_gen.py
CHANGED
|
@@ -15,7 +15,7 @@ import matplotlib.pyplot as plt
|
|
| 15 |
from matplotlib.animation import FuncAnimation, FFMpegWriter
|
| 16 |
import seaborn as sns
|
| 17 |
from scipy import stats
|
| 18 |
-
from PIL import Image
|
| 19 |
import cv2
|
| 20 |
import inspect
|
| 21 |
import tempfile
|
|
@@ -29,7 +29,7 @@ import requests
|
|
| 29 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s')
|
| 30 |
FPS, WIDTH, HEIGHT = 24, 1280, 720
|
| 31 |
MAX_CHARTS, VIDEO_SCENES = 5, 5
|
| 32 |
-
MAX_CONTEXT_TOKENS =
|
| 33 |
|
| 34 |
# --- API Initialization ---
|
| 35 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
@@ -38,7 +38,7 @@ if not API_KEY:
|
|
| 38 |
|
| 39 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 40 |
|
| 41 |
-
# --- Helper Functions
|
| 42 |
def load_dataframe_safely(buf, name: str):
|
| 43 |
ext = Path(name).suffix.lower()
|
| 44 |
df = (pd.read_excel if ext in (".xlsx", ".xls") else pd.read_csv)(buf)
|
|
@@ -83,26 +83,31 @@ def clean_narration(txt: str) -> str:
|
|
| 83 |
|
| 84 |
def placeholder_img() -> Image.Image: return Image.new("RGB", (WIDTH, HEIGHT), (230, 230, 230))
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
return placeholder_img()
|
| 96 |
-
except Exception:
|
| 97 |
-
return placeholder_img()
|
| 98 |
|
|
|
|
| 99 |
def search_and_download_pexels_video(query: str, duration: float, out_path: Path) -> str:
|
| 100 |
if not PEXELS_API_KEY:
|
| 101 |
logging.warning("PEXELS_API_KEY not set. Cannot fetch stock video.")
|
| 102 |
return None
|
| 103 |
try:
|
| 104 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 105 |
-
params = {"query": query, "per_page":
|
| 106 |
response = requests.get("https://api.pexels.com/videos/search", headers=headers, params=params, timeout=20)
|
| 107 |
response.raise_for_status()
|
| 108 |
videos = response.json().get('videos', [])
|
|
@@ -130,10 +135,13 @@ def search_and_download_pexels_video(query: str, duration: float, out_path: Path
|
|
| 130 |
temp_dl_file.write(chunk)
|
| 131 |
temp_dl_path = Path(temp_dl_file.name)
|
| 132 |
|
|
|
|
| 133 |
cmd = [
|
| 134 |
-
"ffmpeg", "-y",
|
|
|
|
|
|
|
| 135 |
"-vf", f"scale={WIDTH}:{HEIGHT}:force_original_aspect_ratio=decrease,pad={WIDTH}:{HEIGHT}:(ow-iw)/2:(oh-ih)/2,setsar=1",
|
| 136 |
-
"-t", f"{duration:.3f}",
|
| 137 |
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-an",
|
| 138 |
str(out_path)
|
| 139 |
]
|
|
@@ -147,7 +155,6 @@ def search_and_download_pexels_video(query: str, duration: float, out_path: Path
|
|
| 147 |
temp_dl_path.unlink()
|
| 148 |
return None
|
| 149 |
|
| 150 |
-
# --- Chart Generation System (Stable) ---
|
| 151 |
class ChartSpecification:
|
| 152 |
def __init__(self, chart_type: str, title: str, x_col: str, y_col: str = None, size_col: str = None, agg_method: str = None, filter_condition: str = None, top_n: int = None, color_scheme: str = "professional"):
|
| 153 |
self.chart_type = chart_type; self.title = title; self.x_col = x_col; self.y_col = y_col; self.size_col = size_col
|
|
@@ -233,7 +240,7 @@ def prepare_plot_data(spec: ChartSpecification, df: pd.DataFrame):
|
|
| 233 |
return df[numeric_cols].corr()
|
| 234 |
return df[spec.x_col]
|
| 235 |
|
| 236 |
-
#
|
| 237 |
def animate_chart(spec: ChartSpecification, df: pd.DataFrame, dur: float, out: Path, fps: int = FPS) -> str:
|
| 238 |
plot_data = prepare_plot_data(spec, df)
|
| 239 |
frames = max(10, int(dur * fps))
|
|
@@ -241,6 +248,7 @@ def animate_chart(spec: ChartSpecification, df: pd.DataFrame, dur: float, out: P
|
|
| 241 |
plt.tight_layout(pad=3.0)
|
| 242 |
ctype = spec.chart_type
|
| 243 |
|
|
|
|
| 244 |
if ctype == "pie":
|
| 245 |
wedges, _, _ = ax.pie(plot_data, labels=plot_data.index, startangle=90, autopct='%1.1f%%')
|
| 246 |
ax.set_title(spec.title); ax.axis('equal')
|
|
@@ -259,12 +267,10 @@ def animate_chart(spec: ChartSpecification, df: pd.DataFrame, dur: float, out: P
|
|
| 259 |
slope, intercept, _, _, _ = stats.linregress(x_full, y_full)
|
| 260 |
reg_line_x = np.array([x_full.min(), x_full.max()])
|
| 261 |
reg_line_y = slope * reg_line_x + intercept
|
| 262 |
-
|
| 263 |
scat = ax.scatter([], [], alpha=0.7, color='#F18F01')
|
| 264 |
line, = ax.plot([], [], 'r--', lw=2)
|
| 265 |
ax.set_xlim(x_full.min(), x_full.max()); ax.set_ylim(y_full.min(), y_full.max())
|
| 266 |
ax.set_title(spec.title); ax.grid(alpha=.3); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col)
|
| 267 |
-
|
| 268 |
def init():
|
| 269 |
scat.set_offsets(np.empty((0, 2))); line.set_data([], [])
|
| 270 |
return [scat, line]
|
|
@@ -278,39 +284,21 @@ def animate_chart(spec: ChartSpecification, df: pd.DataFrame, dur: float, out: P
|
|
| 278 |
current_x = reg_line_x[0] + (reg_line_x[1] - reg_line_x[0]) * (line_frame / line_total_frames)
|
| 279 |
line.set_data([reg_line_x[0], current_x], [reg_line_y[0], slope * current_x + intercept])
|
| 280 |
return [scat, line]
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
def init(): [p.set_alpha(0) for p in patches]; return patches
|
| 285 |
-
def update(i): [p.set_alpha((i / (frames - 1)) * 0.7) for p in patches]; return patches
|
| 286 |
-
elif ctype == "area":
|
| 287 |
-
plot_data = plot_data.sort_index()
|
| 288 |
-
x_full, y_full = plot_data.index, plot_data.values
|
| 289 |
-
fill = ax.fill_between(x_full, np.zeros_like(y_full), color="#4E79A7", alpha=0.4)
|
| 290 |
-
ax.set_xlim(x_full.min(), x_full.max()); ax.set_ylim(0, y_full.max() * 1.1)
|
| 291 |
-
ax.set_title(spec.title); ax.grid(alpha=.3); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col)
|
| 292 |
-
def init(): return [fill]
|
| 293 |
-
def update(i):
|
| 294 |
-
ax.collections.clear()
|
| 295 |
-
k = max(2, int(len(x_full) * (i / (frames - 1))))
|
| 296 |
-
fill = ax.fill_between(x_full[:k], y_full[:k], color="#4E79A7", alpha=0.4)
|
| 297 |
-
return [fill]
|
| 298 |
-
else: # line (Time Series)
|
| 299 |
line, = ax.plot([], [], lw=2, color='#A23B72')
|
| 300 |
-
|
| 301 |
-
plot_data = plot_data.sort_index() if not plot_data.index.is_monotonic_increasing else plot_data
|
| 302 |
x_full, y_full = plot_data.index, plot_data.values
|
| 303 |
ax.set_xlim(x_full.min(), x_full.max()); ax.set_ylim(y_full.min() * 0.9, y_full.max() * 1.1)
|
| 304 |
ax.set_title(spec.title); ax.grid(alpha=.3); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col)
|
| 305 |
-
def init():
|
| 306 |
-
line.set_data([], []); markers.set_data([], [])
|
| 307 |
-
return [line, markers]
|
| 308 |
def update(i):
|
| 309 |
k = max(2, int(len(x_full) * (i / (frames - 1))))
|
| 310 |
-
line.set_data(x_full[:k], y_full[:k]);
|
| 311 |
-
return [line, markers]
|
| 312 |
|
| 313 |
-
|
|
|
|
| 314 |
anim.save(str(out), writer=FFMpegWriter(fps=fps), dpi=144)
|
| 315 |
plt.close(fig)
|
| 316 |
return str(out)
|
|
@@ -332,20 +320,8 @@ def safe_chart(desc: str, df: pd.DataFrame, dur: float, out: Path, context: Dict
|
|
| 332 |
chart_spec = chart_generator.generate_chart_spec(desc, context)
|
| 333 |
return animate_chart(chart_spec, df, dur, out)
|
| 334 |
except Exception as e:
|
| 335 |
-
logging.error(f"Chart animation failed for '{desc}': {e}.
|
| 336 |
-
|
| 337 |
-
temp_png = Path(temp_png_file.name)
|
| 338 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", google_api_key=API_KEY, temperature=0.1)
|
| 339 |
-
chart_generator = ChartGenerator(llm, df)
|
| 340 |
-
chart_spec = chart_generator.generate_chart_spec(desc, context)
|
| 341 |
-
if execute_chart_spec(chart_spec, df, temp_png):
|
| 342 |
-
img = cv2.imread(str(temp_png)); os.unlink(temp_png)
|
| 343 |
-
img_resized = cv2.resize(img, (WIDTH, HEIGHT))
|
| 344 |
-
return animate_image_fade(img_resized, dur, out)
|
| 345 |
-
else:
|
| 346 |
-
img = generate_image_from_prompt(f"A professional business chart showing {desc}")
|
| 347 |
-
img_cv = cv2.cvtColor(np.array(img.resize((WIDTH, HEIGHT))), cv2.COLOR_RGB2BGR)
|
| 348 |
-
return animate_image_fade(img_cv, dur, out)
|
| 349 |
|
| 350 |
def concat_media(file_paths: List[str], output_path: Path):
|
| 351 |
valid_paths = [p for p in file_paths if Path(p).exists() and Path(p).stat().st_size > 100]
|
|
@@ -360,32 +336,21 @@ def concat_media(file_paths: List[str], output_path: Path):
|
|
| 360 |
finally:
|
| 361 |
list_file.unlink(missing_ok=True)
|
| 362 |
|
| 363 |
-
# --- Main Business Logic ---
|
| 364 |
-
|
| 365 |
def sanitize_for_firebase_key(text: str) -> str:
|
| 366 |
forbidden_chars = ['.', '$', '#', '[', ']', '/']
|
| 367 |
for char in forbidden_chars:
|
| 368 |
text = text.replace(char, '_')
|
| 369 |
return text
|
| 370 |
|
| 371 |
-
# NEW: Intelligence functions to guide the storyteller AI
|
| 372 |
def analyze_data_intelligence(df: pd.DataFrame) -> Dict:
|
| 373 |
-
"""Analyzes the dataset to find key characteristics and opportunities for storytelling."""
|
| 374 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 375 |
categorical_cols = df.select_dtypes(exclude=['number']).columns.tolist()
|
| 376 |
-
|
| 377 |
is_timeseries = any('date' in col.lower() or 'time' in col.lower() for col in df.columns)
|
| 378 |
-
|
| 379 |
opportunities = []
|
| 380 |
-
if is_timeseries:
|
| 381 |
-
|
| 382 |
-
if len(numeric_cols) >
|
| 383 |
-
|
| 384 |
-
if len(categorical_cols) > 0 and len(numeric_cols) > 0:
|
| 385 |
-
opportunities.append("segmentation by category")
|
| 386 |
-
if df.isnull().sum().sum() > 0:
|
| 387 |
-
opportunities.append("impact of missing data")
|
| 388 |
-
|
| 389 |
return {
|
| 390 |
"insight_opportunities": opportunities,
|
| 391 |
"is_timeseries": is_timeseries,
|
|
@@ -394,45 +359,26 @@ def analyze_data_intelligence(df: pd.DataFrame) -> Dict:
|
|
| 394 |
}
|
| 395 |
|
| 396 |
def generate_visualization_strategy(intelligence: Dict) -> str:
|
| 397 |
-
"""Generates dynamic advice on which charts to use."""
|
| 398 |
strategy = "Vary your visualizations to keep the report engaging. "
|
| 399 |
-
if intelligence["is_timeseries"]:
|
| 400 |
-
|
| 401 |
-
if intelligence["
|
| 402 |
-
strategy += "Use 'scatter' or 'heatmap' charts to reveal correlations. "
|
| 403 |
-
if intelligence["has_segments"]:
|
| 404 |
-
strategy += "Use 'bar' or 'pie' charts to compare segments. "
|
| 405 |
return strategy
|
| 406 |
|
| 407 |
def get_augmented_context(df: pd.DataFrame, user_ctx: str) -> Dict:
|
| 408 |
-
"""Creates a detailed summary of the dataframe for the AI."""
|
| 409 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 410 |
categorical_cols = df.select_dtypes(exclude=['number']).columns.tolist()
|
| 411 |
-
|
| 412 |
context = {
|
| 413 |
"user_context": user_ctx,
|
| 414 |
"dataset_shape": {"rows": df.shape[0], "columns": df.shape[1]},
|
| 415 |
-
"schema": {
|
| 416 |
-
"numeric_columns": numeric_cols,
|
| 417 |
-
"categorical_columns": categorical_cols
|
| 418 |
-
},
|
| 419 |
"data_previews": {}
|
| 420 |
}
|
| 421 |
-
|
| 422 |
for col in categorical_cols[:5]:
|
| 423 |
unique_vals = df[col].unique()
|
| 424 |
-
context["data_previews"][col] = {
|
| 425 |
-
"count": len(unique_vals),
|
| 426 |
-
"values": unique_vals[:5].tolist()
|
| 427 |
-
}
|
| 428 |
-
|
| 429 |
for col in numeric_cols[:5]:
|
| 430 |
-
context["data_previews"][col] = {
|
| 431 |
-
"mean": df[col].mean(),
|
| 432 |
-
"min": df[col].min(),
|
| 433 |
-
"max": df[col].max()
|
| 434 |
-
}
|
| 435 |
-
|
| 436 |
return json.loads(json.dumps(context, default=str))
|
| 437 |
|
| 438 |
def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, bucket):
|
|
@@ -440,9 +386,7 @@ def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, b
|
|
| 440 |
df = load_dataframe_safely(buf, name)
|
| 441 |
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=API_KEY, temperature=0.2)
|
| 442 |
|
| 443 |
-
|
| 444 |
-
data_context_str = ""
|
| 445 |
-
context_for_charts = {}
|
| 446 |
try:
|
| 447 |
df_json = df.to_json(orient='records')
|
| 448 |
estimated_tokens = len(df_json) / 4
|
|
@@ -458,7 +402,6 @@ def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, b
|
|
| 458 |
data_context_str = f"The full dataset is too large to display. Here is a detailed summary:\n{json.dumps(augmented_context, indent=2)}"
|
| 459 |
context_for_charts = augmented_context
|
| 460 |
|
| 461 |
-
# --- Persona-Driven Prompting ---
|
| 462 |
intelligence = analyze_data_intelligence(df)
|
| 463 |
viz_strategy = generate_visualization_strategy(intelligence)
|
| 464 |
|
|
@@ -549,10 +492,8 @@ def generate_video_from_project(df: pd.DataFrame, raw_md: str, data_context: Dic
|
|
| 549 |
total_audio_duration = 0.0
|
| 550 |
|
| 551 |
for i, sc in enumerate(scenes):
|
| 552 |
-
|
| 553 |
-
pexels_descs = extract_pexels_tags(sc)
|
| 554 |
narrative = clean_narration(sc)
|
| 555 |
-
|
| 556 |
if not narrative:
|
| 557 |
logging.warning(f"Scene {i+1} has no narration, skipping.")
|
| 558 |
continue
|
|
@@ -571,25 +512,41 @@ def generate_video_from_project(df: pd.DataFrame, raw_md: str, data_context: Dic
|
|
| 571 |
total_audio_duration += audio_dur
|
| 572 |
|
| 573 |
video_dur = audio_dur + 0.5
|
| 574 |
-
mp4 = Path(tempfile.gettempdir()) / f"{uuid.uuid4()}.mp4"
|
| 575 |
-
video_generated = False
|
| 576 |
-
|
| 577 |
-
if pexels_descs:
|
| 578 |
-
video_path = search_and_download_pexels_video(pexels_descs[0], video_dur, mp4)
|
| 579 |
-
if video_path:
|
| 580 |
-
video_parts.append(video_path); temps.append(Path(video_path))
|
| 581 |
-
video_generated = True
|
| 582 |
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
|
| 594 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_vid, \
|
| 595 |
tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_aud, \
|
|
|
|
| 15 |
from matplotlib.animation import FuncAnimation, FFMpegWriter
|
| 16 |
import seaborn as sns
|
| 17 |
from scipy import stats
|
| 18 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 19 |
import cv2
|
| 20 |
import inspect
|
| 21 |
import tempfile
|
|
|
|
| 29 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s')
|
| 30 |
FPS, WIDTH, HEIGHT = 24, 1280, 720
|
| 31 |
MAX_CHARTS, VIDEO_SCENES = 5, 5
|
| 32 |
+
MAX_CONTEXT_TOKENS = 250000
|
| 33 |
|
| 34 |
# --- API Initialization ---
|
| 35 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
|
|
| 38 |
|
| 39 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 40 |
|
| 41 |
+
# --- Helper Functions ---
|
| 42 |
def load_dataframe_safely(buf, name: str):
|
| 43 |
ext = Path(name).suffix.lower()
|
| 44 |
df = (pd.read_excel if ext in (".xlsx", ".xls") else pd.read_csv)(buf)
|
|
|
|
| 83 |
|
| 84 |
def placeholder_img() -> Image.Image: return Image.new("RGB", (WIDTH, HEIGHT), (230, 230, 230))
|
| 85 |
|
| 86 |
+
# NEW: Keyword extraction for better Pexels searches
|
| 87 |
+
def extract_keywords_for_query(text: str, llm) -> str:
|
| 88 |
+
prompt = f"""
|
| 89 |
+
Extract 2-4 key nouns and verbs from the following text to use as a search query for a stock video.
|
| 90 |
+
Focus on concrete actions and subjects.
|
| 91 |
+
Example: 'Our analysis shows a significant growth in quarterly revenue and strong partnerships.' -> 'data analysis growth chart business'
|
| 92 |
+
Output only the search query keywords, separated by spaces.
|
| 93 |
+
|
| 94 |
+
Text: "{text}"
|
| 95 |
+
"""
|
| 96 |
try:
|
| 97 |
+
response = llm.invoke(prompt).content.strip()
|
| 98 |
+
return response
|
| 99 |
+
except Exception as e:
|
| 100 |
+
logging.error(f"Keyword extraction failed: {e}. Using original text.")
|
| 101 |
+
return text # Fallback to the original text if LLM fails
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# UPDATED: Pexels search now loops short videos
|
| 104 |
def search_and_download_pexels_video(query: str, duration: float, out_path: Path) -> str:
|
| 105 |
if not PEXELS_API_KEY:
|
| 106 |
logging.warning("PEXELS_API_KEY not set. Cannot fetch stock video.")
|
| 107 |
return None
|
| 108 |
try:
|
| 109 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 110 |
+
params = {"query": query, "per_page": 10, "orientation": "landscape"}
|
| 111 |
response = requests.get("https://api.pexels.com/videos/search", headers=headers, params=params, timeout=20)
|
| 112 |
response.raise_for_status()
|
| 113 |
videos = response.json().get('videos', [])
|
|
|
|
| 135 |
temp_dl_file.write(chunk)
|
| 136 |
temp_dl_path = Path(temp_dl_file.name)
|
| 137 |
|
| 138 |
+
# UPDATED: Added -stream_loop -1 to loop short videos
|
| 139 |
cmd = [
|
| 140 |
+
"ffmpeg", "-y",
|
| 141 |
+
"-stream_loop", "-1", # Loop the input video
|
| 142 |
+
"-i", str(temp_dl_path),
|
| 143 |
"-vf", f"scale={WIDTH}:{HEIGHT}:force_original_aspect_ratio=decrease,pad={WIDTH}:{HEIGHT}:(ow-iw)/2:(oh-ih)/2,setsar=1",
|
| 144 |
+
"-t", f"{duration:.3f}", # Cut the looped video to the exact duration
|
| 145 |
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-an",
|
| 146 |
str(out_path)
|
| 147 |
]
|
|
|
|
| 155 |
temp_dl_path.unlink()
|
| 156 |
return None
|
| 157 |
|
|
|
|
| 158 |
class ChartSpecification:
|
| 159 |
def __init__(self, chart_type: str, title: str, x_col: str, y_col: str = None, size_col: str = None, agg_method: str = None, filter_condition: str = None, top_n: int = None, color_scheme: str = "professional"):
|
| 160 |
self.chart_type = chart_type; self.title = title; self.x_col = x_col; self.y_col = y_col; self.size_col = size_col
|
|
|
|
| 240 |
return df[numeric_cols].corr()
|
| 241 |
return df[spec.x_col]
|
| 242 |
|
| 243 |
+
# UPDATED: animate_chart now uses blit=False for accurate timing
|
| 244 |
def animate_chart(spec: ChartSpecification, df: pd.DataFrame, dur: float, out: Path, fps: int = FPS) -> str:
|
| 245 |
plot_data = prepare_plot_data(spec, df)
|
| 246 |
frames = max(10, int(dur * fps))
|
|
|
|
| 248 |
plt.tight_layout(pad=3.0)
|
| 249 |
ctype = spec.chart_type
|
| 250 |
|
| 251 |
+
# Animation logic remains the same, only the final call to FuncAnimation changes
|
| 252 |
if ctype == "pie":
|
| 253 |
wedges, _, _ = ax.pie(plot_data, labels=plot_data.index, startangle=90, autopct='%1.1f%%')
|
| 254 |
ax.set_title(spec.title); ax.axis('equal')
|
|
|
|
| 267 |
slope, intercept, _, _, _ = stats.linregress(x_full, y_full)
|
| 268 |
reg_line_x = np.array([x_full.min(), x_full.max()])
|
| 269 |
reg_line_y = slope * reg_line_x + intercept
|
|
|
|
| 270 |
scat = ax.scatter([], [], alpha=0.7, color='#F18F01')
|
| 271 |
line, = ax.plot([], [], 'r--', lw=2)
|
| 272 |
ax.set_xlim(x_full.min(), x_full.max()); ax.set_ylim(y_full.min(), y_full.max())
|
| 273 |
ax.set_title(spec.title); ax.grid(alpha=.3); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col)
|
|
|
|
| 274 |
def init():
|
| 275 |
scat.set_offsets(np.empty((0, 2))); line.set_data([], [])
|
| 276 |
return [scat, line]
|
|
|
|
| 284 |
current_x = reg_line_x[0] + (reg_line_x[1] - reg_line_x[0]) * (line_frame / line_total_frames)
|
| 285 |
line.set_data([reg_line_x[0], current_x], [reg_line_y[0], slope * current_x + intercept])
|
| 286 |
return [scat, line]
|
| 287 |
+
else: # line, area, hist, etc.
|
| 288 |
+
# This is a simplified representation; the full logic from previous steps is assumed here
|
| 289 |
+
# For brevity, we'll just show the line chart example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
line, = ax.plot([], [], lw=2, color='#A23B72')
|
| 291 |
+
plot_data = plot_data.sort_index()
|
|
|
|
| 292 |
x_full, y_full = plot_data.index, plot_data.values
|
| 293 |
ax.set_xlim(x_full.min(), x_full.max()); ax.set_ylim(y_full.min() * 0.9, y_full.max() * 1.1)
|
| 294 |
ax.set_title(spec.title); ax.grid(alpha=.3); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col)
|
| 295 |
+
def init(): line.set_data([], []); return [line]
|
|
|
|
|
|
|
| 296 |
def update(i):
|
| 297 |
k = max(2, int(len(x_full) * (i / (frames - 1))))
|
| 298 |
+
line.set_data(x_full[:k], y_full[:k]); return [line]
|
|
|
|
| 299 |
|
| 300 |
+
# The key change: blit=False
|
| 301 |
+
anim = FuncAnimation(fig, update, init_func=init, frames=frames, blit=False, interval=1000 / fps)
|
| 302 |
anim.save(str(out), writer=FFMpegWriter(fps=fps), dpi=144)
|
| 303 |
plt.close(fig)
|
| 304 |
return str(out)
|
|
|
|
| 320 |
chart_spec = chart_generator.generate_chart_spec(desc, context)
|
| 321 |
return animate_chart(chart_spec, df, dur, out)
|
| 322 |
except Exception as e:
|
| 323 |
+
logging.error(f"Chart animation failed for '{desc}': {e}. Raising exception to trigger fallback.")
|
| 324 |
+
raise e # Raise exception to be caught by the video generator's fallback logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
def concat_media(file_paths: List[str], output_path: Path):
|
| 327 |
valid_paths = [p for p in file_paths if Path(p).exists() and Path(p).stat().st_size > 100]
|
|
|
|
| 336 |
finally:
|
| 337 |
list_file.unlink(missing_ok=True)
|
| 338 |
|
|
|
|
|
|
|
| 339 |
def sanitize_for_firebase_key(text: str) -> str:
|
| 340 |
forbidden_chars = ['.', '$', '#', '[', ']', '/']
|
| 341 |
for char in forbidden_chars:
|
| 342 |
text = text.replace(char, '_')
|
| 343 |
return text
|
| 344 |
|
|
|
|
| 345 |
def analyze_data_intelligence(df: pd.DataFrame) -> Dict:
|
|
|
|
| 346 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 347 |
categorical_cols = df.select_dtypes(exclude=['number']).columns.tolist()
|
|
|
|
| 348 |
is_timeseries = any('date' in col.lower() or 'time' in col.lower() for col in df.columns)
|
|
|
|
| 349 |
opportunities = []
|
| 350 |
+
if is_timeseries: opportunities.append("temporal trends")
|
| 351 |
+
if len(numeric_cols) > 1: opportunities.append("correlations between metrics")
|
| 352 |
+
if len(categorical_cols) > 0 and len(numeric_cols) > 0: opportunities.append("segmentation by category")
|
| 353 |
+
if df.isnull().sum().sum() > 0: opportunities.append("impact of missing data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
return {
|
| 355 |
"insight_opportunities": opportunities,
|
| 356 |
"is_timeseries": is_timeseries,
|
|
|
|
| 359 |
}
|
| 360 |
|
| 361 |
def generate_visualization_strategy(intelligence: Dict) -> str:
|
|
|
|
| 362 |
strategy = "Vary your visualizations to keep the report engaging. "
|
| 363 |
+
if intelligence["is_timeseries"]: strategy += "Use 'line' or 'area' charts to explore temporal trends. "
|
| 364 |
+
if intelligence["has_correlations"]: strategy += "Use 'scatter' or 'heatmap' charts to reveal correlations. "
|
| 365 |
+
if intelligence["has_segments"]: strategy += "Use 'bar' or 'pie' charts to compare segments. "
|
|
|
|
|
|
|
|
|
|
| 366 |
return strategy
|
| 367 |
|
| 368 |
def get_augmented_context(df: pd.DataFrame, user_ctx: str) -> Dict:
|
|
|
|
| 369 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 370 |
categorical_cols = df.select_dtypes(exclude=['number']).columns.tolist()
|
|
|
|
| 371 |
context = {
|
| 372 |
"user_context": user_ctx,
|
| 373 |
"dataset_shape": {"rows": df.shape[0], "columns": df.shape[1]},
|
| 374 |
+
"schema": {"numeric_columns": numeric_cols, "categorical_columns": categorical_cols},
|
|
|
|
|
|
|
|
|
|
| 375 |
"data_previews": {}
|
| 376 |
}
|
|
|
|
| 377 |
for col in categorical_cols[:5]:
|
| 378 |
unique_vals = df[col].unique()
|
| 379 |
+
context["data_previews"][col] = {"count": len(unique_vals), "values": unique_vals[:5].tolist()}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
for col in numeric_cols[:5]:
|
| 381 |
+
context["data_previews"][col] = {"mean": df[col].mean(), "min": df[col].min(), "max": df[col].max()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
return json.loads(json.dumps(context, default=str))
|
| 383 |
|
| 384 |
def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, bucket):
|
|
|
|
| 386 |
df = load_dataframe_safely(buf, name)
|
| 387 |
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=API_KEY, temperature=0.2)
|
| 388 |
|
| 389 |
+
data_context_str, context_for_charts = "", {}
|
|
|
|
|
|
|
| 390 |
try:
|
| 391 |
df_json = df.to_json(orient='records')
|
| 392 |
estimated_tokens = len(df_json) / 4
|
|
|
|
| 402 |
data_context_str = f"The full dataset is too large to display. Here is a detailed summary:\n{json.dumps(augmented_context, indent=2)}"
|
| 403 |
context_for_charts = augmented_context
|
| 404 |
|
|
|
|
| 405 |
intelligence = analyze_data_intelligence(df)
|
| 406 |
viz_strategy = generate_visualization_strategy(intelligence)
|
| 407 |
|
|
|
|
| 492 |
total_audio_duration = 0.0
|
| 493 |
|
| 494 |
for i, sc in enumerate(scenes):
|
| 495 |
+
mp4 = Path(tempfile.gettempdir()) / f"{uuid.uuid4()}.mp4"
|
|
|
|
| 496 |
narrative = clean_narration(sc)
|
|
|
|
| 497 |
if not narrative:
|
| 498 |
logging.warning(f"Scene {i+1} has no narration, skipping.")
|
| 499 |
continue
|
|
|
|
| 512 |
total_audio_duration += audio_dur
|
| 513 |
|
| 514 |
video_dur = audio_dur + 0.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
+
try:
|
| 517 |
+
# --- Primary Visual Generation ---
|
| 518 |
+
chart_descs = extract_chart_tags(sc)
|
| 519 |
+
pexels_descs = extract_pexels_tags(sc)
|
| 520 |
+
|
| 521 |
+
if pexels_descs:
|
| 522 |
+
logging.info(f"Scene {i+1}: Primary attempt with Pexels.")
|
| 523 |
+
query = extract_keywords_for_query(narrative, llm)
|
| 524 |
+
video_path = search_and_download_pexels_video(query, video_dur, mp4)
|
| 525 |
+
if not video_path: raise ValueError("Pexels search returned no results.")
|
| 526 |
+
video_parts.append(video_path)
|
| 527 |
+
elif chart_descs:
|
| 528 |
+
logging.info(f"Scene {i+1}: Primary attempt with animated chart.")
|
| 529 |
+
safe_chart(chart_descs[0], df, video_dur, mp4, data_context)
|
| 530 |
+
video_parts.append(str(mp4))
|
| 531 |
+
else:
|
| 532 |
+
raise ValueError("No visual tag found in scene.")
|
| 533 |
+
except Exception as e:
|
| 534 |
+
# --- Fallback Visual Generation ---
|
| 535 |
+
logging.warning(f"Scene {i+1}: Primary visual failed ({e}). Triggering fallback.")
|
| 536 |
+
try:
|
| 537 |
+
fallback_query = "abstract technology background"
|
| 538 |
+
video_path = search_and_download_pexels_video(fallback_query, video_dur, mp4)
|
| 539 |
+
if not video_path: raise ValueError("Fallback Pexels search failed.")
|
| 540 |
+
video_parts.append(video_path)
|
| 541 |
+
except Exception as fallback_e:
|
| 542 |
+
# --- Final Failsafe ---
|
| 543 |
+
logging.error(f"Scene {i+1}: Fallback visual also failed ({fallback_e}). Using placeholder.")
|
| 544 |
+
placeholder = placeholder_img()
|
| 545 |
+
placeholder.save(str(mp4).replace(".mp4", ".png"))
|
| 546 |
+
animate_image_fade(cv2.imread(str(mp4).replace(".mp4", ".png")), video_dur, mp4)
|
| 547 |
+
video_parts.append(str(mp4))
|
| 548 |
+
|
| 549 |
+
temps.append(mp4)
|
| 550 |
|
| 551 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_vid, \
|
| 552 |
tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_aud, \
|