Spaces:
Running
Running
Update main.ts
Browse files
main.ts
CHANGED
@@ -1,154 +1,449 @@
|
|
1 |
-
import { serve } from "https://deno.land/std/http/server.ts";
|
2 |
-
import { EdgeSpeechTTS } from "https://esm.sh/@lobehub/tts@1";
|
3 |
-
|
4 |
-
const AUTH_TOKEN = Deno.env.get("AUTH_TOKEN");
|
5 |
-
const VOICES_URL = "https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list?trustedclienttoken=6A5AA1D4EAFF4E9FB37E23D68491D6F4";
|
6 |
-
|
7 |
-
async function fetchVoiceList() {
|
8 |
-
const response = await fetch(VOICES_URL);
|
9 |
-
const voices = await response.json();
|
10 |
-
return voices.reduce((acc: Record<string, { model: string, name: string, friendlyName: string, locale: string }[]>, voice: any) => {
|
11 |
-
const { ShortName: model, ShortName: name, FriendlyName: friendlyName, Locale: locale } = voice;
|
12 |
-
if (!acc[locale]) acc[locale] = [];
|
13 |
-
acc[locale].push({ model, name, friendlyName, locale });
|
14 |
-
return acc;
|
15 |
-
}, {});
|
16 |
-
}
|
17 |
-
|
18 |
-
async function synthesizeSpeech(model: string, voice: string, text: string) {
|
19 |
-
let voiceName;
|
20 |
-
let rate = 0;
|
21 |
-
let pitch = 0;
|
22 |
-
|
23 |
-
if (model.includes("tts")) {
|
24 |
-
rate = 0.1;
|
25 |
-
pitch = 0.2;
|
26 |
-
|
27 |
-
switch (voice) {
|
28 |
-
case "alloy":
|
29 |
-
voiceName = "zh-CN-YunjianNeural";
|
30 |
-
break;
|
31 |
-
case "echo":
|
32 |
-
voiceName = "zh-CN-YunyangNeural";
|
33 |
-
break;
|
34 |
-
case "fable":
|
35 |
-
voiceName = "zh-CN-XiaoxiaoNeural";
|
36 |
-
break;
|
37 |
-
default:
|
38 |
-
voiceName = "zh-CN-YunxiNeural";
|
39 |
-
break;
|
40 |
-
}
|
41 |
-
} else {
|
42 |
-
voiceName = model;
|
43 |
-
const params = Object.fromEntries(
|
44 |
-
voice.split("|").map((p) => p.split(":") as [string, string])
|
45 |
-
);
|
46 |
-
rate = Number(params["rate"] || 0);
|
47 |
-
pitch = Number(params["pitch"] || 0);
|
48 |
-
}
|
49 |
-
|
50 |
-
const tts = new EdgeSpeechTTS();
|
51 |
-
|
52 |
-
const payload = {
|
53 |
-
input: text,
|
54 |
-
options: {
|
55 |
-
rate: rate,
|
56 |
-
pitch: pitch,
|
57 |
-
voice: voiceName
|
58 |
-
},
|
59 |
-
};
|
60 |
-
const response = await tts.create(payload);
|
61 |
-
const mp3Buffer = new Uint8Array(await response.arrayBuffer());
|
62 |
-
|
63 |
-
console.log(`Successfully synthesized speech, returning audio/mpeg response`);
|
64 |
-
return new Response(mp3Buffer, {
|
65 |
-
headers: { "Content-Type": "audio/mpeg" },
|
66 |
-
});
|
67 |
-
}
|
68 |
-
|
69 |
-
function
|
70 |
-
const
|
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 |
});
|
|
|
1 |
+
import { serve } from "https://deno.land/std/http/server.ts";
|
2 |
+
import { EdgeSpeechTTS } from "https://esm.sh/@lobehub/tts@1";
|
3 |
+
|
4 |
+
const AUTH_TOKEN = Deno.env.get("AUTH_TOKEN");
|
5 |
+
const VOICES_URL = "https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list?trustedclienttoken=6A5AA1D4EAFF4E9FB37E23D68491D6F4";
|
6 |
+
|
7 |
+
async function fetchVoiceList() {
|
8 |
+
const response = await fetch(VOICES_URL);
|
9 |
+
const voices = await response.json();
|
10 |
+
return voices.reduce((acc: Record<string, { model: string, name: string, friendlyName: string, locale: string }[]>, voice: any) => {
|
11 |
+
const { ShortName: model, ShortName: name, FriendlyName: friendlyName, Locale: locale } = voice;
|
12 |
+
if (!acc[locale]) acc[locale] = [];
|
13 |
+
acc[locale].push({ model, name, friendlyName, locale });
|
14 |
+
return acc;
|
15 |
+
}, {});
|
16 |
+
}
|
17 |
+
|
18 |
+
async function synthesizeSpeech(model: string, voice: string, text: string) {
|
19 |
+
let voiceName;
|
20 |
+
let rate = 0;
|
21 |
+
let pitch = 0;
|
22 |
+
|
23 |
+
if (model.includes("tts")) {
|
24 |
+
rate = 0.1;
|
25 |
+
pitch = 0.2;
|
26 |
+
|
27 |
+
switch (voice) {
|
28 |
+
case "alloy":
|
29 |
+
voiceName = "zh-CN-YunjianNeural";
|
30 |
+
break;
|
31 |
+
case "echo":
|
32 |
+
voiceName = "zh-CN-YunyangNeural";
|
33 |
+
break;
|
34 |
+
case "fable":
|
35 |
+
voiceName = "zh-CN-XiaoxiaoNeural";
|
36 |
+
break;
|
37 |
+
default:
|
38 |
+
voiceName = "zh-CN-YunxiNeural";
|
39 |
+
break;
|
40 |
+
}
|
41 |
+
} else {
|
42 |
+
voiceName = model;
|
43 |
+
const params = Object.fromEntries(
|
44 |
+
voice.split("|").map((p) => p.split(":") as [string, string])
|
45 |
+
);
|
46 |
+
rate = Number(params["rate"] || 0);
|
47 |
+
pitch = Number(params["pitch"] || 0);
|
48 |
+
}
|
49 |
+
|
50 |
+
const tts = new EdgeSpeechTTS();
|
51 |
+
|
52 |
+
const payload = {
|
53 |
+
input: text,
|
54 |
+
options: {
|
55 |
+
rate: rate,
|
56 |
+
pitch: pitch,
|
57 |
+
voice: voiceName
|
58 |
+
},
|
59 |
+
};
|
60 |
+
const response = await tts.create(payload);
|
61 |
+
const mp3Buffer = new Uint8Array(await response.arrayBuffer());
|
62 |
+
|
63 |
+
console.log(`Successfully synthesized speech, returning audio/mpeg response`);
|
64 |
+
return new Response(mp3Buffer, {
|
65 |
+
headers: { "Content-Type": "audio/mpeg" },
|
66 |
+
});
|
67 |
+
}
|
68 |
+
|
69 |
+
function validateContentType(req: Request, expected: string) {
|
70 |
+
const contentType = req.headers.get("Content-Type");
|
71 |
+
if (contentType !== expected) {
|
72 |
+
console.log(`Invalid Content-Type ${contentType}, expected ${expected}`);
|
73 |
+
return new Response("Bad Request", { status: 400 });
|
74 |
+
}
|
75 |
+
}
|
76 |
+
|
77 |
+
async function handleDebugRequest(req: Request) {
|
78 |
+
const url = new URL(req.url);
|
79 |
+
const voice = url.searchParams.get("voice") || "";
|
80 |
+
const model = url.searchParams.get("model") || "";
|
81 |
+
const text = url.searchParams.get("text") || "";
|
82 |
+
|
83 |
+
console.log(`Debug request with model=${model}, voice=${voice}, text=${text}`);
|
84 |
+
|
85 |
+
if (!voice || !model || !text) {
|
86 |
+
console.log("Missing required parameters");
|
87 |
+
return new Response("Bad Request", { status: 400 });
|
88 |
+
}
|
89 |
+
|
90 |
+
return synthesizeSpeech(model, voice, text);
|
91 |
+
}
|
92 |
+
|
93 |
+
async function handleSynthesisRequest(req: Request) {
|
94 |
+
|
95 |
+
if (req.method !== "POST") {
|
96 |
+
console.log(`Invalid method ${req.method}, expected POST`);
|
97 |
+
return new Response("Method Not Allowed", { status: 405 });
|
98 |
+
}
|
99 |
+
|
100 |
+
const invalidContentType = validateContentType(req, "application/json");
|
101 |
+
if (invalidContentType) return invalidContentType;
|
102 |
+
|
103 |
+
const { model, input, voice } = await req.json();
|
104 |
+
console.log(`Synthesis request with model=${model}, input=${input}, voice=${voice}`);
|
105 |
+
|
106 |
+
return synthesizeSpeech(model, voice, input);
|
107 |
+
}
|
108 |
+
|
109 |
+
|
110 |
+
async function handleDemoRequest(req: Request) {
|
111 |
+
const groupedVoiceList = await fetchVoiceList();
|
112 |
+
|
113 |
+
const html = `<!DOCTYPE html>
|
114 |
+
<html lang="en">
|
115 |
+
<head>
|
116 |
+
<meta charset="UTF-8" />
|
117 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
118 |
+
<title>tts</title>
|
119 |
+
<link
|
120 |
+
href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap"
|
121 |
+
rel="stylesheet"
|
122 |
+
/>
|
123 |
+
<style>
|
124 |
+
:root {
|
125 |
+
--primary-color: #6c8bd6;
|
126 |
+
--primary-light: #a2b3e3;
|
127 |
+
--primary-dark: #3d5b8f;
|
128 |
+
--secondary-color: #f08080;
|
129 |
+
--text-color: #333;
|
130 |
+
--text-secondary: #777;
|
131 |
+
--bg-color: #fff;
|
132 |
+
}
|
133 |
+
body {
|
134 |
+
font-family: "Noto Sans SC", "Arial", sans-serif;
|
135 |
+
color: var(--text-color);
|
136 |
+
margin: 0;
|
137 |
+
padding: 0;
|
138 |
+
display: flex;
|
139 |
+
justify-content: center;
|
140 |
+
background-color: #fafafa;
|
141 |
+
background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
142 |
+
position: relative;
|
143 |
+
overflow: hidden;
|
144 |
+
}
|
145 |
+
body::before {
|
146 |
+
content: "";
|
147 |
+
position: absolute;
|
148 |
+
top: 0;
|
149 |
+
left: 0;
|
150 |
+
right: 0;
|
151 |
+
bottom: 0;
|
152 |
+
background: repeating-radial-gradient(
|
153 |
+
circle at 50% 50%,
|
154 |
+
rgba(255, 255, 255, 0.8) 0%,
|
155 |
+
rgba(255, 255, 255, 0.8) 2%,
|
156 |
+
transparent 2%,
|
157 |
+
transparent 4%,
|
158 |
+
rgba(255, 255, 255, 0.8) 4%,
|
159 |
+
rgba(255, 255, 255, 0.8) 6%,
|
160 |
+
transparent 6%,
|
161 |
+
transparent 8%,
|
162 |
+
rgba(255, 255, 255, 0.8) 8%,
|
163 |
+
rgba(255, 255, 255, 0.8) 10%,
|
164 |
+
transparent 10%
|
165 |
+
),
|
166 |
+
repeating-linear-gradient(
|
167 |
+
45deg,
|
168 |
+
#d4f4ff 0%,
|
169 |
+
#d4f4ff 5%,
|
170 |
+
#e6f9ff 5%,
|
171 |
+
#e6f9ff 10%,
|
172 |
+
#f0faff 10%,
|
173 |
+
#f0faff 15%,
|
174 |
+
#e6f9ff 15%,
|
175 |
+
#e6f9ff 20%,
|
176 |
+
#d4f4ff 20%,
|
177 |
+
#d4f4ff 25%
|
178 |
+
);
|
179 |
+
background-blend-mode: multiply;
|
180 |
+
opacity: 0.8;
|
181 |
+
z-index: -1;
|
182 |
+
animation: glitch 15s infinite;
|
183 |
+
}
|
184 |
+
.container {
|
185 |
+
display: flex;
|
186 |
+
max-width: 1200px;
|
187 |
+
width: 100%;
|
188 |
+
margin: 40px;
|
189 |
+
background: #fff;
|
190 |
+
border-radius: 12px;
|
191 |
+
position: relative;
|
192 |
+
background-color: rgba(255, 255, 255, 0.8);
|
193 |
+
z-index: 1;
|
194 |
+
}
|
195 |
+
@keyframes glitch {
|
196 |
+
0% {
|
197 |
+
background-position: 0 0, 0 0;
|
198 |
+
filter: hue-rotate(0deg);
|
199 |
+
}
|
200 |
+
50% {
|
201 |
+
background-position: 10px 10px, -10px 10px;
|
202 |
+
filter: hue-rotate(360deg);
|
203 |
+
}
|
204 |
+
100% {
|
205 |
+
background-position: 0 0, 0 0;
|
206 |
+
filter: hue-rotate(0deg);
|
207 |
+
}
|
208 |
+
}
|
209 |
+
.input-area,
|
210 |
+
.output-area {
|
211 |
+
padding: 30px;
|
212 |
+
width: 50%;
|
213 |
+
}
|
214 |
+
.input-area {
|
215 |
+
border-right: 1px solid #e0e0e0;
|
216 |
+
}
|
217 |
+
h1 {
|
218 |
+
font-size: 36px;
|
219 |
+
color: var(--primary-color);
|
220 |
+
margin-bottom: 30px;
|
221 |
+
}
|
222 |
+
.filter-section {
|
223 |
+
margin-bottom: 30px;
|
224 |
+
}
|
225 |
+
.filter-section label {
|
226 |
+
display: block;
|
227 |
+
font-size: 16px;
|
228 |
+
color: var(--text-secondary);
|
229 |
+
margin-bottom: 10px;
|
230 |
+
}
|
231 |
+
.filter-section input {
|
232 |
+
font-size: 16px;
|
233 |
+
padding: 10px 15px;
|
234 |
+
border: 2px solid var(--primary-light);
|
235 |
+
border-radius: 8px;
|
236 |
+
outline: none;
|
237 |
+
transition: border-color 0.3s, box-shadow 0.3s;
|
238 |
+
width: 100%;
|
239 |
+
box-sizing: border-box;
|
240 |
+
}
|
241 |
+
.filter-section input:focus {
|
242 |
+
border-color: var(--primary-color);
|
243 |
+
box-shadow: 0 0 0 2px var(--primary-light);
|
244 |
+
}
|
245 |
+
.slider-container {
|
246 |
+
margin-bottom: 30px;
|
247 |
+
}
|
248 |
+
.slider-container label {
|
249 |
+
display: block;
|
250 |
+
font-size: 16px;
|
251 |
+
color: var(--text-secondary);
|
252 |
+
margin-bottom: 10px;
|
253 |
+
}
|
254 |
+
.slider {
|
255 |
+
-webkit-appearance: none;
|
256 |
+
width: 100%;
|
257 |
+
height: 10px;
|
258 |
+
border-radius: 5px;
|
259 |
+
background: linear-gradient(
|
260 |
+
to right,
|
261 |
+
var(--secondary-color) 0%,
|
262 |
+
var(--primary-color) 50%,
|
263 |
+
var(--primary-light) 100%
|
264 |
+
);
|
265 |
+
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1),
|
266 |
+
0 1px rgba(255, 255, 255, 0.1);
|
267 |
+
outline: none;
|
268 |
+
opacity: 0.7;
|
269 |
+
-webkit-transition: 0.2s;
|
270 |
+
transition: opacity 0.2s;
|
271 |
+
margin-bottom: 10px;
|
272 |
+
}
|
273 |
+
.slider:hover {
|
274 |
+
opacity: 1;
|
275 |
+
}
|
276 |
+
.slider::-webkit-slider-thumb {
|
277 |
+
-webkit-appearance: none;
|
278 |
+
appearance: none;
|
279 |
+
width: 20px;
|
280 |
+
height: 20px;
|
281 |
+
border-radius: 50%;
|
282 |
+
background: #fff;
|
283 |
+
border: 2px solid var(--primary-color);
|
284 |
+
cursor: pointer;
|
285 |
+
}
|
286 |
+
.slider::-moz-range-thumb {
|
287 |
+
width: 20px;
|
288 |
+
height: 20px;
|
289 |
+
border-radius: 50%;
|
290 |
+
background: #fff;
|
291 |
+
border: 2px solid var(--primary-color);
|
292 |
+
cursor: pointer;
|
293 |
+
}
|
294 |
+
.slider-value {
|
295 |
+
font-size: 14px;
|
296 |
+
color: var(--text-secondary);
|
297 |
+
}
|
298 |
+
.textarea-container {
|
299 |
+
margin-bottom: 30px;
|
300 |
+
}
|
301 |
+
.textarea-container label {
|
302 |
+
display: block;
|
303 |
+
font-size: 18px;
|
304 |
+
margin-bottom: 10px;
|
305 |
+
}
|
306 |
+
.textarea-container textarea {
|
307 |
+
width: 100%;
|
308 |
+
padding: 10px;
|
309 |
+
font-size: 16px;
|
310 |
+
border: 2px solid var(--primary-light);
|
311 |
+
border-radius: 8px;
|
312 |
+
outline: none;
|
313 |
+
resize: vertical;
|
314 |
+
transition: border-color 0.3s, box-shadow 0.3s;
|
315 |
+
box-sizing: border-box;
|
316 |
+
height: 200px;
|
317 |
+
}
|
318 |
+
.textarea-container textarea:focus {
|
319 |
+
border-color: var(--primary-color);
|
320 |
+
box-shadow: 0 0 0 2px var(--primary-light);
|
321 |
+
}
|
322 |
+
.voice-group {
|
323 |
+
margin-bottom: 20px;
|
324 |
+
border: 2px solid var(--primary-light);
|
325 |
+
border-radius: 12px;
|
326 |
+
overflow: hidden;
|
327 |
+
cursor: move;
|
328 |
+
background: #fff;
|
329 |
+
}
|
330 |
+
.voice-header {
|
331 |
+
padding: 15px 20px;
|
332 |
+
font-size: 18px;
|
333 |
+
background: var(--primary-light);
|
334 |
+
color: #fff;
|
335 |
+
cursor: pointer;
|
336 |
+
display: flex;
|
337 |
+
justify-content: space-between;
|
338 |
+
align-items: center;
|
339 |
+
}
|
340 |
+
.voice-header:hover {
|
341 |
+
background: var(--primary-color);
|
342 |
+
}
|
343 |
+
.voice-buttons {
|
344 |
+
padding: 20px;
|
345 |
+
display: none;
|
346 |
+
gap: 12px;
|
347 |
+
flex-wrap: wrap;
|
348 |
+
}
|
349 |
+
.voice-button {
|
350 |
+
background: var(--secondary-color);
|
351 |
+
color: #fff;
|
352 |
+
border: none;
|
353 |
+
padding: 10px 20px;
|
354 |
+
border-radius: 50px;
|
355 |
+
cursor: pointer;
|
356 |
+
transition: filter 0.3s;
|
357 |
+
}
|
358 |
+
.voice-button:hover {
|
359 |
+
filter: brightness(0.9);
|
360 |
+
}
|
361 |
+
.chevron {
|
362 |
+
transition: transform 0.3s;
|
363 |
+
}
|
364 |
+
.voice-group.open .voice-buttons {
|
365 |
+
display: flex;
|
366 |
+
}
|
367 |
+
.voice-group.open .chevron {
|
368 |
+
transform: rotate(180deg);
|
369 |
+
}
|
370 |
+
.dragging {
|
371 |
+
opacity: 0.5;
|
372 |
+
}
|
373 |
+
</style>
|
374 |
+
</head>
|
375 |
+
<body>
|
376 |
+
<div class="container">
|
377 |
+
<div class="input-area">
|
378 |
+
<div class="filter-section">
|
379 |
+
<label for="keywords">фильтр по языкам:</label
|
380 |
+
><input type="text" id="keywords" value="multilingual,-RU" />
|
381 |
+
</div>
|
382 |
+
<div class="slider-container">
|
383 |
+
<label for="rate">скорость:</label
|
384 |
+
><input
|
385 |
+
type="range"
|
386 |
+
min="-1"
|
387 |
+
max="1"
|
388 |
+
step="0.1"
|
389 |
+
value="-0.1"
|
390 |
+
class="slider"
|
391 |
+
id="rate"
|
392 |
+
/>
|
393 |
+
<div class="slider-value" id="rateValue">-0.1</div>
|
394 |
+
<label for="pitch">тон:</label
|
395 |
+
><input
|
396 |
+
type="range"
|
397 |
+
min="-1"
|
398 |
+
max="1"
|
399 |
+
step="0.1"
|
400 |
+
value="0.1"
|
401 |
+
class="slider"
|
402 |
+
id="pitch"
|
403 |
+
/>
|
404 |
+
<div class="slider-value" id="pitchValue">0.1</div>
|
405 |
+
</div>
|
406 |
+
<div class="textarea-container">
|
407 |
+
<label for="inputText">текст:</label
|
408 |
+
><textarea id="inputText">Привет, хочешь я расскажу сказку?</textarea>
|
409 |
+
</div>
|
410 |
+
</div>
|
411 |
+
<div class="output-area">
|
412 |
+
<h1>голос</h1>
|
413 |
+
<div id="voices"></div>
|
414 |
+
</div>
|
415 |
+
</div>" +
|
416 |
+
"<script>const voiceList = ${JSON.stringify(groupedVoiceList)};let audio=null;function filterVoices(){const keywords=document.getElementById('keywords').value.split(',').map(k=>k.trim().toLowerCase());const voicesDiv=document.getElementById('voices');voicesDiv.innerHTML='';const filteredVoices={};for(const[locale,voices]of Object.entries(voiceList)){const filtered=voices.filter(({name,friendlyName})=>keywords.some(keyword=>name.toLowerCase().includes(keyword)||friendlyName.toLowerCase().includes(keyword)));if(filtered.length>0){filteredVoices[locale]=filtered}}for(const[locale,voices]of Object.entries(filteredVoices)){const group=document.createElement('div');group.className='voice-group';group.draggable=true;const header=document.createElement('div');header.className='voice-header';header.textContent=locale.toUpperCase();const chevron=document.createElement('span');chevron.className='chevron';chevron.innerHTML='▼';header.appendChild(chevron);const buttonsContainer=document.createElement('div');buttonsContainer.className='voice-buttons';voices.forEach(({model,name})=>{const button=document.createElement('button');button.className='voice-button';button.textContent=name;button.onclick=()=>synthesize(model);buttonsContainer.appendChild(button)});header.onclick=()=>{group.classList.toggle('open')};group.appendChild(header);group.appendChild(buttonsContainer);voicesDiv.appendChild(group)}addDragDropListeners()}function synthesize(model){const text=document.getElementById('inputText').value||'Hello world';const rate=document.getElementById('rate').value||'-0.1';const pitch=document.getElementById('pitch').value||'0.1';const voice=\`rate:\${rate}|pitch:\${pitch}\`;if(audio){audio.pause();audio.currentTime=0}fetch('/v1/audio/speech',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({model,input:text,voice})}).then(response=>response.blob()).then(blob=>{const audioUrl=URL.createObjectURL(blob);audio=new Audio(audioUrl);audio.play()})}function addDragDropListeners(){const voicesDiv=document.getElementById('voices');let draggedItem=null;voicesDiv.addEventListener('dragstart',e=>{draggedItem=e.target;e.target.classList.add('dragging')});voicesDiv.addEventListener('dragend',e=>{e.target.classList.remove('dragging');draggedItem=null});voicesDiv.addEventListener('dragover',e=>{e.preventDefault();const afterElement=getDragAfterElement(voicesDiv,e.clientY);if(afterElement==null){voicesDiv.appendChild(draggedItem)}else{voicesDiv.insertBefore(draggedItem,afterElement)}})}function getDragAfterElement(container,y){const draggableElements=[...container.querySelectorAll('.voice-group:not(.dragging)')];return draggableElements.reduce((closest,child)=>{const box=child.getBoundingClientRect();const offset=y-box.top-box.height/2;if(offset<0&&offset>closest.offset){return{offset:offset,element:child}}else{return closest}},{offset:Number.NEGATIVE_INFINITY}).element}filterVoices();document.getElementById('keywords').addEventListener('input',filterVoices);const rateSlider=document.getElementById('rate');const rateValue=document.getElementById('rateValue');rateSlider.oninput=function(){rateValue.innerHTML=this.value};const pitchSlider=document.getElementById('pitch');const pitchValue=document.getElementById('pitchValue');pitchSlider.oninput=function(){pitchValue.innerHTML=this.value}</script>" +
|
417 |
+
"</body></html>`;
|
418 |
+
|
419 |
+
return new Response(html, {
|
420 |
+
headers: { "Content-Type": "text/html" },
|
421 |
+
});
|
422 |
+
}
|
423 |
+
|
424 |
+
|
425 |
+
serve(async (req) => {
|
426 |
+
try {
|
427 |
+
const url = new URL(req.url);
|
428 |
+
|
429 |
+
if (url.pathname === "/") {
|
430 |
+
return handleDemoRequest(req);
|
431 |
+
}
|
432 |
+
|
433 |
+
if (url.pathname === "/tts") {
|
434 |
+
return handleDebugRequest(req);
|
435 |
+
}
|
436 |
+
|
437 |
+
if (url.pathname !== "/v1/audio/speech") {
|
438 |
+
console.log(`Unhandled path ${url.pathname}`);
|
439 |
+
return new Response("Not Found", { status: 404 });
|
440 |
+
}
|
441 |
+
|
442 |
+
return handleSynthesisRequest(req);
|
443 |
+
} catch (err) {
|
444 |
+
console.error(`Error processing request: ${err.message}`);
|
445 |
+
return new Response(`Internal Server Error\n${err.message}`, {
|
446 |
+
status: 500,
|
447 |
+
});
|
448 |
+
}
|
449 |
});
|