11import json
22import time
3+ from concurrent .futures import ThreadPoolExecutor , as_completed
34
45import openai
56import tiktoken
@@ -310,14 +311,19 @@ def moderate_content(self, content, content_type='text', custom_prompt=None):
310311 for i in range (0 , content_chars , MAX_CHARS_PER_CHUNK ):
311312 chunks .append (content [i :i + MAX_CHARS_PER_CHUNK ])
312313
314+ # Process all chunks IN PARALLEL for maximum speed
313315 chunk_results = []
314- for i , chunk in enumerate (chunks ):
315- result = self ._analyze_with_custom_prompt (chunk , custom_prompt )
316- chunk_results .append (result )
317-
318- # Early exit if chunk is rejected
319- if result ['decision' ] == 'rejected' :
320- break
316+ with ThreadPoolExecutor (max_workers = min (len (chunks ), 10 )) as executor :
317+ # Submit all chunks at once
318+ future_to_chunk = {
319+ executor .submit (self ._analyze_with_custom_prompt , chunk , custom_prompt ): i
320+ for i , chunk in enumerate (chunks )
321+ }
322+
323+ # Collect results as they complete
324+ for future in as_completed (future_to_chunk ):
325+ result = future .result ()
326+ chunk_results .append (result )
321327
322328 return self ._combine_chunk_results (chunk_results , len (content ))
323329 else :
@@ -340,14 +346,19 @@ def moderate_content(self, content, content_type='text', custom_prompt=None):
340346 # Split content and analyze each chunk
341347 chunks = self .split_text_into_chunks (content , max_content_tokens )
342348
349+ # Process all chunks IN PARALLEL for maximum speed
343350 chunk_results = []
344- for i , chunk in enumerate (chunks ):
345- result = self ._run_enhanced_default_moderation (chunk )
346- chunk_results .append (result )
347-
348- # Early exit if chunk is rejected (for efficiency)
349- if result ['decision' ] == 'rejected' :
350- break
351+ with ThreadPoolExecutor (max_workers = min (len (chunks ), 10 )) as executor :
352+ # Submit all chunks at once
353+ future_to_chunk = {
354+ executor .submit (self ._run_enhanced_default_moderation , chunk ): i
355+ for i , chunk in enumerate (chunks )
356+ }
357+
358+ # Collect results as they complete
359+ for future in as_completed (future_to_chunk ):
360+ result = future .result ()
361+ chunk_results .append (result )
351362
352363 return self ._combine_chunk_results (chunk_results , len (content ))
353364
0 commit comments