
    Qp_j                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ  ee	          
                                j        d         ZdZdZdZdZd Zd	 Zd
 Zd Zd Zedk    r e             dS dS )    N)Path   zhttp://192.168.40.33:7862zngoc-huyen-vbeei  i  c                 N    t          j        |                                           S N)hashlibsha256	hexdigest)datas    script/build-tts-plan.py	sha_bytesr      s    >$))+++    c                    t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                  .                          d          S )Ni   )read)handles   r   <lambda>zsha_path.<locals>.<lambda>   s    &++o">"> r   r   )r   r   openiterupdater	   )pathdigestblockr   s      @r   sha_pathr      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'c                     | j                             dd           |                     | j        dz             }|                    t          j        |dd          dz   d	           t          j        ||            d S )
NT)parentsexist_okz.partF   )ensure_asciiindent
utf-8encoding)	parentmkdirwith_suffixsuffix
write_textjsondumpsosreplace)r   valueparts      r   atomic_jsonr0      s|    KdT222DK'122DOODJu5CCCdJU\O]]]JtTr   c                    t          j        d| t           j                  }d |D             }g }d}|D ]}t          |          t          k    r.t          j        d|t           j                  }d |D             }n|g}|D ]o}|r?t          |          t          |          z   t
          k    r|                    |           d}t          |          t          k    rt          d          ||z  }p|r|                    |           d                    |          | k    rt          d          |rt          d	 |D                       rt          d
          |S )Nz.*?(?:\n\n|\Z))flagsc                     g | ]}||S  r4   ).0ps     r   
<listcomp>zsplit_text.<locals>.<listcomp>&   s    ---1-!---r    u   .*?(?:[.!?…][”’']?\s+|\Z)c                     g | ]}||S r4   r4   )r5   ss     r   r7   zsplit_text.<locals>.<listcomp>,   s    ///1Q/Q///r   z%sentence exceeds segmentation maximumz"segmentation reconstruction failedc              3   L   K   | ]}| pt          |          t          k    V   d S r   )len	MAX_CHARSr5   xs     r   	<genexpr>zsplit_text.<locals>.<genexpr>:   s4      EEQ4#a&&9"4EEEEEEr   zinvalid segmentation plan)
refindallDOTALLr<   r=   TARGET_CHARSappendRuntimeErrorjoinany)text
paragraphschunkscurrent	paragraph	sentencesunitsunits           r   
split_textrQ   $   s   -t29EEEJ--Z---JFG  	y>>I%%
#EyXZXabbbI//	///EEKE 	 	D 3w<<#d))3lBBg&&&4yy9$$"#JKKKtOGG	  g	wwv$?@@@ 8SEEfEEEEE 86777Mr   c                     t           dz  } t          j        |                     d                    }|d         }t           dz  }|                                s*t          t           dz                      d                    rt          d          |                    d	i                               d
          dk    rt          d          t           |d         z  }t           |d         z  }t          j        |                    d                    }t          j        |                    d                    }|                    d          dus|                    d          durt          d          t           |d         z  }t           |d         z  }	t           |d         z  }
|	                                }t          |          }||                    d          k    rt          d          t          |	          |                    d          k    rt          d          t          |
          |                    d          k    rt          d          |                    d          |                    d          k    rt          d          t          |                    d                    }g }d}|D ];}|                    ||t          |          z   f           |t          |          z  }<g }|                    dg           D ]>}|d         \  t          fd |D                       s|                    |           ?|rt          d!          g }t!          |d"          D ]\  }}|                    d          }t$          j                            |t*          d#                              d          }|                    ||t          |          t          |          t,          d$z   t*          d%t          |          d&|d'd(d)d*
           |t,          d$z   t*          d+ |D             d,}t.          j                            t.          j        j                                                  }i d-d.ddd/|d         d0|d|d         d|d         d|d         d1|d         d2t          |          d3d4d5t,          d6d$d7t*          d8d9d7gd:d%d;dd<t          |          t          t          j        |d=dd>?                              d                    |||d@}t;          ||           d|d         dA<   d.ddt          |          |dB         |dC|dD<   ||dE<   t;          | |           t=          t          j        d.t          |          |dB         dFd=G                     d S )HNzscript/project-manifest.jsonr"   r#   active_pathszscript/tts-manifest.jsonzaudio/segmentszsegment-*.wav*z TTS plan/segments must be virginstepspronunciation	completedzpronunciation must be terminalpromotion_receipttts_pronunciation_receiptverifiedTzupstream gate not terminalspoken_narrationspoken_narration_sourcetts_pronunciation_lexiconspoken_narration_sha256znormalized narration hash driftspoken_narration_source_sha256znarration source hash driftlexicon_sha256zlexicon hash driftsource_canon_sha256zcanon lineage driftr   replacementsoutput_char_spanc              3   6   K   | ]\  }}|k    o|k    V  d S r   r4   )r5   leftrightendstarts      r   r@   zmain.<locals>.<genexpr>d   s4      PPkdE45=1SE\PPPPPPr   z2segmentation cuts a pronunciation replacement spanr   )rI   voicez/ttsz!application/x-www-form-urlencodedzaudio/segments/segment-04dz.wavpending)
indexrI   text_sha256payload_sha256endpointrh   content_type
byte_countoutput_pathstatusc                     g | ]=}|d          |d         |d         |d         |d         |d         |d         |d         d>S )	rk   rl   rm   rn   rh   ro   rp   rq   )rk   rl   rm   rn   rh   ro   rp   rq   r4   r>   s     r   r7   zmain.<locals>.<listcomp>|   s      Y  Y  Y  KLqz!M:J^_`p^q  @A  BL  @M  XY  Za  Xb  tu  vD  tE  UV  Wc  Ud  uv  wD  uE  F  F  Y  Y  Yr   )input_text_sha256rn   rh   segmentsrr   locked
input_pathrt   tts_pronunciation_receipt_path tts_pronunciation_receipt_sha256providerzpiper-wrapperbase_urlrn   rh   request_fieldsrI   request_content_typepronunciation_spans_preservedsegment_countF),:)r   	sort_keys
separators)segmentation_plan_sha256ru   
created_at
updated_attts_manifestr   )rr   rY   r   r   r   r   tts_planr   )rr   ru   plan_sha256)r   )ROOTr*   loads	read_textexistsrH   globrF   get
read_bytesr   r   rQ   decoderE   r<   	enumerateencodeurllibparse	urlencodeVOICEBASEdatetimenowtimezoneutc	isoformatr+   r0   print)manifest_pathmanifestpathsoutputpromotion_pathpronunciation_path	promotionrU   narration_pathsource_pathlexicon_pathnarration_bytesnarration_sharK   
boundariescursorchunksplit_replacementsrowru   rk   rI   rawpayloadbasisr   planrf   rg   s                              @@r   mainr   ?   s   99Mz-1171CCDDH^$E..F}} ?#t&66<<=MNNOO ?=>>>||GR  $$_55DD;<<<E"566N&A BB
>33W3EEFFIJ1;;W;MMNNM}}Z  ,,0A0A*0M0MUY0Y0Y7888E"455N899K% ;<<L$//11Oo..M))*CDDDD<=== 1 12R S SSS8999!2!23C!D!DDD/000}}*++}/@/@AV/W/WWW0111..w7788FJF  66CJJ#67888#e**  44 + ++,
sPPPPPZPPPPP 	+%%c*** QOPPPH ++  tkk'"",(($)G)GHHOOPWXX$S>>'00v?c((DUDDDD
 
 	 	 	 	 +6M Y  Y  PX  Y  Y  Y	 E 


 1 5
6
6
@
@
B
BC(D 	e./ 	]	
 	}-BC 	)-8X*Y 	-(89 	)%0K*L 	+H5G,H,H 	O 	D 	F 	 	67+ 	 C  	(!" 	X#$ %.djU^boy.z.z.z  /B  /B  CJ  /K  /K  %L  %L+  D. /IH^^,&.DJdwz  |D  xE  xE  cg  hB  cC  SV  W  WHZ H\x(((	$*c(mmTXYsTtuu  EJ  K  K  K  L  L  L  L  Lr   __main__)r   r   r*   r,   rA   urllib.parser   pathlibr   __file__resolver   r   r   r   rD   r=   r   r   r0   rQ   r   __name__r4   r   r   <module>r      s       				 				          tH~~'*"	, , ,      6\L \L \L~ zDFFFFF r   