-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_processor.py
367 lines (327 loc) · 12.9 KB
/
scene_processor.py
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
363
364
365
366
367
# scene_processor.py
from typing import Dict, Optional, List, Any
import logging
from baml_client import b
from baml_client.type_builder import TypeBuilder
from entity_registry import EntityRegistry
from entity_extractors import (
extract_and_register_entities,
infer_object_owners
)
logger = logging.getLogger(__name__)
async def process_scene(
scene_data: Dict,
story_context: str,
scene_number: int,
*,
next_scene_uuid: Optional[str]=None,
entity_registry: Optional[EntityRegistry]=None,
tb: Optional[TypeBuilder] = None,
known_agent_uuids: Optional[List[str]] = None,
known_object_uuids: Optional[List[str]] = None
) -> Dict:
"""Process a single scene, extracting all relevant information."""
try:
if entity_registry is None:
entity_registry = EntityRegistry()
# Generate scene UUID
scene_uuid = f"scene-{scene_number:03}"
scene_title = scene_data.get("Scene", "Untitled Scene")
logger.info(f"Processing scene: {scene_title} (UUID: {scene_uuid})")
# Format scene text for processing, now passing scene_location
scene_text = format_dialogue(scene_data.get("Dialogue", []), scene_number, scene_data.get("Scene"))
# First extract entities to ensure proper typing
await extract_and_register_entities(
scene_data,
scene_text,
story_context,
entity_registry,
tb
)
# Get current UUIDs after entity registration
current_agent_uuids = known_agent_uuids or list(entity_registry.agents.keys())
current_object_uuids = known_object_uuids or list(entity_registry.objects.keys())
# Extract metadata (now using existing entity UUIDs)
metadata = await b.ExtractSceneMetadata(
scene_text=scene_text,
story_context=story_context,
baml_options={"tb": tb}
)
if metadata:
metadata_dict = metadata.model_dump()
# Directly use the location from ExtractSceneMetadata if it's a valid UUID
if metadata_dict.get('location') and not entity_registry.normalizer.validate_reference(metadata_dict['location']):
# Only resolve if it's not already a valid UUID
location_uuid = entity_registry.resolve_reference('locations', metadata_dict['location'])
metadata_dict['location'] = location_uuid
metadata_dict['uuid'] = scene_uuid
metadata_dict['scene_number'] = scene_number
metadata_dict['next_scene'] = next_scene_uuid
else:
metadata_dict = {
'uuid': scene_uuid,
'scene_number': scene_number,
'next_scene': next_scene_uuid,
'title': scene_title,
'description': ''
}
# Extract events using known entity UUIDs
events = await b.ExtractEvents(
scene_text=scene_text,
story_context=story_context,
scene_number=scene_number,
known_agents=current_agent_uuids,
known_objects=current_object_uuids,
baml_options={"tb": tb}
)
events_list = []
for event in events:
event_dict = event.model_dump()
# Ensure all agent/object references are valid UUIDs
if 'agent_participations' in event_dict:
event_dict['agent_participations'] = [
uuid for uuid in event_dict['agent_participations']
if entity_registry.get_entity_details('agents', uuid)
]
if 'object_involvements' in event_dict:
event_dict['object_involvements'] = [
uuid for uuid in event_dict['object_involvements']
if entity_registry.get_entity_details('objects', uuid)
]
events_list.append(event_dict)
# Extract participations and involvements
agent_participations = await b.ExtractAgentParticipations(
scene_text=scene_text,
story_context=story_context,
events=events_list,
agents=current_agent_uuids,
baml_options={"tb": tb}
)
participations_list = []
for ap in agent_participations:
ap_dict = ap.model_dump()
agent_uuid = entity_registry.resolve_reference('agents', ap_dict['agent'])
if agent_uuid and ap_dict.get('event'):
ap_dict['agent'] = agent_uuid
ap_dict['uuid'] = f"participation-{agent_uuid}-{ap_dict['event']}"
participations_list.append(ap_dict)
object_involvements = await b.ExtractObjectInvolvements(
scene_text=scene_text,
story_context=story_context,
events=events_list,
objects=current_object_uuids,
baml_options={"tb": tb}
)
involvements_list = []
for oi in object_involvements:
oi_dict = oi.model_dump()
object_uuid = entity_registry.resolve_reference('objects', oi_dict['object'])
if object_uuid and oi_dict.get('event'):
oi_dict['object'] = object_uuid
oi_dict['uuid'] = f"involvement-{object_uuid}-{oi_dict['event']}"
involvements_list.append(oi_dict)
return {
"scene_uuid": scene_uuid,
"original_scene_data": scene_data,
"extracted_data": {
"metadata": metadata_dict,
"events": events_list,
"agent_participations": participations_list,
"object_involvements": involvements_list
}
}
except Exception as e:
logger.error(f"Error processing scene {scene_title}: {str(e)}")
return {
"scene_uuid": scene_uuid,
"original_scene_data": scene_data,
"error": str(e)
}
async def extract_scene_metadata(
scene_text: str,
story_context: str,
scene_uuid: str,
scene_number: int,
next_scene_uuid: Optional[str],
entity_registry: EntityRegistry,
tb: TypeBuilder
) -> Dict:
"""Extract and process scene metadata."""
metadata_extracted = await b.ExtractSceneMetadata(
scene_text=scene_text,
story_context=story_context,
baml_options={"tb": tb}
)
metadata = metadata_extracted.model_dump()
metadata["uuid"] = scene_uuid
metadata["scene_number"] = scene_number
metadata["next_scene"] = next_scene_uuid
# Extract and assign primary location
locations = await b.ExtractLocations(
scene_text=scene_text,
story_context=story_context,
baml_options={"tb": tb}
)
if locations:
primary_location = locations[0].model_dump()
location_uuid = entity_registry.register_entity('locations', primary_location)
metadata["location"] = location_uuid
return metadata
async def extract_scene_events(
scene_text: str,
story_context: str,
scene_number: int,
known_agent_uuids: List[str],
known_object_uuids: List[str],
tb: TypeBuilder
) -> List[Dict]:
"""Extract events from a scene."""
events_extracted = await b.ExtractEvents(
scene_text=scene_text,
story_context=story_context,
scene_number=scene_number,
known_agents=known_agent_uuids,
known_objects=known_object_uuids,
baml_options={"tb": tb}
)
events = [ev.model_dump() for ev in events_extracted]
for event in events:
event['uuid'] = f"event-{scene_number}-{event['sequence_within_scene']}"
return events
async def extract_agent_participations(
scene_text: str,
story_context: str,
events: List[Dict],
entity_registry: EntityRegistry,
tb: TypeBuilder
) -> List[Dict]:
"""Extract agent participations for events."""
registry_agents = [v for v in entity_registry.agents.values()]
agent_parts_extracted = await b.ExtractAgentParticipations(
scene_text=scene_text,
story_context=story_context,
events=events,
agents=registry_agents,
baml_options={"tb": tb}
)
agent_participations = []
for p in agent_parts_extracted:
d = p.model_dump()
agent_uuid = entity_registry.resolve_reference('agents', d['agent'])
if agent_uuid:
d['agent'] = agent_uuid
d['uuid'] = f"participation-{agent_uuid}-{d['event']}"
agent_participations.append(d)
else:
logger.warning(f"Skipping invalid agent participation for '{d['agent']}'")
return agent_participations
async def extract_object_involvements(
scene_text: str,
story_context: str,
events: List[Dict],
entity_registry: EntityRegistry,
tb: TypeBuilder
) -> List[Dict]:
"""Extract object involvements for events."""
registry_objects = [v for v in entity_registry.objects.values()]
obj_invs_extracted = await b.ExtractObjectInvolvements(
scene_text=scene_text,
story_context=story_context,
events=events,
objects=registry_objects,
baml_options={"tb": tb}
)
object_involvements = []
for oi in obj_invs_extracted:
d = oi.model_dump()
obj_uuid = entity_registry.resolve_reference('objects', d['object'])
if obj_uuid:
d['object'] = obj_uuid
d['uuid'] = f"involvement-{obj_uuid}-{d['event']}"
object_involvements.append(d)
else:
logger.warning(f"Skipping invalid object involvement for '{d['object']}'")
return object_involvements
def build_scene_output(
metadata: Dict,
events: List[Dict],
agent_participations: List[Dict],
object_involvements: List[Dict],
entity_registry: EntityRegistry
) -> Dict:
"""Build the final scene output structure."""
used_agent_uuids = set()
used_object_uuids = set()
used_location_uuids = set()
used_org_uuids = set()
# Primary location from metadata
if metadata.get("location"):
used_location_uuids.add(metadata["location"])
# Collect references from events
for e in events:
used_agent_uuids.update(e.get('agent_participations', []))
used_object_uuids.update(e.get('object_involvements', []))
if e.get('location'):
used_location_uuids.add(e['location'])
# Collect references from agent_participations
for ap in agent_participations:
used_agent_uuids.add(ap['agent'])
agent_details = entity_registry.get_entity_details('agents', ap['agent'])
if agent_details and agent_details.get('affiliated_org'):
org_uuid = entity_registry.resolve_reference(
'organizations',
agent_details['affiliated_org']
)
if org_uuid:
used_org_uuids.add(org_uuid)
# Collect references from object_involvements
for oi in object_involvements:
used_object_uuids.add(oi['object'])
obj_details = entity_registry.get_entity_details('objects', oi['object'])
if obj_details and obj_details.get('original_owner'):
owner_uuid = entity_registry.resolve_reference(
'agents',
obj_details['original_owner']
)
if owner_uuid:
used_agent_uuids.add(owner_uuid)
return {
"metadata": metadata,
"events": events,
"agents": [
entity_registry.get_entity_details('agents', au)
for au in used_agent_uuids
if entity_registry.get_entity_details('agents', au)
],
"objects": [
entity_registry.get_entity_details('objects', ou)
for ou in used_object_uuids
if entity_registry.get_entity_details('objects', ou)
],
"locations": [
entity_registry.get_entity_details('locations', lu)
for lu in used_location_uuids
if entity_registry.get_entity_details('locations', lu)
],
"organizations": [
entity_registry.get_entity_details('organizations', ou)
for ou in used_org_uuids
if entity_registry.get_entity_details('organizations', ou)
],
"agent_participations": agent_participations,
"object_involvements": object_involvements,
}
def format_dialogue(
dialogues: List[Dict[str, str]],
scene_number: int,
scene_location: Optional[str] = None
) -> str:
formatted_lines = [f"(Scene Number: {scene_number})"]
if scene_location:
formatted_lines.append(f"Location: {scene_location}")
for dialogue in dialogues:
if "Stage Direction" in dialogue:
formatted_lines.append(f"[{dialogue['Stage Direction']}]")
elif "Character" in dialogue and "Line" in dialogue:
formatted_lines.append(f"{dialogue['Character']}: {dialogue['Line']}")
return "\n".join(formatted_lines)