Update app.py
Browse files15 unmodified lines161718192021141 unmodified lines163164165166167168169170171200 unmodified lines372373374375376377378379380381382383384385386387388389399 unmodified lines78979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582615 unmodified linesβ’ π¬ Optional outro video (plays after the main reel)β’ π API keys can be set as Hugging Face secrets (GROQ_API_KEY, PEXELS_API_KEY)β’ πΌοΈ Logo and outro are autoβloaded from the `files/` folder if present (logo.png, exit.mp4)"""# ββ unbuffered stdout so container logs appear in real-time ββββββββββββββββββ141 unmodified lines"3. The core issue explained concisely (the key detail that drives the conflict).\n""4. The stakes for the industry or investors.\n""5. A rhetorical question that builds anticipation.\n""6. A clear callβtoβaction that includes the source domain (e.g., 'Full details live now at chanstreet.io').\n\n""Write in a conversational, authoritative newscaster style that will sound natural when spoken aloud.\n""Write exactly {n} lines, each a single, punchy sentence (8β15 words) overall length should be 105 β 115 words or 580 β 650 characters. The last line MUST be the callβtoβaction using the source domain from the content if available; otherwise use a generic CTA.\n""Output ONLY the {n} lines, one per line, no extra text."),}200 unmodified linesreturn None# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# LOGO HELPER# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef load_logo(logo_path: str | None, target_w: int = 180) -> Image.Image | None:if not logo_path:return Nonetry:logo = Image.open(logo_path).convert("RGBA")ratio = target_w / logo.widthlogo = logo.resize((target_w, int(logo.height * ratio)), Image.LANCZOS)return logoexcept Exception as e:print(f"[logo] {e}")return Nonedef paste_logo(base: Image.Image, logo: Image.Image | None, position: str = "top-right") -> Image.Image:399 unmodified lineslogo_path = logo_file if isinstance(logo_file, str) else (logo_file.name if logo_file else None)main_reel_path = create_reel(sentences, audio_path, bg_video, logo_path, logo_pos, accent_hex, show_caption_bg)# ββ Add outro video if provided ββββββββββββββββββββββββββββββββββββββif outro_video:progress(0.85, desc="β Adding outro videoβ¦")import moviepy.editor as mpeoutro_path = outro_video if isinstance(outro_video, str) else outro_video.nameoutro_clip = mpe.VideoFileClip(outro_path)# Resize outro to match main reel dimensionsW, H = VIDEO_W, VIDEO_Hif outro_clip.w / outro_clip.h > W / H:outro_clip = outro_clip.resize(height=H)else:outro_clip = outro_clip.resize(width=W)if outro_clip.w > W:outro_clip = outro_clip.crop(x_center=outro_clip.w / 2, width=W)if outro_clip.h > H:outro_clip = outro_clip.crop(y_center=outro_clip.h / 2, height=H)# Load main reel and concatenatemain_clip = mpe.VideoFileClip(main_reel_path)final_clip = mpe.concatenate_videoclips([main_clip, outro_clip], method="compose")final_path = tempfile.mktemp(suffix=".mp4")final_clip.write_videofile(final_path, codec="libx264", audio_codec="aac",fps=FPS, preset="ultrafast", threads=4, logger=None)# Clean up intermediate fileos.unlink(main_reel_path)main_reel_path = final_pathprogress(0.95, desc="β Outro added")script_md = "\n\n".join(f"**{i+1}.** {s}" for i, s in enumerate(sentences))progress(1.0, desc="β Video ready!")return main_reel_path, f"## β Reel Ready!\n\n**Script:**\n\n{script_md}"except Exception as e:import traceback15 unmodified lines16171819202122141 unmodified lines164165166167168169170171172200 unmodified lines373374375376377378379380381382383384385386387388389390391392393394399 unmodified lines79479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185215 unmodified linesβ’ π¬ Optional outro video (plays after the main reel)β’ π API keys can be set as Hugging Face secrets (GROQ_API_KEY, PEXELS_API_KEY)β’ πΌοΈ Logo and outro are autoβloaded from the `files/` folder if present (logo.png, exit.mp4)β’ π‘οΈ Robust error handling: corrupted or missing outro/logo won't crash the app"""# ββ unbuffered stdout so container logs appear in real-time ββββββββββββββββββ141 unmodified lines"3. The core issue explained concisely (the key detail that drives the conflict).\n""4. The stakes for the industry or investors.\n""5. A rhetorical question that builds anticipation.\n""6. A clear callβtoβaction that includes the source domain (e.g., 'Link in bio β check it before the deadline hits').\n\n""Write in a conversational, authoritative newscaster style that will sound natural when spoken aloud.\n""Write exactly {n} lines, each a single, punchy sentence (8β15 words). The last line MUST be the callβtoβaction using the source domain from the content if available; otherwise use a generic CTA.\n""Output ONLY the {n} lines, one per line, no extra text."),}200 unmodified linesreturn None# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# LOGO HELPER (with error handling)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef load_logo(logo_path: str | None, target_w: int = 180) -> Image.Image | None:if not logo_path:return Nonetry:# Check if file exists and is readableif not os.path.isfile(logo_path):print(f"[logo] File not found: {logo_path}")return Nonelogo = Image.open(logo_path).convert("RGBA")ratio = target_w / logo.widthlogo = logo.resize((target_w, int(logo.height * ratio)), Image.LANCZOS)return logoexcept Exception as e:print(f"[logo] Error loading logo: {e}")return Nonedef paste_logo(base: Image.Image, logo: Image.Image | None, position: str = "top-right") -> Image.Image:399 unmodified lineslogo_path = logo_file if isinstance(logo_file, str) else (logo_file.name if logo_file else None)main_reel_path = create_reel(sentences, audio_path, bg_video, logo_path, logo_pos, accent_hex, show_caption_bg)# ββ Add outro video if provided (with robust error handling) ββββββββif outro_video:progress(0.85, desc="β Adding outro videoβ¦")import moviepy.editor as mpeoutro_path = outro_video if isinstance(outro_video, str) else outro_video.nametry:# Check if file exists and is readableif not os.path.isfile(outro_path):print(f"[outro] File not found: {outro_path}")raise FileNotFoundError(f"Outro file not found: {outro_path}")# Try to open with moviepyoutro_clip = mpe.VideoFileClip(outro_path)print(f"[outro] Successfully loaded {outro_path}")# Resize outro to match main reel dimensionsW, H = VIDEO_W, VIDEO_Hif outro_clip.w / outro_clip.h > W / H:outro_clip = outro_clip.resize(height=H)else:outro_clip = outro_clip.resize(width=W)if outro_clip.w > W:outro_clip = outro_clip.crop(x_center=outro_clip.w / 2, width=W)if outro_clip.h > H:outro_clip = outro_clip.crop(y_center=outro_clip.h / 2, height=H)# Load main reel and concatenatemain_clip = mpe.VideoFileClip(main_reel_path)final_clip = mpe.concatenate_videoclips([main_clip, outro_clip], method="compose")final_path = tempfile.mktemp(suffix=".mp4")final_clip.write_videofile(final_path, codec="libx264", audio_codec="aac",fps=FPS, preset="ultrafast", threads=4, logger=None)# Clean up intermediate fileos.unlink(main_reel_path)main_reel_path = final_pathprogress(0.95, desc="β Outro added")except Exception as e:print(f"[outro] Error processing outro video: {e}")# Continue without outro, but show a warning in the output (optional)# We'll just log it and proceed with main_reel_path unchangedprogress(0.95, desc="β οΈ Outro skipped (file error)")# Optionally add a warning to the status message lateroutro_error = Trueelse:outro_error = Falseelse:outro_error = Falsescript_md = "\n\n".join(f"**{i+1}.** {s}" for i, s in enumerate(sentences))progress(1.0, desc="β Video ready!")status_msg = f"## β Reel Ready!\n\n**Script:**\n\n{script_md}"if outro_error:status_msg += "\n\nβ οΈ **Note:** Outro video was skipped because it could not be read (file may be corrupted). Please reβupload the outro video if needed."return main_reel_path, status_msgexcept Exception as e:import traceback
app.py+54-28