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+
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from pydantic import BaseModel, Field
|
| 9 |
+
|
| 10 |
+
from agents import Agent, function_tool, Runner # OpenAI Agents SDK
|
| 11 |
+
from openai import OpenAI
|
| 12 |
+
|
| 13 |
+
load_dotenv()
|
| 14 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# ---------- Output schema (structured outputs via output_type) ----------
|
| 18 |
+
class Hook(BaseModel):
|
| 19 |
+
spoken: str
|
| 20 |
+
onScreen: str
|
| 21 |
+
durationSec: float
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class Beat(BaseModel):
|
| 25 |
+
timeRange: str
|
| 26 |
+
spoken: str
|
| 27 |
+
onScreen: str
|
| 28 |
+
shot: str
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class CTA(BaseModel):
|
| 32 |
+
spoken: str
|
| 33 |
+
onScreen: str
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class ReelScript(BaseModel):
|
| 37 |
+
language: str
|
| 38 |
+
hook: Hook
|
| 39 |
+
beats: List[Beat] = Field(min_length=3, max_length=6)
|
| 40 |
+
cta: CTA
|
| 41 |
+
caption: str
|
| 42 |
+
hashtags: List[str] = Field(min_length=5, max_length=10)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class FinalOutput(BaseModel):
|
| 46 |
+
transcript: str
|
| 47 |
+
reelScript: ReelScript
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# ---------- Tools ----------
|
| 51 |
+
@function_tool
|
| 52 |
+
def transcribe_video(video_path: str, language: str = "nl") -> str:
|
| 53 |
+
"""
|
| 54 |
+
Extract audio (ffmpeg) + transcribe (Whisper).
|
| 55 |
+
video_path must be a local file path.
|
| 56 |
+
"""
|
| 57 |
+
if not os.path.exists(video_path):
|
| 58 |
+
raise FileNotFoundError(f"Video not found: {video_path}")
|
| 59 |
+
|
| 60 |
+
with tempfile.TemporaryDirectory() as td:
|
| 61 |
+
audio_path = os.path.join(td, "audio.mp3")
|
| 62 |
+
|
| 63 |
+
# Extract audio
|
| 64 |
+
subprocess.run(
|
| 65 |
+
["ffmpeg", "-y", "-i", video_path, "-vn", "-acodec", "libmp3lame", "-b:a", "128k", audio_path],
|
| 66 |
+
check=True,
|
| 67 |
+
stdout=subprocess.DEVNULL,
|
| 68 |
+
stderr=subprocess.DEVNULL,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Transcribe
|
| 72 |
+
with open(audio_path, "rb") as f:
|
| 73 |
+
t = client.audio.transcriptions.create(
|
| 74 |
+
model="whisper-1",
|
| 75 |
+
file=f,
|
| 76 |
+
# language=language, # optional: enable if you want to force language
|
| 77 |
+
)
|
| 78 |
+
return t.text
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@function_tool
|
| 82 |
+
def generate_reel_script(transcript: str, language: str = "nl", tone: str = "informatief", goal: str = "") -> dict:
|
| 83 |
+
"""
|
| 84 |
+
Generate Reel script as JSON object.
|
| 85 |
+
"""
|
| 86 |
+
prompt = f"""
|
| 87 |
+
Je krijgt een transcript van een video. Maak een Instagram Reel script in {language}.
|
| 88 |
+
Tone of voice: {tone}.
|
| 89 |
+
Doel: {goal or "maak een sterke samenvatting die mensen afkijkt"}.
|
| 90 |
+
|
| 91 |
+
Regels:
|
| 92 |
+
- Hook = 1 zin + korte on-screen tekst (max ~8 woorden) + 2-4 sec.
|
| 93 |
+
- Beats: 3 tot 6 stappen met timeRange ("0:03-0:08"), spoken, onScreen (kort), shot (camera/beeldsuggestie).
|
| 94 |
+
- CTA: 1 zin gesproken + on-screen.
|
| 95 |
+
- Caption: 1–2 zinnen.
|
| 96 |
+
- Hashtags: 5–10 relevante hashtags.
|
| 97 |
+
|
| 98 |
+
Transcript:
|
| 99 |
+
{transcript}
|
| 100 |
+
""".strip()
|
| 101 |
+
|
| 102 |
+
# We vragen expliciet om JSON en valideren daarna via output_type van de agent.
|
| 103 |
+
r = client.responses.create(
|
| 104 |
+
model="gpt-4o-mini",
|
| 105 |
+
input=prompt,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# output_text is meestal JSON als je prompt strak is; agent output_type vangt formatfouten af.
|
| 109 |
+
return json.loads(r.output_text)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# ---------- Agent ----------
|
| 113 |
+
reel_agent = Agent(
|
| 114 |
+
name="ReelMaker",
|
| 115 |
+
instructions=(
|
| 116 |
+
"Je maakt Instagram Reel scripts.\n"
|
| 117 |
+
"1) Gebruik transcribe_video om transcript te krijgen.\n"
|
| 118 |
+
"2) Gebruik generate_reel_script om reelScript te maken.\n"
|
| 119 |
+
"3) Geef als FINAL output exact dit object: { transcript: str, reelScript: ReelScript }.\n"
|
| 120 |
+
"Geen extra tekst."
|
| 121 |
+
),
|
| 122 |
+
tools=[transcribe_video, generate_reel_script],
|
| 123 |
+
output_type=FinalOutput, # structured outputs in Agents SDK :contentReference[oaicite:3]{index=3}
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def run_local(video_path: str, language: str = "nl", tone: str = "informatief", goal: str = "") -> FinalOutput:
|
| 128 |
+
# Runner voert de agent uit en regelt tool-calls :contentReference[oaicite:4]{index=4}
|
| 129 |
+
result = Runner.run_sync(
|
| 130 |
+
reel_agent,
|
| 131 |
+
input=f"Verwerk video_path={video_path} language={language} tone={tone} goal={goal}",
|
| 132 |
+
)
|
| 133 |
+
return result.final_output
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
| 137 |
+
# Voorbeeld: python agent_app.py /pad/naar/video.mp4
|
| 138 |
+
import sys
|
| 139 |
+
if len(sys.argv) < 2:
|
| 140 |
+
print("Usage: python agent_app.py /path/to/video.mp4")
|
| 141 |
+
raise SystemExit(1)
|
| 142 |
+
|
| 143 |
+
out = run_local(sys.argv[1])
|
| 144 |
+
print("\n=== TRANSCRIPT ===\n")
|
| 145 |
+
print(out.transcript)
|
| 146 |
+
print("\n=== REEL SCRIPT (JSON) ===\n")
|
| 147 |
+
print(out.reelScript.model_dump_json(indent=2))
|