@@ -265,19 +265,40 @@ def synthesize(self, script_text: str, speaker_mapping: Dict[str, str], output_f
265265
266266 def _parse_script_segments (self , script_text : str ) -> List [Tuple [str , str ]]:
267267 segments = []
268+ current_speaker = None
269+ current_text_lines = []
270+
268271 for raw_line in script_text .splitlines ():
269272 line = raw_line .strip ()
270273 if not line :
271274 continue
272- m = re .match (r"^(\w+):\s*(.+)$" , line )
273- if not m :
274- self .logger .info (f"Skipping non-dialogue line for ElevenLabs: '{ line } '" )
275- continue
276- speaker , text = m .group (1 ).strip (), m .group (2 ).strip ()
277- # Apply sanitize_text here, after speaker and text are separated
278- text = sanitize_text (text )
279- if text :
280- segments .append ((speaker , text ))
275+
276+ match = re .match (r"^(\w+)\s*:\s*(.+)$" , line )
277+
278+ if match :
279+ # This is a new speaker line.
280+ # First, save the previous speaker's collected text if it exists.
281+ if current_speaker and current_text_lines :
282+ full_text = " " .join (current_text_lines )
283+ # Sanitize the joined text, then remove any newlines for ElevenLabs.
284+ sanitized_text = sanitize_text (full_text ).replace ('\n ' , ' ' ).replace ('\r ' , '' )
285+ if sanitized_text :
286+ segments .append ((current_speaker , sanitized_text ))
287+
288+ # Start the new speaker's block.
289+ current_speaker = match .group (1 ).strip ()
290+ current_text_lines = [match .group (2 ).strip ()]
291+ elif current_speaker :
292+ # This is a continuation of the current speaker's dialogue.
293+ current_text_lines .append (line )
294+
295+ # After the loop, add the last speaker's segment if it exists.
296+ if current_speaker and current_text_lines :
297+ full_text = " " .join (current_text_lines )
298+ sanitized_text = sanitize_text (full_text ).replace ('\n ' , ' ' ).replace ('\r ' , '' )
299+ if sanitized_text :
300+ segments .append ((current_speaker , sanitized_text ))
301+
281302 return segments
282303
283304
@@ -396,10 +417,10 @@ def sanitize_app_settings_for_backend(app_settings: Dict[str, Any]) -> Dict[str,
396417 clean_elevenlabs = {}
397418 for speaker , data in elevenlabs_voices .items ():
398419 if isinstance (data , dict ):
399- elevenlabs_mapping_clean [speaker ] = data .get ('id' , '' )
420+ clean_elevenlabs [speaker ] = data .get ('id' , '' )
400421 else :
401422 # Legacy format: use the string as-is
402- elevenlabs_mapping_clean [speaker ] = data
423+ clean_elevenlabs [speaker ] = data
403424 clean_settings ["speaker_voices_elevenlabs" ] = clean_elevenlabs
404425
405426 return clean_settings
0 commit comments