File size: 10,497 Bytes
1425afc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | from fastapi import FastAPI, UploadFile, File, Form, Request
from fastapi.responses import FileResponse, JSONResponse
from contextlib import asynccontextmanager
import os
import uuid
import asyncio
from pathlib import Path
import gradio as gr
# ==============================
# LOGGER + QUEUE
# ==============================
from utils.logger import logger
from utils.job_queue import start_worker, create_job, get_job
from ingestion.resolver import resolve_input
# ==============================
# AUTH SYSTEM (HARDENED IMPORT)
# ==============================
try:
from auth.routes import router as auth_router
from auth.database import Base, engine
AUTH_ENABLED = True
except Exception as e:
logger.error(f"[AUTH BOOT FAILED] {e}")
AUTH_ENABLED = False
# ==============================
# CORE PIPELINE
# ==============================
from utils.transcription import transcribe_video
from utils.srt import generate_srt
from utils.render import render_subtitles
from utils.highlights import detect_highlights
from utils.viral_scorer import score_clip
from utils.director import rewrite_script, viral_score
from utils.engagement import simulate_retention
from utils.platform import adapt_platform
from utils.persona import predict_audience
from utils.clipper import create_clips
from utils.autonomous_engine import run_autonomous_engine
# ==============================
# PUBLISHER
# ==============================
from publisher.publisher_ai import autonomous_loop
from publisher.scheduler_engine import init_scheduler
from publisher.platform_dispatcher import dispatch_publish
from publisher.bulk import execute as bulk_execute
from publisher.metadata_engine import generate_metadata
from publisher.thumbnail_engine import generate_thumbnail
# ==============================
# SAFE bcrypt SHIELD (DO NOT FAIL BOOT)
# ==============================
try:
import bcrypt
except Exception as e:
logger.warning(f"[bcrypt warning ignored] {e}")
# ==============================
# INIT
# ==============================
BASE_DIR = Path(__file__).resolve().parent
UPLOAD_DIR = str(BASE_DIR / "jobs")
os.makedirs(UPLOAD_DIR, exist_ok=True)
# =========================================================
# SAFE DB INITIALIZATION
# =========================================================
def init_database_safe():
if not AUTH_ENABLED:
logger.warning("Auth disabled - skipping DB init")
return
try:
Base.metadata.create_all(bind=engine)
logger.info("Database initialized successfully")
except Exception as e:
logger.error(f"Database init failed (non-fatal): {e}")
# =========================================================
# LIFECYCLE
# =========================================================
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Starting Basyx Whisper V10.1")
# DB init (NON-FATAL)
init_database_safe()
# workers (must not block boot)
try:
start_worker()
except Exception as e:
logger.error(f"Worker failed: {e}")
# scheduler
try:
init_scheduler()
except Exception as e:
logger.error(f"Scheduler failed: {e}")
# autonomous engine (isolated task)
try:
asyncio.create_task(autonomous_loop())
except Exception as e:
logger.error(f"Autonomous engine failed: {e}")
yield
logger.info("Shutdown complete")
# =========================================================
# APP
# =========================================================
app = FastAPI(
title="Basyx Whisper V10.1 Autonomous Operator",
lifespan=lifespan,
)
# AUTH ROUTER (only if available)
if AUTH_ENABLED:
app.include_router(auth_router)
# =========================================================
# TASKS
# =========================================================
VALID_TASKS = {
"autonomous",
"auto-publish",
"publish",
"bulk-publish",
"generate-metadata",
"generate-thumbnail",
"schedule-post",
"transcribe",
"subtitles",
"render",
"highlights",
"viral-score",
"strategy",
"batch",
"clips",
}
def normalize_task(task: str):
task = task.lower().replace("_", "-")
if task not in VALID_TASKS:
raise Exception(f"Unknown task: {task}")
return task
# =========================================================
# SAFE INPUT RESOLVER
# =========================================================
async def safe_resolve(file, source):
try:
if not file and not source:
return None
upload_file = file if isinstance(file, UploadFile) else None
return await asyncio.to_thread(resolve_input, source, upload_file)
except Exception as e:
logger.error(f"resolve_input failed: {e}")
return None
# =========================================================
# EXECUTION ENGINE
# =========================================================
async def execute_task(video_path, task, payload=None, webhook=None):
payload = payload or {}
if task == "bulk-publish":
return await bulk_execute(payload), None
if task not in ["bulk-publish", "schedule-post"] and not video_path:
return {"error": "No valid input resolved"}, None
if task == "autonomous":
return await asyncio.to_thread(run_autonomous_engine, video_path), None
if task == "auto-publish":
auto = await asyncio.to_thread(run_autonomous_engine, video_path)
return await dispatch_publish(variants=auto.get("all_variants", [])), None
if task == "publish":
return await dispatch_publish(video_path=video_path, payload=payload), None
if task == "generate-metadata":
return generate_metadata(video_path), None
if task == "generate-thumbnail":
output_path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.jpg")
thumb = generate_thumbnail(video_path, output=output_path)
return {"thumbnail": thumb}, output_path
if task == "batch":
job_id = create_job(video_path, webhook=webhook)
return {"status": "queued", "job_id": job_id}, None
if task == "transcribe":
words = await asyncio.to_thread(transcribe_video, video_path)
return {"words": words}, None
if task == "subtitles":
words = await asyncio.to_thread(transcribe_video, video_path)
return {"srt": generate_srt(words)}, None
if task == "render":
words = await asyncio.to_thread(transcribe_video, video_path)
srt = generate_srt(words)
output = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}_render.mp4")
await asyncio.to_thread(render_subtitles, video_path, srt, output)
return {"status": "render_complete"}, output
if task == "highlights":
words = await asyncio.to_thread(transcribe_video, video_path)
highlights = detect_highlights(words) or []
clips = create_clips(video_path, highlights)
return {"clips_created": len(clips)}, (clips[0] if clips else None)
if task == "clips":
words = await asyncio.to_thread(transcribe_video, video_path)
highlights = detect_highlights(words) or []
return {"clips": create_clips(video_path, highlights)}, None
if task == "viral-score":
words = await asyncio.to_thread(transcribe_video, video_path)
segments = detect_highlights(words) or []
return {"scores": [score_clip(s) for s in segments]}, None
if task == "strategy":
words = await asyncio.to_thread(transcribe_video, video_path)
script = rewrite_script(words)
persona = predict_audience(words)
curve = simulate_retention(words)
return {
"hook": script["hook"],
"persona": persona,
"viral_score": viral_score(curve),
"platforms": {
"tiktok": adapt_platform(script, "tiktok"),
"reels": adapt_platform(script, "reels"),
},
}, None
return {"error": "Task execution failed"}, None
# =========================================================
# ROUTER
# =========================================================
@app.post("/execute/{task_name}")
async def execute_router(
request: Request,
task_name: str,
file: UploadFile = File(None),
url_input: str = Form(None),
source: str = Form(None),
webhook: str = Form(None),
):
try:
task = normalize_task(task_name)
payload = {}
if request.headers.get("content-type", "").startswith("application/json"):
payload = await request.json()
video_path = await safe_resolve(file, url_input or source)
result, output = await execute_task(video_path, task, payload, webhook)
if output and isinstance(output, str) and os.path.exists(output):
return FileResponse(output)
return {"task": task, "result": result}
except Exception as e:
logger.exception(e)
return JSONResponse({"error": str(e)}, status_code=500)
# =========================================================
# HEALTH
# =========================================================
@app.get("/api/health")
def health():
return {
"status": "online",
"version": "V10.1",
"auth_enabled": AUTH_ENABLED
}
@app.get("/api/status/{job_id}")
def status(job_id: str):
return get_job(job_id) or {"error": "Job not found"}
# =========================================================
# GRADIO UI
# =========================================================
async def ui_handler(video, task, webhook, url_input):
source = url_input or video
video_path = await safe_resolve(video, source)
result, output = await execute_task(
video_path,
normalize_task(task),
{},
webhook,
)
return str(result), output
with gr.Blocks() as demo:
gr.Markdown("# 🚀 Basyx Whisper V10.1 Stable Operator")
video_input = gr.Video()
url_input = gr.Textbox(label="Video URL")
task_dropdown = gr.Dropdown(
choices=list(VALID_TASKS),
value="autonomous",
)
webhook_input = gr.Textbox(label="Webhook")
run_btn = gr.Button("Execute")
output_box = gr.Textbox()
video_output = gr.Video()
run_btn.click(
ui_handler,
[video_input, task_dropdown, webhook_input, url_input],
[output_box, video_output],
)
app = gr.mount_gradio_app(app, demo, path="/")
|