import os, requests, time, base64, re
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
MV = os.environ["MAVERA_API_KEY"]
MV_BASE = "https://app.mavera.io/api/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
os.makedirs("sora_output", exist_ok=True)
MAX_ITERATIONS = 3
QUALITY_THRESHOLD = 8
# 1. Generate video script with Mavera
script_gen = requests.post(f"{MV_BASE}/generations", headers=MV_H, json={
"prompt": "Write a detailed 15-second video prompt for Sora. "
"Product: SaaS marketing analytics dashboard. "
"Style: Clean, modern, cinematic. Show the dashboard in use — "
"charts animating upward, a milestone notification popping. "
"End on a wide shot with the tagline visible. "
"Include camera angles, lighting, transitions.",
}).json()
script = script_gen.get("output") or script_gen.get("content") or ""
print(f"Script generated: {len(script)} chars")
time.sleep(2)
for iteration in range(1, MAX_ITERATIONS + 1):
print(f"\n{'='*60}\nITERATION {iteration}\n{'='*60}")
# 2. Generate video with Sora
try:
sora_resp = client.images.generate(model="sora", prompt=script, n=1, size="1792x1024")
video_url = sora_resp.data[0].url
video_b64 = sora_resp.data[0].b64_json
output_path = f"sora_output/video_v{iteration}.mp4"
if video_b64:
with open(output_path, "wb") as f:
f.write(base64.b64decode(video_b64))
print(f"Video saved: {output_path} ({os.path.getsize(output_path) // 1024} KB)")
elif video_url:
vid_data = requests.get(video_url).content
with open(output_path, "wb") as f:
f.write(vid_data)
print(f"Video downloaded: {output_path} ({len(vid_data) // 1024} KB)")
except Exception as e:
print(f"Sora error: {e} — retrying...")
time.sleep(5)
continue
time.sleep(2)
# 3. Analyze with Mavera
analysis = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"Video quality assessor. Iteration {iteration}.\n\n"
f"SCRIPT:\n{script[:3000]}\n\n"
"Score 1-10 on: BRAND ALIGNMENT, NARRATIVE COHERENCE, "
"VISUAL QUALITY, EMOTIONAL IMPACT, CTA CLARITY.\n"
"Give OVERALL SCORE (1-10) and SPECIFIC IMPROVEMENTS as a refined Sora prompt.",
}).json()
analysis_text = analysis.get("content", "")
print(analysis_text[:2000])
score_line = [l for l in analysis_text.split("\n") if "OVERALL" in l.upper()]
nums = re.findall(r'\b(\d+)\b', score_line[0]) if score_line else []
score = int(nums[0]) if nums else 0
print(f"\nScore: {score}/10 (threshold: {QUALITY_THRESHOLD})")
if score >= QUALITY_THRESHOLD:
print(f"Quality met at iteration {iteration}. Final: {output_path}")
break
# 4. Refine prompt
refined = [l for l in analysis_text.split("\n") if "prompt" in l.lower() or "refine" in l.lower()]
if refined:
script += "\n\nRefinements: " + " ".join(refined)[:500]
time.sleep(3)
else:
print(f"Max iterations ({MAX_ITERATIONS}) reached.")