-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_pipeline_test.py
More file actions
290 lines (246 loc) · 11.7 KB
/
Copy pathrun_pipeline_test.py
File metadata and controls
290 lines (246 loc) · 11.7 KB
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
"""
run_pipeline_test.py
---------------------
End-to-end test for the full Phase 1+2 pipeline:
Phase 1 (auto-run):
Idea Agent -> Character Agent -> Story Agent -> [PAUSE]
Phase 2 (after approval):
Screenplay Agent -> Dialogue Agent -> END
Usage:
python run_pipeline_test.py
python run_pipeline_test.py "Your idea here"
Prints:
- Story concept, characters, 3-act structure (after Phase 1)
- Full screenplay scenes with dialogue (after Phase 2)
- SQLite + ChromaDB write verification
"""
import sys
import textwrap
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
SEP = "-" * 65
SEP2 = "=" * 65
def _wrap(text: str, indent: int = 4, width: int = 80) -> str:
prefix = " " * indent
return textwrap.fill(text or "(empty)", width=width,
initial_indent=prefix, subsequent_indent=prefix)
# ─────────────────────────────────────────────────────────────────────────────
def _print_phase1(state: dict) -> None:
concept = state.get("concept")
characters = state.get("characters", [])
acts = state.get("acts", [])
print(f"\n{SEP2}")
print(" STORY CONCEPT")
print(SEP2)
if concept:
print(f" Title : {concept.title}")
print(f" Genre : {concept.genre}")
print(f" Target Audience: {concept.target_audience}")
print(f" Mood : {concept.mood}")
print(f"\n Theme:")
print(_wrap(concept.theme))
print(f"\n Synopsis:")
print(_wrap(concept.synopsis))
print(f"\n{SEP2}")
print(f" CHARACTERS ({len(characters)} generated)")
print(SEP2)
for i, char in enumerate(characters, 1):
print(f"\n [{i}] {char.name} [{char.role.upper()}]")
print(f" {SEP}")
print(f" Personality : ", end=""); print(_wrap(char.personality, indent=0))
print(f" Goals : ", end=""); print(_wrap(char.goals, indent=0))
print(f" Weaknesses : ", end=""); print(_wrap(char.weaknesses, indent=0))
print(f" Speaking Style: ", end=""); print(_wrap(char.speaking_style, indent=0))
print(f" Background : ", end=""); print(_wrap(char.background, indent=0))
if char.relationships:
print(f" Relationships:")
for name, rel in char.relationships.items():
print(f" -> {name}: {rel}")
print(f"\n{SEP2}")
print(f" STORY STRUCTURE ({len(acts)} acts)")
print(SEP2)
for act in acts:
print(f"\n ACT {act.act_number}: {act.title}")
print(f" {SEP}")
print(f" Summary:")
print(_wrap(act.summary))
print(f"\n Scenes:")
for j, scene_summary in enumerate(act.scene_summaries, 1):
global_num = (act.act_number - 1) * 5 + j
print(f"\n Scene {global_num}:")
print(_wrap(scene_summary, indent=6))
def _print_phase2(state: dict) -> None:
scenes = state.get("scenes", [])
dialogue = state.get("dialogue", [])
# Build dialogue lookup: scene_number -> list of lines in sequence order
dlg_by_scene: dict[int, list] = {}
for d in dialogue:
dlg_by_scene.setdefault(d.scene_number, []).append(d)
for lines in dlg_by_scene.values():
lines.sort(key=lambda x: x.sequence)
# Print first 3 scenes in full + summary count for the rest
FULL_PRINT = 3
print(f"\n{SEP2}")
print(f" SCREENPLAY + DIALOGUE ({len(scenes)} scenes, {len(dialogue)} total lines)")
print(SEP2)
for scene in scenes:
sn = scene.scene_number
is_full = sn <= FULL_PRINT
print(f"\n {'=' * 55}")
print(f" SCENE {sn} (Act {scene.act_number}) [{scene.emotion_tone}]")
print(f" {scene.slugline}")
print(f" {'=' * 55}")
if is_full:
print()
# Action lines -- wrap each paragraph separately
for para in (scene.action_lines or "").split("\n"):
if para.strip():
print(_wrap(para, indent=2))
print()
lines = dlg_by_scene.get(sn, [])
if lines:
for dl in lines:
print(f" {dl.character_name.upper()}")
print(_wrap(f"({dl.emotion}) {dl.line}", indent=10))
print()
else:
print(" (no dialogue generated for this scene)")
else:
print(f" Summary: {scene.summary}")
lines = dlg_by_scene.get(sn, [])
print(f" Dialogue: {len(lines)} lines")
if len(scenes) > FULL_PRINT:
print(f"\n [Scenes {FULL_PRINT + 1}-{len(scenes)} shown as summaries above]")
# ─────────────────────────────────────────────────────────────────────────────
def main():
print(SEP2)
print(" Scriptify AI -- Full Pipeline Test (Phase 1 + Phase 2)")
print(SEP2)
# ── Validate Ollama ───────────────────────────────────────────────────────
if not Path(".env").exists():
print("\n ERROR: .env not found. Copy .env.example to .env.")
sys.exit(1)
from backend.config import require_ollama, OLLAMA_MODEL
try:
require_ollama()
print(f"\n [OK] Ollama running | model: {OLLAMA_MODEL}")
except EnvironmentError as e:
print(f"\n ERROR: {e}")
sys.exit(1)
# ── Idea ──────────────────────────────────────────────────────────────────
raw_idea = (
sys.argv[1] if len(sys.argv) > 1
else "A detective solving mysterious murders in Mumbai"
)
print(f"\n Idea: \"{raw_idea}\"")
# ── Create project ────────────────────────────────────────────────────────
from backend.db.database import create_tables, SessionLocal
from backend.db import crud
create_tables()
db = SessionLocal()
try:
project = crud.create_project(db, title="[Pipeline Test -- pending]")
project_id = project.id
print(f" [OK] Project created (id={project_id})")
finally:
db.close()
# ── PHASE 1: idea -> character -> story -> pause ───────────────────────────
import time
print(f"\n{SEP}")
print(" PHASE 1: Idea / Character / Story agents running...")
print(f" (3 x 1 LLM call for idea+characters, 3 calls for story acts)")
print(SEP)
t0 = time.time()
from backend.agents.graph import run_pipeline
try:
thread_id, state = run_pipeline(project_id=project_id, raw_idea=raw_idea)
except Exception as e:
print(f"\n PIPELINE ERROR: {e}")
import traceback; traceback.print_exc()
sys.exit(1)
print(f"\n Phase 1 complete ({time.time() - t0:.1f}s)")
print(f" Thread ID: {thread_id}")
_print_phase1(state)
# ── Verify Phase 1 SQLite ─────────────────────────────────────────────────
print(f"\n{SEP2}")
print(" SQLITE VERIFICATION (Phase 1)")
print(SEP2)
db = SessionLocal()
try:
proj = crud.get_project(db, project_id)
db_chars = crud.list_characters(db, project_id)
db_acts = crud.list_acts(db, project_id)
print(f"\n Project: {proj.title} [status={proj.status}]")
print(f" Characters in DB: {len(db_chars)}")
for c in db_chars:
print(f" [{c.id}] {c.name} ({c.role})")
print(f" Acts in DB: {len(db_acts)}")
for a in db_acts:
print(f" [{a.id}] Act {a.act_number}: {a.title}")
finally:
db.close()
# ── Verify ChromaDB ───────────────────────────────────────────────────────
from backend.memory.chroma_client import get_collection, COLLECTION_CHARACTER_MEMORY
col = get_collection(COLLECTION_CHARACTER_MEMORY)
metas = col.get(where={"project_id": {"$eq": project_id}}).get("metadatas", [])
print(f"\n ChromaDB character_memory: {len(metas)} entries")
for m in metas:
print(f" {m.get('character_name')} ({m.get('role')})")
# ── PHASE 2: approve + screenplay + dialogue ───────────────────────────────
print(f"\n{SEP}")
print(" PHASE 2: Approving review gate -> Screenplay + Dialogue agents...")
print(f" ({sum(len(a.scene_summaries) for a in state.get('acts', []))} scenes x 2 calls each)")
print(f" Expected time: ~{sum(len(a.scene_summaries) for a in state.get('acts', []))*2}+ minutes on CPU")
print(SEP)
t1 = time.time()
from backend.agents.graph import resume_pipeline
try:
final_state = resume_pipeline(thread_id)
except Exception as e:
print(f"\n PHASE 2 ERROR: {e}")
import traceback; traceback.print_exc()
sys.exit(1)
print(f"\n Phase 2 complete ({time.time() - t1:.1f}s)")
_print_phase2(final_state)
# ── Verify Phase 2 SQLite ─────────────────────────────────────────────────
print(f"\n{SEP2}")
print(" SQLITE VERIFICATION (Phase 2)")
print(SEP2)
db = SessionLocal()
try:
db_scenes = crud.list_scenes(db, project_id)
print(f"\n Scenes in DB: {len(db_scenes)}")
total_dlg = 0
for s in db_scenes[:5]: # show first 5
dlg = crud.list_dialogue(db, s.id)
total_dlg += len(dlg)
print(f" Scene {s.scene_number}: \"{s.slugline}\" [{s.emotion_tone}] "
f"-- {len(dlg)} dialogue lines")
if len(db_scenes) > 5:
for s in db_scenes[5:]:
dlg = crud.list_dialogue(db, s.id)
total_dlg += len(dlg)
print(f" ... ({len(db_scenes) - 5} more scenes)")
print(f"\n Total dialogue lines in DB: {total_dlg}")
finally:
db.close()
# ── Verify ChromaDB Phase 2 ───────────────────────────────────────────────
from backend.memory.chroma_client import (
COLLECTION_SCENE_SUMMARIES, COLLECTION_DIALOGUE_HISTORY
)
sc_col = get_collection(COLLECTION_SCENE_SUMMARIES)
dh_col = get_collection(COLLECTION_DIALOGUE_HISTORY)
sc_ids = sc_col.get(where={"project_id": {"$eq": project_id}}).get("ids", [])
dh_ids = dh_col.get(where={"project_id": {"$eq": project_id}}).get("ids", [])
print(f"\n ChromaDB scene_summaries : {len(sc_ids)} entries")
print(f" ChromaDB dialogue_history : {len(dh_ids)} entries")
# ── Done ──────────────────────────────────────────────────────────────────
print(f"\n{SEP2}")
print(" PIPELINE COMPLETE")
print(SEP2)
print(f"\n Project ID : {project_id}")
print(f" Thread ID : {thread_id}")
print(f" Total time : {time.time() - t0:.1f}s")
print()
if __name__ == "__main__":
main()