o
    㥵im                     @   s  U 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	 d dl
mZmZ d dlmZmZ dd Zedkre ZejZedB ed	< edu rqejZejZedu r[g Znd
d eD Zedu rig Zndd eD Zng Zg Z	 ejdd eeeD eejejej ej!ej"ej#ej$ej%ej&dZ'ed!i e'Z(ej)ej*ej+e(ej,dej$dej- dddZ.e.j/dkrpej$r/e0 Z1ej2Z3e1j4e3ej5ej6ddZ7e4ej8 ddZ9e9:ej5 e9;e1<e3 e9=ej6 dZ>z/e.j?ddD ]Z@e@r	e7Ae@ e9Be@ qe>se7C  dZ>qW e7D  e1E  e9D  dS e7D  e1E  e9D  w e.jFZGej8 dej ZHe4eHdZIeIAeG W d   n	1 sQw   Y  ejJeHejdZKej	rfe	eK eLdeH d dS eLd e.j/  eLe.M  dS dS )"    N)AudioSegment)play)audio_to_bytesread_ref_text)ServeReferenceAudioServeTTSRequestc                  C   s  t jdt jd} | jddtddd | jdd	td
dd | jddtd dd | jddtdd dd | jddtdd dd | jddtddd | jdt jd
dd | jdtg d d!d" | jd#td$d$d%gd&d' | jd(td)d*d | jd+td,d-d | jd.td/d0d | jd1td2d3d | jd4td/d5d | jd6td7d8d | jd9td:d;d | jd<td=d>d | jd?td@dAd@gdBd' | jdCtd dDd | jdEtdFdGd | 	 S )HNzCSend a WAV file and text to a server and receive synthesized audio.)descriptionformatter_classz--urlz-uzhttp://127.0.0.1:8080/v1/ttszURL of the server)typedefaulthelpz--textz-tTzText to be synthesized)r
   requiredr   z--reference_idz-idzgID of the reference model to be used for the speech
(Local: name of folder containing audios and files)z--reference_audioz-ra+zPath to the audio file)r
   nargsr   r   z--reference_textz-rtz"Reference text for voice synthesisz--outputz-ogenerated_audiozOutput audio file namez--playz*Whether to play audio after receiving data)actionr   r   z--format)wavmp3flacr   )r
   choicesr   z	--latencynormalbalancedzUsed in api.fish.audio/v1/tts)r
   r   r   r   z--max_new_tokens   z2Maximum new tokens to generate. 
0 means no limit.z--chunk_lengthi,  zChunk length for synthesisz--top_pg?zTop-p sampling for synthesisz--repetition_penaltyg?z Repetition penalty for synthesisz--temperaturezTemperature for samplingz--streamingFzEnable streaming responsez
--channels   zNumber of audio channelsz--rateiD  zSample rate for audioz--use_memory_cacheoffonz*Cache encoded references codes in memory.
z--seedza`None` means randomized inference, otherwise deterministic.
It can't be used for fixing a timbre.z	--api_keyYOUR_API_KEYzAPI key for authentication)
argparseArgumentParserRawTextHelpFormatteradd_argumentstrBooleanOptionalActionintfloatbool
parse_args)parser r(   D/home/ubuntu/.local/lib/python3.10/site-packages/tools/api_client.pyr&      s   
r&   __main__idstrc                 C      g | ]}t |qS r(   )r   ).0	ref_audior(   r(   r)   
<listcomp>       r/   c                 C   r,   r(   )r   )r-   ref_textr(   r(   r)   r/      r0   c                 C   s(   g | ]\}}t |d ur|nd|dqS )N    )audiotext)r   )r-   r1   r.   r(   r(   r)   r/      s    )r4   
referencesreference_idformatmax_new_tokenschunk_lengthtop_prepetition_penaltytemperature	streaminguse_memory_cacheseed)optionzBearer zapplication/msgpack)authorizationzcontent-type)datastreamheaders   T)r7   channelsrateoutputz.wavwbFr   )
chunk_size.)r7   zAudio has been saved to 'z'.z Request failed with status code r(   )Nr   base64wave	ormsgpackpyaudiorequestspydubr   pydub.playbackr   fish_speech.utils.filer   r   fish_speech.utils.schemar   r   r&   __name__argsr6   r+   r!   __annotations__reference_audio
ref_audiosreference_text	ref_textsbyte_audiosr4   zipr7   r8   r9   r:   r;   r<   r=   r>   r?   rB   pydantic_dataposturlpackbOPT_SERIALIZE_PYDANTICapi_keyresponsestatus_codePyAudioppaInt16audio_formatopenrF   rG   rC   rH   wfsetnchannelssetsampwidthget_sample_sizesetframeratestream_stopped_flagiter_contentchunkwritewriteframesrawstop_streamclose	terminatecontentaudio_content
audio_path
audio_file	from_filer3   printjsonr(   r(   r(   r)   <module>   s   
 
t


	
