@@ -123,8 +123,7 @@ <h1 class="text-center">ChatTTS WebUI & API<span class="fs-6">(v{{version}})</sp
123123 $ ( '[data-toggle="tooltip"]' ) . tooltip ( ) ;
124124
125125 let audioGenerated = false ;
126- let jsCode = '' ;
127-
126+
128127 $ ( '#temper_wrap input[type="range"]' ) . change ( function ( ) {
129128 $ ( this ) . next ( ) . text ( ( $ ( this ) . val ( ) ) ) ;
130129 } ) ;
@@ -146,86 +145,70 @@ <h1 class="text-center">ChatTTS WebUI & API<span class="fs-6">(v{{version}})</sp
146145 custom_voice :custom_voice ?parseInt ( custom_voice ) :0
147146 } ;
148147
149- $ . ajax ( {
150- url : '/tts' ,
151- type : 'POST' ,
152- data : data ,
153- timeout : 3600000 ,
154- success : function ( response ) {
155- if ( response . code === 0 ) {
156- console . log ( response ) ;
157- if ( response . audio_files ) {
158- response . audio_files . forEach ( function ( audio , index ) {
159- let pos = audio . filename . lastIndexOf ( '/' ) + 1 ;
160- let filename = audio . filename . substr ( pos ) ;
161- let audioId = `audio-${ index } ` ;
162- let buttonId = `show-js-btn-${ index } ` ;
163- let jsCode = `# API调用代码
148+ $ . ajax ( {
149+ url : '/tts' ,
150+ type : 'POST' ,
151+ data : data ,
152+ timeout : 3600000 ,
153+ success : function ( response ) {
154+ if ( response . code === 0 ) {
155+ console . log ( response ) ;
156+ if ( response . audio_files ) {
157+ response . audio_files . forEach ( function ( audio , index ) {
158+ let pos = audio . filename . lastIndexOf ( '/' ) + 1 ;
159+ let filename = audio . filename . substr ( pos ) ;
160+ let jsCode = `# API调用代码
164161
165162import requests
166163
167- res= requests.post('http://${ location . host } /tts',data=${ JSON . stringify ( data ) } )
164+ res = requests.post('http://${ location . host } /tts', data=${ JSON . stringify ( data , null , 2 ) } )
168165print(res.json())
169166
170167#ok
171- {code:0,msg:'ok',audio_files:[{filename:${ audio . filename } ,url:${ audio . url } }]}
168+ {code:0, msg:'ok', audio_files:[{filename: ${ audio . filename } , url: ${ audio . url } }]}
172169
173170#error
174- {code:1,msg:"error"}
171+ {code:1, msg:"error"}
175172` ;
176173
177- $ ( '#audio-container' ) . append ( `
178- <div class="col-12 mb-2 pb-2 border-bottom" id="${ audioId } ">
179- <div class="fs-6 text-secondary">${ filename } </div>
180- <audio controls src="${ audio . url } "></audio>
181- <div>推理时间: ${ audio . inference_time } 秒</div>
182- <div>生成时间: ${ audio . audio_duration } 秒</div>
183-
184- <button id="${ buttonId } " class="btn btn-info mt-2">显示API调用</button>
185- <div class="m-2 bg-black text-white d-none" id="code-${ index } "></div>
186-
187- </div>
188- ` ) ;
189-
190- $ ( `#${ buttonId } ` ) . click ( function ( ) {
191- let codeId = `#code-${ index } ` ;
192- $ ( codeId ) . toggleClass ( 'd-none' ) ;
193- if ( ! $ ( codeId ) . hasClass ( 'd-none' ) ) {
194- $ ( codeId ) . html ( `<pre><code>${ jsCode } </code></pre>` ) ;
195- }
196- } ) ;
197- } ) ;
198-
199- audioGenerated = true ;
200- } else {
201- layer . alert ( '音频文件生成失败' , { title : false } ) ;
202- }
203- } else {
204- layer . alert ( response . msg , { title :false } ) ;
205- }
206- } ,
207- error : function ( xhr , status , error ) {
208- layer . alert ( '发生错误: ' + error , { title :false } ) ;
209- } ,
210- complete :function ( ) {
211- layer . close ( index )
212- }
213- } ) ;
214- }
215- } ) ;
216-
217- // Show API call example button
218- $ ( '#show-js-btn' ) . click ( function ( ) {
219- if ( audioGenerated ) {
220- $ ( '#code' ) . toggleClass ( 'd-none' ) ;
221- $ ( '#show-js-btn' ) . toggleClass ( 'btn-warning' ) ;
222- if ( ! $ ( '#code' ) . hasClass ( 'd-none' ) ) {
223- $ ( '#code' ) . html ( `<pre><code>${ jsCode } </code></pre>` ) ;
174+ $ ( '#audio-container' ) . append ( `
175+ <div class="col-12 mb-2 pb-2 border-bottom">
176+ <div class="fs-6 text-secondary">${ filename } </div>
177+ <audio controls src="${ audio . url } "></audio>
178+ <div>推理时长: ${ audio . inference_time } 秒</div>
179+ <div>音频时长: ${ audio . audio_duration } 秒</div>
180+ <button class="btn btn-info mt-2 show-js-btn" data-js-code="${ encodeURIComponent ( jsCode ) } ">显示API调用</button>
181+ <div class="m-2 bg-black text-white d-none code-container"></div>
182+ </div>
183+ ` ) ;
184+ } ) ;
185+
186+ audioGenerated = true ;
187+ } else {
188+ layer . alert ( '音频文件生成失败' , { title : false } ) ;
224189 }
225- } else {
226- layer . alert ( '请先生成音频文件' , { title : false } ) ;
190+ } else {
191+ layer . alert ( response . msg , { title : false } ) ;
192+ }
193+ } ,
194+ error : function ( xhr , status , error ) {
195+ layer . alert ( '发生错误: ' + error , { title : false } ) ;
196+ } ,
197+ complete : function ( ) {
198+ layer . close ( index ) ;
227199 }
228- } ) ;
200+ } ) ;
201+ }
202+ } ) ;
203+
204+ $ ( document ) . on ( 'click' , '.show-js-btn' , function ( ) {
205+ let codeContainer = $ ( this ) . next ( '.code-container' ) ;
206+ codeContainer . toggleClass ( 'd-none' ) ;
207+ if ( ! codeContainer . hasClass ( 'd-none' ) ) {
208+ codeContainer . html ( `<pre><code>${ decodeURIComponent ( $ ( this ) . data ( 'js-code' ) ) } </code></pre>` ) ;
209+ }
210+ } ) ;
211+
229212
230213 $ ( '#upload-btn' ) . click ( function ( ) {
231214 $ ( '#file-input' ) . click ( ) ; // Trigger file input when upload button is clicked
0 commit comments