File size: 3,567 Bytes
01e655b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import axios from "axios";

class APIClient {
  constructor(baseURL) {
    this.client = axios.create({
      baseURL: baseURL,
      headers: {
        "Content-Type": "application/json",
      },
    });
  }

  async synthesizeTTS({
    text,
    spk = "female2",
    style = "chat",
    temperature = 0.3,
    top_P = 0.7,
    top_K = 20,
    seed = 34060637,
    format = "mp3",
    prompt1 = "",
    prompt2 = "",
    prefix = "",
  }) {
    try {
      const response = await this.client.get("/v1/tts", {
        params: {
          text,
          spk,
          style,
          temperature,
          top_P,
          top_K,
          seed,
          format,
          prompt1,
          prompt2,
          prefix,
        },
        responseType: "blob", // Important for handling binary data
      });
      return response.data;
    } catch (error) {
      console.error("Error synthesizing TTS:", error);
      throw error;
    }
  }

  /**
   *
   * @param {string} ssml - The SSML text to synthesize
   * @param {string} format - The format of the synthesized audio (e.g. "mp3")
   */
  async synthesizeSSML({ ssml, format = "mp3" }) {
    try {
      const response = await this.client.post(
        "/v1/ssml",
        {
          ssml,
          format,
        },
        {
          responseType: "blob", // Important for handling binary data
        }
      );
      return response.data;
    } catch (error) {
      console.error("Error synthesizing SSML:", error);
      throw error;
    }
  }

  async openaiSpeechAPI({
    input,
    model = "chattts-4w",
    voice = "female2",
    response_format = "mp3",
    speed = 1,
  }) {
    try {
      const response = await this.client.post(
        "/v1/audio/speech",
        {
          input,
          model,
          voice,
          response_format,
          speed,
        },
        {
          responseType: "blob", // Important for handling binary data
        }
      );
      return response.data;
    } catch (error) {
      console.error("Error generating speech audio:", error);
      throw error;
    }
  }

  async refineText({
    text,
    prompt = "[oral_2][laugh_0][break_6]",
    seed = -1,
    top_P = 0.7,
    top_K = 20,
    temperature = 0.7,
    repetition_penalty = 1.0,
    max_new_token = 384,
  }) {
    try {
      const response = await this.client.post("/v1/prompt/refine", {
        text,
        prompt,
        seed,
        top_P,
        top_K,
        temperature,
        repetition_penalty,
        max_new_token,
      });
      return response.data;
    } catch (error) {
      console.error("Error refining text:", error);
      throw error;
    }
  }

  async listStyles() {
    try {
      const response = await this.client.get("/v1/styles/list");
      return response.data;
    } catch (error) {
      console.error("Error listing styles:", error);
      throw error;
    }
  }

  async listSpeakers() {
    try {
      const response = await this.client.get("/v1/speakers/list");
      return response.data;
    } catch (error) {
      console.error("Error listing speakers:", error);
      throw error;
    }
  }

  async createSpeaker({ name, seed }) {
    try {
      const response = await this.client.post("/v1/speaker/create", {
        name,
        seed,
      });
      return response.data;
    } catch (error) {
      console.error("Error creating speaker:", error);
      throw error;
    }
  }
}

export const client = new APIClient(
  localStorage.getItem("__chattts_playground_api_base__") ||
    `${window.location.origin}`
);