| import threading |
| import uuid |
| from queue import Queue |
| import traceback |
| import time |
|
|
| from .logger import logger |
| from .validators import validate_video |
| from .transcription import transcribe_video |
| from .director import rewrite_script, viral_score |
| from .engagement import simulate_retention |
| from .platform import adapt_platform |
| from .persona import predict_audience |
| from .clipper import create_clip |
|
|
|
|
| |
| |
| |
|
|
| jobs = {} |
| queue = Queue() |
|
|
| WORKER_STARTED = False |
|
|
|
|
| |
| |
| |
|
|
| def create_job(video_path: str, webhook: str | None = None): |
|
|
| job_id = str(uuid.uuid4()) |
|
|
| jobs[job_id] = { |
| "id": job_id, |
| "status": "queued", |
| "stage": "waiting", |
| "progress": 0, |
|
|
| "video": video_path, |
|
|
| |
| "viral_score": None, |
| "persona": None, |
| "hook": None, |
| "platforms": [], |
| "strategy_summary": None, |
|
|
| "clips": [], |
| "webhook": webhook, |
| "error": None, |
| "created_at": time.time(), |
| } |
|
|
| queue.put(job_id) |
|
|
| logger.info(f"[V7] Director job queued: {job_id}") |
|
|
| return job_id |
|
|
|
|
| |
| |
| |
|
|
| def get_job(job_id: str): |
| return jobs.get(job_id) |
|
|
|
|
| |
| |
| |
|
|
| def update(job_id, **kwargs): |
| if job_id in jobs: |
| jobs[job_id].update(kwargs) |
|
|
|
|
| |
| |
| |
|
|
| def worker(): |
|
|
| logger.info("[V7] Autonomous Viral Director started") |
|
|
| while True: |
|
|
| job_id = queue.get() |
| job = jobs[job_id] |
|
|
| try: |
|
|
| |
| |
| |
| update(job_id, status="processing", stage="transcribing", progress=10) |
| words = transcribe_video(job["video"]) |
|
|
| |
| |
| |
| update(job_id, stage="rewriting narrative", progress=25) |
| script = rewrite_script(words) |
|
|
| |
| |
| |
| persona = predict_audience(words) |
|
|
| |
| |
| |
| update(job_id, stage="simulating audience", progress=45) |
| curve = simulate_retention(words) |
|
|
| v_score = viral_score(curve) |
|
|
| |
| |
| |
| update(job_id, stage="platform adaptation", progress=65) |
|
|
| tiktok = adapt_platform(script, "tiktok") |
| reels = adapt_platform(script, "reels") |
|
|
| platforms = ["tiktok", "reels"] |
|
|
| |
| |
| |
| update(job_id, stage="rendering final cut", progress=85) |
|
|
| clip = create_clip( |
| job["video"], |
| words[0]["start"], |
| words[-1]["end"], |
| 0 |
| ) |
|
|
| |
| |
| |
| update(job_id, |
| status="completed", |
| stage="director finished", |
| progress=100, |
|
|
| viral_score=v_score, |
| persona=persona, |
| hook=script["hook"], |
| platforms=platforms, |
|
|
| strategy_summary={ |
| "curve_peak": max(curve), |
| "avg_curve": sum(curve) / len(curve), |
| "decision": "auto-selected best full narrative cut" |
| }, |
|
|
| clips=[clip]) |
|
|
| logger.info(f"[V7] Director output complete: {job_id}") |
|
|
| except Exception as e: |
|
|
| logger.error(traceback.format_exc()) |
|
|
| update(job_id, |
| status="failed", |
| stage="error", |
| error=str(e)) |
|
|
| finally: |
| queue.task_done() |
|
|
|
|
| |
| |
| |
|
|
| def start_worker(): |
|
|
| global WORKER_STARTED |
|
|
| if WORKER_STARTED: |
| return |
|
|
| WORKER_STARTED = True |
|
|
| t = threading.Thread(target=worker, daemon=True) |
| t.start() |
|
|
| logger.info("[V7] Worker initialized") |