lenML/ChatTTS-Forge
301
1import axios from "axios";2 3class APIClient {4 constructor(baseURL) {5 this.client = axios.create({6 baseURL: baseURL,7 headers: {8 "Content-Type": "application/json",9 },10 });11 }12 13 async synthesizeTTS({14 text,15 spk = "female2",16 style = "chat",17 temperature = 0.3,18 top_P = 0.7,19 top_K = 20,20 seed = 34060637,21 format = "mp3",22 prompt1 = "",23 prompt2 = "",24 prefix = "",25 }) {26 try {27 const response = await this.client.get("/v1/tts", {28 params: {29 text,30 spk,31 style,32 temperature,33 top_P,34 top_K,35 seed,36 format,37 prompt1,38 prompt2,39 prefix,40 },41 responseType: "blob", // Important for handling binary data42 });43 return response.data;44 } catch (error) {45 console.error("Error synthesizing TTS:", error);46 throw error;47 }48 }49 50 /**51 *52 * @param {string} ssml - The SSML text to synthesize53 * @param {string} format - The format of the synthesized audio (e.g. "mp3")54 */55 async synthesizeSSML({ ssml, format = "mp3" }) {56 try {57 const response = await this.client.post(58 "/v1/ssml",59 {60 ssml,61 format,62 },63 {64 responseType: "blob", // Important for handling binary data65 }66 );67 return response.data;68 } catch (error) {69 console.error("Error synthesizing SSML:", error);70 throw error;71 }72 }73 74 async openaiSpeechAPI({75 input,76 model = "chattts-4w",77 voice = "female2",78 response_format = "mp3",79 speed = 1,80 }) {81 try {82 const response = await this.client.post(83 "/v1/audio/speech",84 {85 input,86 model,87 voice,88 response_format,89 speed,90 },91 {92 responseType: "blob", // Important for handling binary data93 }94 );95 return response.data;96 } catch (error) {97 console.error("Error generating speech audio:", error);98 throw error;99 }100 }101 102 async refineText({103 text,104 prompt = "[oral_2][laugh_0][break_6]",105 seed = -1,106 top_P = 0.7,107 top_K = 20,108 temperature = 0.7,109 repetition_penalty = 1.0,110 max_new_token = 384,111 }) {112 try {113 const response = await this.client.post("/v1/prompt/refine", {114 text,115 prompt,116 seed,117 top_P,118 top_K,119 temperature,120 repetition_penalty,121 max_new_token,122 });123 return response.data;124 } catch (error) {125 console.error("Error refining text:", error);126 throw error;127 }128 }129 130 async listStyles() {131 try {132 const response = await this.client.get("/v1/styles/list");133 return response.data;134 } catch (error) {135 console.error("Error listing styles:", error);136 throw error;137 }138 }139 140 async listSpeakers() {141 try {142 const response = await this.client.get("/v1/speakers/list");143 return response.data;144 } catch (error) {145 console.error("Error listing speakers:", error);146 throw error;147 }148 }149 150 async createSpeaker({ name, seed }) {151 try {152 const response = await this.client.post("/v1/speaker/create", {153 name,154 seed,155 });156 return response.data;157 } catch (error) {158 console.error("Error creating speaker:", error);159 throw error;160 }161 }162}163 164export const client = new APIClient(165 localStorage.getItem("__chattts_playground_api_base__") ||166 `${window.location.origin}`167);168 