o
    §µi¬£  ã                   @   s  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 d dlmZ d dl	m
Z
 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 d dlmZmZ zd dlZdZW n ey_   dZY nw d	d
„ Zd dlmZmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z& dZ'e'a(dde )e*ddddddd¡gZ+eƒ Z,dd„ Z-dd„ Z.dìde/fdd„Z0e-ƒ Z1er­e.ƒ nda2d\a3a4da5da6ed d!„ ƒZ7ed"d#„ ƒZ8ed$d%ed&d'd(ej9fd)d*„ƒƒZ:e ;¡ :Z<e =d+¡ ej>d,d-d.Z?e @¡  ejAd/d0d1dd2ZBejCd3d4gd(d5ZDW d  ƒ n	1 sw   Y  ejEd6d7d8ZFejGd9dd:–ZHe @¡  ejAd;d<ddd=ZIejCd>d4gd(d5ZJW d  ƒ n	1 s>w   Y  e @¡ 9 ejKd?d@ddAdBZLejMdd d d(dCZNejOddD ejKdEdFddGZPW d  ƒ n	1 stw   Y  W d  ƒ n	1 s„w   Y  ejQdHdIdJdKdLdMdNZRejQdOddPd'ddQdNZSejQdRdSdKd&dTdUdNZTW d  ƒ n	1 sµw   Y  dVdW„ ZUe<jVeUdeHdX ej>dYdZZWejXd[dZZYed\d]„ ƒZZeDj[e8eDgeBgd^ eJj[e8eJgeIgd^ e? \d_d`„ deIeJg¡ eFjeZe?eIeBePeLeNeTeSeRg	eWeYeIeNgd^ W d  ƒ n	1 sw   Y  dadb„ Z]e ;¡ ƒZ^e =dc¡ e @¡  e =dd¡ e =de¡ W d  ƒ n	1 sEw   Y  e =df¡ ej@dgd8µZ_ejOd(dhdi ejAdjdkdlZ`ejEdmdnd8ZaW d  ƒ n	1 sww   Y  ejOdAdD ej>dod-d.ZbW d  ƒ n	1 s•w   Y  ejOdAdD: ejAdpddqZce @¡  ejQddrdsdrd(dtduZdejQddIdJdKdLdvduZeW d  ƒ n	1 sÎw   Y  W d  ƒ n	1 sÞw   Y  ejOd(dhdi ejCd>d4gdwZfW d  ƒ n	1 sþw   Y  W d  ƒ n	1 sw   Y  dxZge_gZhe`gZiebgZjecgZkefgZledgZmeegZndgZoeagZpeqegd( ƒD ]ûZrej@dgddy»ZsejOd(dhdi ejAdkdZZtejEdmdnd8ZuejEdzd{d8ZvW d  ƒ n	1 sgw   Y  ejOdAdD ej>d,d-d.ZwW d  ƒ n	1 s…w   Y  ejOdAdD: ejAd;ddqZIe @¡  ejQddrdsdrd(d|duZNejQddIdJdKdLdvduZxW d  ƒ n	1 s¾w   Y  W d  ƒ n	1 sÎw   Y  ejOd(dhdi ejCd>d4gdwZyW d  ƒ n	1 sîw   Y  W d  ƒ n	1 sþw   Y  eh zes¡ ei zet¡ ej zew¡ ek zeI¡ el zey¡ em zeN¡ en zex¡ eo zev¡ ep zeu¡ q6eqegƒD ]$Zrejer  \d}d`„ deker eler g¡ eler j[e8eler geker gd^ q6e Ed~¡Z{d(a|dd€„ Z}e{je}ehd d‚dƒ„ Z~eqd(eeoƒƒD ]Zreoer je~eher eier ejer eker eler gd qxe @¡  ejAd/d0d1dd„d…Z€ejCd3d4gd(d5ZW d  ƒ n	1 s·w   Y  d†d‡„ Z‚eƒepƒD ]\ZrZue‚erƒZ„euje„e€eier emer ener ge€d^ qÄejGd9dd:W e @¡ B e O¡  ejKdˆd‰ddGZ…W d  ƒ n	1 sw   Y  e O¡  ejKdEdŠddGZ†W d  ƒ n	1 s"w   Y  W d  ƒ n	1 s2w   Y  W d  ƒ n	1 sBw   Y  ejEd‹d7d8Z‡ej>dYdZZˆejAdŒd0d1dgdddŽZ‰e… Šdd`„ e…e‰¡ ej[e8ege€gd^ edd‘„ ƒZ‹e‡je‹e€gei ej ek e†g eˆgek e‰g d^ d’d“„ ZŒe€jŠeŒe€e`gei e‡d^ W d  ƒ n	1 s¬w   Y  e ;¡ ðZe =d”¡ d•d–gZŽed—d˜„ ƒZerÏeeŽd  ƒ ejeŽeŽd  d™dše d›Z‘ejEdœd7e dyZ’ejOedZ“e‘jŠdžd`„ de’dŸd  e’jee‘ge’e“gd^ e“’ e @¡ ® e O¡  ej>d,d-d.Z”W d  ƒ n	1 s!w   Y  e O¡ } ejGd9dd:e e @¡  ejAd;d¡ddAd=Z•ejCd>d4gd(d5Z–W d  ƒ n	1 sUw   Y  e @¡  ejKd?dd¢dAd£Z—ejMdd d d(dCZ˜W d  ƒ n	1 s|w   Y  ejKdEdd¤Z™ejAd¥d¦dd§ZšW d  ƒ n	1 s›w   Y  W d  ƒ n	1 s«w   Y  W d  ƒ n	1 s»w   Y  ej›d¨dZZœe @¡ P e O¡  ejd©d-d.Zžej>ddªZŸW d  ƒ n	1 sèw   Y  e O¡  ejAd«d(dqZ e Ed¬¡Z¡e Ed­¡Z¢W d  ƒ n	1 sw   Y  W d  ƒ n	1 sw   Y  ed®d¯„ ƒZ£ed°d±„ ƒZ¤ed²d³„ ƒZ¥d´dµ„ Z¦e–j[e8e–ge•gd^ ežj§e j¨e¡jfD ]/Z©e©e£eœeže geœgd^jªe¤eœešgeœgd^jªe¥eœe”e•e™e—e˜geŸe•e˜gd^ ªd¶d`„ deže g¡ qKe¢ješjŠeœj\fD ]Z©e©e¦eœeŸgd qƒW d  ƒ n	1 sšw   Y  W d  ƒ n	1 sªw   Y  e ;¡ Z«e =d·¡ W d  ƒ n	1 sÄw   Y  e ;¡ Ýa¬e =d¸esÖd¹ndº› d»¡ e
d¼ƒ ­d½¡Z®d¾d¿„ Z¯dÀdÁ„ Z°dÂdÃ„ Z±e @¡ m esej²e'dÄdÅgdÆe'dÇZ³n
ej²e'dÄgdÆe'dÇZ³eje+d  ge¯ƒ d  ddÈddÉZ´eje+d( ge¯ƒ d( ddÊddÉZµeje+d e )e*dddddddd(dË¡e )e*dÌdÍdÎddddd(dË¡ge¯ƒ d ddÏddÉZ¶W d  ƒ n	1 sdw   Y  e³jŠe°e³ge´eµe¶gdŸdÐ e´jŠe±e´eµe¶gdŸdÑ eµjŠe±e´eµe¶gdŸdÑ e¶jŠe±e´eµe¶gdŸdÑ e ·e<e^ee«gg dÒ¢¡ W d  ƒ n	1 s®w   Y  e ¸¡ ej¹dÓdÔdeºdÕdÖej¹d×dØddÙdÚej¹dÛdÜdddÝdÞej¹dßdàdddádÞej¹dâdãde/dädÖej¹dådædddçdèdédê„ ƒƒƒƒƒƒƒZ»e¼dëk	re	se»ƒ  dS t¬ ½¡  ¾¡  dS dS )íé    N)ÚOrderedDict)Ú	lru_cache)Úfiles)Úcached_path)ÚAutoModelForCausalLMÚAutoTokenizerTFc                 C   s   t rt | ¡S | S ©N)ÚUSING_SPACESÚspacesÚGPU)Úfunc© r   úM/home/ubuntu/.local/lib/python3.10/site-packages/f5_tts/infer/infer_gradio.pyÚgpu_decorator   s   
r   )Úinfer_processÚ
load_modelÚload_vocoderÚpreprocess_ref_audio_textÚ remove_silence_for_generated_wavÚsave_spectrogramÚtempfile_kwargs)ÚDiTÚUNetTz	F5-TTS_v1z:hf://SWivid/F5-TTS/F5TTS_v1_Base/model_1250000.safetensorsz*hf://SWivid/F5-TTS/F5TTS_v1_Base/vocab.txté   é   é   é   é   é   )ÚdimÚdepthÚheadsÚff_multÚtext_dimÚconv_layersc                  C   s*   t ttd ƒƒ} t td ¡}tt|| ƒS )Nr   r   )Ústrr   ÚDEFAULT_TTS_MODEL_CFGÚjsonÚloadsr   r   )Ú	ckpt_pathÚF5TTS_model_cfgr   r   r   Ú
load_f5ttsA   s   r+   c                  C   s,   t tdƒƒ} tddddddd}tt|| ƒS )	Nz7hf://SWivid/E2-TTS/E2TTS_Base/model_1200000.safetensorsr   é   r   r   Fé   )r   r    r!   r"   Útext_mask_paddingÚpe_attn_head)r%   r   Údictr   r   )r)   ÚE2TTS_model_cfgr   r   r   Ú
load_e2ttsG   s   r2   Ú r)   c                 C   sz   |   ¡ |  ¡ } }|  d¡rtt| ƒƒ} | d¡rtt|ƒƒ}|d u r+t td ¡}n
t|tƒr5t |¡}tt	|| |dS )Nzhf://r   )Ú
vocab_file)
ÚstripÚ
startswithr%   r   r'   r(   r&   Ú
isinstancer   r   )r)   Ú
vocab_pathÚ	model_cfgr   r   r   Úload_customM   s   



r:   )Nr3   c                 C   sj   |j | ddd}||gdd |j¡}|jdi |¤dddd	œ¤Ž}d
d„ t|j|ƒD ƒ}|j|ddd S )zGenerate response using QwenFT)ÚtokenizeÚadd_generation_promptÚpt)Úreturn_tensorsr   gffffffæ?gffffffî?)Úmax_new_tokensÚtemperatureÚtop_pc                 S   s    g | ]\}}|t |ƒd … ‘qS r   )Úlen)Ú.0Ú	input_idsÚ
output_idsr   r   r   Ú
<listcomp>s   s    ÿz(chat_model_inference.<locals>.<listcomp>)Úskip_special_tokensr   Nr   )Úapply_chat_templateÚtoÚdeviceÚgenerateÚziprD   Úbatch_decode)ÚmessagesÚmodelÚ	tokenizerÚtextÚmodel_inputsÚgenerated_idsr   r   r   Úchat_model_inferenceb   s"   ýÿ
ü
ÿrT   c                 C   sP   | r t | ddd}| ¡  ¡ }W d   ƒ n1 sw   Y  nd}tj|dS )NÚrúutf-8©Úencodingr3   )Úvalue)ÚopenÚreadr5   ÚgrÚupdate)ÚfileÚfrQ   r   r   r   Úload_text_from_filey   s   ÿ€r`   iè  )Úmaxsizeç333333Ã?é    r-   c
                 C   s(  | st  d¡ t  ¡ t  ¡ |fS |dk s|dkr$t  d¡ tj dd¡}t |¡ |}
| ¡ s=t  d¡ t  ¡ t  ¡ |fS t	| ||	d\}}|t
krMt}n@|dkr_td u r\|	dƒ tƒ at}n.t|tƒr|d d	krtrpJ d
ƒ‚t|d kr‹|	dƒ t|d |d |d da|d at}t||||t||||	t  ¡ d
\}}}|rétjdddit¤Ž}|j}W d   ƒ n1 sºw   Y  zt |||¡ t|jƒ t |j¡\}}W t  !|¡ nt  !|¡ w | "¡  #¡  $¡ }tjdddit¤Ž}|j}W d   ƒ n	1 sw   Y  t%||ƒ ||f|||
fS )NzPlease provide reference audio.r   éÿÿÿz=Seed must in range 0 ~ 2147483647. Using random seed instead.z4Please enter text to generate or upload a text file.)Ú	show_infoúE2-TTSzLoading E2-TTS model...ÚCustomz,Only official checkpoints allowed in Spaces.r-   zLoading Custom TTS model...r   é   )r8   r9   )Úcross_fade_durationÚnfe_stepÚspeedre   ÚprogressÚsuffixz.wavz.pngr   )&r\   ÚWarningr]   ÚnpÚrandomÚrandintÚtorchÚmanual_seedr5   r   ÚDEFAULT_TTS_MODELÚF5TTS_ema_modelÚE2TTS_ema_modelr2   r7   Útupler	   Úpre_custom_pathr:   Úcustom_ema_modelr   ÚvocoderÚProgressÚtempfileÚNamedTemporaryFiler   ÚnameÚsfÚwriter   Ú
torchaudioÚloadÚosÚunlinkÚsqueezeÚcpuÚnumpyr   )Úref_audio_origÚref_textÚgen_textrO   Úremove_silenceÚseedri   rj   rk   re   Ú	used_seedÚ	ref_audioÚ	ema_modelÚ
final_waveÚfinal_sample_rateÚcombined_spectrogramr_   Ú	temp_pathÚ_Útmp_spectrogramÚspectrogram_pathr   r   r   Úinferƒ   sj   



öÿ
ÿ
r—   z# Batched TTSzReference AudioÚfilepath)ÚlabelÚtypezText to Generateé
   é(   )r™   ÚlinesÚ	max_linesÚscalez&Load Text to Generate from File (.txt)z.txt)r™   Ú
file_typesrŸ   Ú
SynthesizeÚprimary)ÚvariantzAdvanced Settings©rZ   zReference TextzŠLeave blank to automatically transcribe the reference audio. If you enter text or upload a file, it will override automatic transcription.)r™   Úinfor   rŸ   z$Load Reference Text from File (.txt)zRandomize SeedzRCheck to use a random seed for each generation. Uncheck to use the seed specified.rh   )r™   r¥   rY   rŸ   )Ú
show_labelrY   Ú	precisionrŸ   )rŸ   zRemove SilenceszPIf undesired long silence(s) produced, turn on to automatically detect and crop.)r™   r¥   rY   ÚSpeedg333333Ó?g       @ç      ð?gš™™™™™¹?zAdjust the speed of the audio.)r™   ÚminimumÚmaximumrY   Ústepr¥   z	NFE Stepsé@   z"Set the number of denoising steps.zCross-Fade Duration (s)g        g{®Gáz„?z7Set the duration of the cross-fade between audio clips.c                   C   ó   t jddS )NFr¤   )r\   Ú	Accordionr   r   r   r   Úcollapse_accordion  s   r°   )ÚfnÚinputsÚoutputszSynthesized Audio)r™   ÚSpectrogramc	                 C   s@   |r	t j dd¡}t| ||t|||||d	\}	}
}}|	|
||fS )Nr   rd   )rŒ   ri   rj   rk   )ro   rp   rq   r—   Útts_model_choice)Úref_audio_inputÚref_text_inputÚgen_text_inputr‹   Úrandomize_seedÚ
seed_inputÚcross_fade_duration_sliderÚ
nfe_sliderÚspeed_sliderÚ	audio_outr–   Úref_text_outr   r   r   r   Ú	basic_tts  s   ÷rÀ   )r²   r³   c                   C   ó   d d gS r   r   r   r   r   r   Ú<lambda>G  ó    rÂ   c              	   C   s®   d}t  || ¡}g }ddddœ}tt|ƒƒD ]>}|d dkr0||  ¡ }|r/||d< | |¡ q||  ¡ }zt |¡}W q tjj	yT   |d	d… }|dddœ}Y qw |S )
Nz	(\{.*?\})ÚRegularéÿÿÿÿr©   ©r~   rŒ   rk   r   r   rQ   r-   )
ÚreÚsplitÚrangerB   r5   Úappendr'   r(   ÚdecoderÚJSONDecodeError)rŠ   ÚpatternÚtokensÚsegmentsÚcurrent_type_dictÚirQ   Útype_strr   r   r   Úparse_speechtypes_text]  s,   ý
€þrÓ   a¤  
    # Multiple Speech-Type Generation

    This section allows you to generate multiple speech types or multiple people's voices. Enter your text in the format shown below, or upload a .txt file with the same format. The system will generate speech using the appropriate type. If unspecified, the model will use the regular speech type. The current speech type will be used until the next speech type is specified.
    a  
            **Example Input:** <br>
            {Regular} Hello, I'd like to order a sandwich please. <br>
            {Surprised} What do you mean you're out of bread? <br>
            {Sad} I really wanted a sandwich though... <br>
            {Angry} You know what, darn you and your little shop! <br>
            {Whisper} I'll just go back home and cry now. <br>
            {Shouting} Why me?!
            aÕ  
            **Example Input 2:** <br>
            {"name": "Speaker1_Happy", "seed": -1, "speed": 1} Hello, I'd like to order a sandwich please. <br>
            {"name": "Speaker2_Regular", "seed": -1, "speed": 1} Sorry, we're out of bread. <br>
            {"name": "Speaker1_Sad", "seed": -1, "speed": 1} I really wanted a sandwich though... <br>
            {"name": "Speaker2_Whisper", "seed": -1, "speed": 1} I'll give you the last one I was hiding.
            z¤Upload different audio clips for each speech type. The first speech type is mandatory. You can add additional speech types by clicking the "Add Speech Type" button.Úcompacté    )rŸ   Ú	min_widthrÄ   zSpeech Type Name)rY   r™   zInsert LabelÚ	secondaryzRegular Reference AudiozReference Text (Regular))r™   r   rÅ   iç  zSeed, -1 for random)r¦   rª   r«   rY   r¬   r¥   zAdjust the speed)r™   r    éd   )r£   ÚvisiblezDelete TypeÚstopzSeed. -1 for randomc                   C   rÁ   r   r   r   r   r   r   rÂ   ä  rÃ   zAdd Speech Typec                  C   sD   dd„ t tƒD ƒ} ttk rtjdd| t< td7 a| S t d¡ | S )Nc                 S   s   g | ]}t  ¡ ‘qS r   ©r\   r]   )rC   r”   r   r   r   rF   ö  s    z&add_speech_type_fn.<locals>.<listcomp>T©rÙ   r-   zCExhausted maximum number of speech types. Consider restart the app.)rÉ   Úmax_speech_typesÚspeech_type_countr\   r]   rn   )Úrow_updatesr   r   r   Úadd_speech_type_fnõ  s   
ÿrà   )r³   c                   C   s   t jddd d d d fS )NFrÜ   rÛ   r   r   r   r   Údelete_speech_type_fn  s   rá   ad  Enter the script with speaker names (or emotion types) at the start of each block, e.g.:

{Regular} Hello, I'd like to order a sandwich please.
{Surprised} What do you mean you're out of bread?
{Sad} I really wanted a sandwich though...
{Angry} You know what, darn you and your little shop!
{Whisper} I'll just go back home and cry now.
{Shouting} Why me?!)r™   r   rž   rŸ   Úplaceholderc                 C   s   dd„ }|S )Nc                 S   s<   | pd} |st  d¡ | S |||dœ}| t |¡ d }|S )Nr3   z,Please enter speech type name before insert.rÆ   ú )r\   rn   r'   Údumps)Úcurrent_textÚspeech_type_nameÚspeech_type_seedÚspeech_type_speedÚspeech_type_dictÚupdated_textr   r   r   Úinsert_speech_type_fn  s   
ýz9make_insert_speech_type_fn.<locals>.insert_speech_type_fnr   )Úindexrë   r   r   r   Úmake_insert_speech_type_fn  s   rí   zShow Cherry-pick InterfacezCTurn on to show interface, picking seeds from previous generations.z7Turn on to automatically detect and crop long silences.zGenerate Multi-Style SpeechzCherry-pick InterfaceÚcopy)r™   r   rž   ÚbuttonsÚinteractiverÙ   c                 C   s   t j| dS )NrÜ   rÛ   )Ú
is_visibler   r   r   rÂ   V  ó    c                    s2  |d t … }|t dt  … }|dt  dt  … }|dt   }tƒ ‰ d}t|||ƒD ] \}}}	|r:|r:||	dœˆ |< ndddœˆ d|› d< |d7 }q)t| ƒ}
g }d}d}|
D ]•}|d	 }|d
 }|d }|d }|ˆ v ro|}nt d|› d¡ d}zˆ | d }W n# ty¥   t d|› d¡ d g‡ fdd„ˆ D ƒ d g  Y   S w ˆ |  dd¡}|dkr¹tj	 
dd¡}t|||t||d|td	\}}}}|\}}| |¡ |ˆ | d< |t t|||d¡d|› d 7 }qV|rt |¡}||fg‡ fdd„ˆ D ƒ |g S t d¡ d g‡ fdd„ˆ D ƒ d g S )Nr   rh   r   )Úaudior‰   r3   ú@r-   rÄ   r~   rŒ   rk   rQ   zType z/ is not available, will use Regular as default.ró   z(Please provide reference audio for type Ú.c                    ó   g | ]}ˆ | d  ‘qS ©r‰   r   ©rC   r~   ©Úspeech_typesr   r   rF     ó    z.generate_multistyle_speech.<locals>.<listcomp>r‰   rÅ   rd   ©rŒ   ri   rk   re   rÆ   rã   Ú
c                    rö   r÷   r   rø   rù   r   r   rF   ­  rû   zNo audio generated.c                    rö   r÷   r   rø   rù   r   r   rF   ²  rû   )rÝ   r   rL   rÓ   r\   rn   ÚKeyErrorÚgetro   rp   rq   r—   rµ   ÚprintrÊ   r'   rä   r0   Úconcatenate)rŠ   ÚargsÚspeech_type_names_listÚspeech_type_audios_listÚspeech_type_ref_texts_listr‹   Úref_text_idxÚ
name_inputÚaudio_inputr·   rÏ   Úgenerated_audio_segmentsÚcurrent_type_nameÚinference_meta_dataÚsegmentr~   rº   rk   rQ   rŽ   r‰   r¾   r”   r¿   r   ÚsrÚ
audio_dataÚfinal_audio_datar   rù   r   Úgenerate_multistyle_speechb  sx   ÿ
&þ÷
&
ÿþÿ
r  c           	      G   sn   |}t ƒ }|r| |¡ |D ]	}|r| |¡ qt| ƒ}t dd„ |D ƒƒ}|| }|r1tjddS tjddS )Nc                 s   s    | ]}|d  V  qdS )r~   Nr   )rC   r  r   r   r   Ú	<genexpr>Ð  s   € z(validate_speech_types.<locals>.<genexpr>F)rð   T)ÚsetÚaddrÓ   r\   r]   )	rŠ   Úregular_namer  r  Úspeech_types_availabler  rÏ   Úspeech_types_in_textÚmissing_speech_typesr   r   r   Úvalidate_speech_typesÃ  s   

€r  a&  
# Voice Chat
Have a conversation with an AI using your reference voice!
1. Upload a reference audio clip and optionally its transcript (via text or .txt file).
2. Load the chat model.
3. Record your message through your microphone or type it.
4. The AI will respond using the reference voice.
zQwen/Qwen2.5-3B-Instructzmicrosoft/Phi-4-mini-instructc                 C   sx   t j}td urd ad at ¡  tj ¡  |d| › ƒ t	j
| dddat 
| ¡a|d| › dƒ t jddt jddfS )	NzLoading chat model: Úauto)Útorch_dtypeÚ
device_mapzChat model z loaded successfully!FrÜ   T)r\   ÚInfoÚchat_model_stateÚchat_tokenizer_stateÚgcÚcollectrr   ÚcudaÚempty_cacher   Úfrom_pretrainedr   r]   )Úchat_model_namere   r   r   r   Úload_chat_modelô  s   

r%  zChat Model Namez*Enter the name of a HuggingFace chat model)ÚchoicesrY   r™   r¥   Úallow_custom_valuezLoad Chat ModelrÜ   c                   C   r®   )NTrÜ   rÛ   r   r   r   r   rÂ     rò   Úhidden)Úshow_progressz(Optional: Leave blank to auto-transcribez"Uncheck to use the seed specified.)r™   rY   r¥   rŸ   )r™   rY   zSystem PromptzŸYou are not an AI assistant, you are whoever the user says you are. You must stay in character. Keep your responses concise since they will be spoken out loud.)r™   rY   r   ÚConversationzSpeak your message)ÚautoplayzType your messagezSend MessagezClear Conversationc                 C   sB   |s|  ¡ s| S |rt||ƒd }|  ¡ s| S |  d|dœ¡ | S )z$Handle audio or text input from userr-   Úuser©ÚroleÚcontent)r5   r   rÊ   )Ú
conv_stateÚ
audio_pathrQ   r   r   r   Úprocess_audio_inputR  s   r2  c                 C   s„   | D ]'}t |d tƒr)t|d ƒdkr|d d d dksJ ‚|d d d |d< qd|dœg}t||  ttƒ}|  d|dœ¡ | S )	zGenerate text response from AIr/  r-   r   rš   rQ   Úsystemr-  Ú	assistant)r7   ÚlistrB   rT   r  r  rÊ   )r0  Úsystem_promptÚsingle_stateÚsystem_prompt_stateÚresponser   r   r   Úgenerate_text_responseb  s   (€r:  c                 C   s‚   | r|s	d||fS | d d d d }|r| d d dkr"d||fS |r+t j dd¡}t|||t||d	d
td	\}}}	}
||	|
fS )z"Generate TTS audio for AI responseNrÅ   r/  r   rQ   r.  r4  rd   rb   r©   rü   )ro   rp   rq   r—   rµ   r   )r0  rŽ   r‰   r‹   r¹   rº   Úlast_ai_responseÚaudio_resultr”   r¿   r   r   r   r   Úgenerate_audio_responsep  s&   

÷
r=  c                   C   s   g dfS )zReset the conversationNr   r   r   r   r   Úclear_conversationŠ  s   r>  c                   C   rÁ   r   r   r   r   r   r   rÂ   ©  rÃ   a]  
# Credits

* [mrfakename](https://github.com/fakerybakery) for the original [online demo](https://huggingface.co/spaces/mrfakename/E2-F5-TTS)
* [RootingInLoad](https://github.com/RootingInLoad) for initial chunk generation and podcast app exploration
* [jpgallegoar](https://github.com/jpgallegoar) for multiple speech-type generation & voice chat
z
# F5-TTS Demo Space

This is z=a local web UI for [F5-TTS](https://github.com/SWivid/F5-TTS)z=an online demo for [F5-TTS](https://github.com/SWivid/F5-TTS)uõ   with advanced batch processing support. This app supports the following TTS models:

* [F5-TTS](https://arxiv.org/abs/2410.06885) (A Fairytaler that Fakes Fluent and Faithful Speech with Flow Matching)
* [E2 TTS](https://arxiv.org/abs/2406.18009) (Embarrassingly Easy Fully Non-Autoregressive Zero-Shot TTS)

The checkpoints currently support English and Chinese.

If you're having issues, try converting your reference audio to WAV or MP3, clipping it to 12s with  âœ‚  in the bottom right corner (otherwise might have non-optimal auto-trimmed result).

**NOTE: Reference text will be automatically transcribed with Whisper if not provided. For best results, keep your reference clips short (<12s). Ensure the audio is fully uploaded before generating.**
Úf5_ttsz/infer/.cache/last_used_custom_model_info_v1.txtc                  C   s|   z*g } t tddd}|D ]	}|  | ¡ ¡ qW d   ƒ | W S 1 s#w   Y  | W S  ty=   tjjddd t Y S w )NrU   rV   rW   T)ÚparentsÚexist_ok)rZ   Úlast_used_customrÊ   r5   ÚFileNotFoundErrorÚparentÚmkdirr&   )Úcustomr_   Úliner   r   r   Úload_last_used_customÔ  s   ÿ
ÿýþrH  c                 C   sn   | dkr$t ƒ \}}}d|||fatjd|dtjd|dtjd|dfS | atjddtjddtjddfS )Nrg   T)rÙ   rY   FrÜ   )rH  rµ   r\   r]   )Ú
new_choiceÚcustom_ckpt_pathÚcustom_vocab_pathÚcustom_model_cfgr   r   r   Úswitch_tts_modelß  s   ý"rM  c                 C   s^   d| ||fa ttddd}| | d | d | d ¡ W d   ƒ d S 1 s(w   Y  d S )Nrg   ÚwrV   rW   rý   )rµ   rZ   rB  r€   )rJ  rK  rL  r_   r   r   r   Úset_custom_modelí  s    "ÿrO  rf   rg   zChoose TTS Model)r&  r™   rY   z3Model: local_path | hf://user_id/repo_id/model_ckpt)r&  rY   r'  r™   rÙ   z3Vocab: local_path | hf://user_id/repo_id/vocab_file)r   r    r!   r"   r#   r.   r$   r/   i   é   é   zConfig: in a dictionary form)r²   r³   r)  )r²   r)  )z	Basic-TTSzMulti-Speechz
Voice-ChatÚCreditsz--portz-pzPort to run the app on)Údefaultrš   Úhelpz--hostz-HzHost to run the app on)rS  rT  z--sharez-sz#Share the app via Gradio share link)rS  Úis_flagrT  z--apiz-azAllow API accessz--root_pathz-ra$  The root path (or "mount point") of the application, if it's not served from the root ("/") of the domain. Often used when the application is behind a reverse proxy that forwards requests to the application, e.g. set "/myapp" or full URL for application served at "https://example.com/myapp".z--inbrowserz-iz=Automatically launch the interface in the default web browser)rU  rS  rT  c                 C   s(   t dƒ tj|dj|| |||d d S )NzStarting app...)Úapi_open)Úserver_nameÚserver_portÚshareÚ	root_pathÚ	inbrowser)r   ÚappÚqueueÚlaunch)ÚportÚhostrY  ÚapirZ  r[  r   r   r   ÚmainH  s   
ûrb  Ú__main__)r3   N)¿r  r'   rƒ   rÇ   r|   Úcollectionsr   Ú	functoolsr   Úimportlib.resourcesr   ÚclickÚgradior\   r‡   ro   Ú	soundfiler   rr   r   r   Útransformersr   r   r
   r	   ÚImportErrorr   Úf5_tts.infer.utils_inferr   r   r   r   r   r   r   Úf5_tts.modelr   r   rt   rµ   rä   r0   r&   rz   r+   r2   r%   r:   ru   rv   ry   rx   r  r  rT   r`   r  r—   ÚBlocksÚapp_ttsÚMarkdownÚAudior¶   ÚRowÚTextboxr¸   ÚFileÚgen_text_fileÚButtonÚgenerate_btnr¯   Ú	adv_settnr·   Úref_text_fileÚCheckboxr¹   ÚNumberrº   ÚColumnr‹   ÚSliderr½   r¼   r»   r°   r‚   Úaudio_outputÚImageÚspectrogram_outputrÀ   ÚuploadÚclearrÓ   Úapp_multistyleÚregular_rowr  Úregular_insertÚregular_audioÚregular_ref_textÚregular_seed_sliderÚregular_speed_sliderÚregular_ref_text_filerÝ   Úspeech_type_rowsÚspeech_type_namesÚspeech_type_audiosÚspeech_type_ref_textsÚspeech_type_ref_text_filesÚspeech_type_seedsÚspeech_type_speedsÚspeech_type_delete_btnsÚspeech_type_insert_btnsrÉ   rÑ   Úrowr  Ú
insert_btnÚ
delete_btnr  Úspeed_inputÚref_text_file_inputrÊ   Úadd_speech_type_btnrÞ   rà   rá   rB   Úgen_text_input_multistyleÚgen_text_file_multistylerí   Ú	enumerateÚ	insert_fnÚshow_cherrypick_multistyleÚremove_silence_multistyleÚgenerate_multistyle_btnÚaudio_output_multistyleÚcherrypick_interface_multistyleÚchanger  r  Úapp_chatÚchat_model_name_listr%  ÚDropdownÚchat_model_name_inputÚload_chat_model_btnÚchat_interface_containerÚref_audio_chatÚref_text_chatÚref_text_file_chatÚrandomize_seed_chatÚseed_input_chatÚremove_silence_chatÚsystem_prompt_chatÚChatbotÚchatbot_interfaceÚ
MicrophoneÚaudio_input_chatÚaudio_output_chatÚtext_input_chatÚsend_btn_chatÚclear_btn_chatr2  r:  r=  r>  Ústop_recordingÚsubmitÚuser_operationÚthenÚapp_creditsr\  ÚjoinpathrB  rH  rM  rO  ÚRadioÚchoose_tts_modelrJ  rK  rL  ÚTabbedInterfaceÚcommandÚoptionÚintrb  Ú__name__r]  r^  r   r   r   r   Ú<module>   sà  ÿ$	ý	

	öO

üù
üù
üýÿ€øúúúÙ0ý
ýýý÷
óˆ 
"ÿ
ÿÿóÿþÿ
ÿÿü€þ	ÿ€ñýÿ
ÿÿü€þ	ÿ€ð







ý
ý

û
þ
ûø

ý

ýÿ
ýÿ€ù€ÿú
ýý
Qÿýüûÿú	õ
ý  ¡  fÿþ
ûüÿ

ÿ

üÿù

üùþýé€ÿ€ý!ÿ

þû
þ
ú€ù


ýýùúîý
þÿ ì€Ç 
Tÿ

ýÿ
ÿ
ÿûûøÿøÿòáé9üýýý
þ ý 	ûûû

ü