v89: чанкованный HTTP POST <10KB — обход DPI (proxy_pass fix + /v1/transcribe)
This commit is contained in:
Vendored
+29
-40
@@ -250,13 +250,12 @@ async function playHistorySyl(recId, sylIdx, total) {
|
||||
|
||||
</script>
|
||||
<script>
|
||||
// ---------- Groq API: транскрипция (WebSocket, base64-чанки — обход DPI) ----------
|
||||
// ---------- Groq API: транскрипция (HTTP-чанки <10KB — обход DPI) ----------
|
||||
|
||||
async function transcribe(blob) {
|
||||
if (!GROQ_API_KEY) return { text: '', error: 'no_key' };
|
||||
const tStart = performance.now();
|
||||
|
||||
// Кодируем blob в base64 (текстовые WS-фреймы — DPI не режет)
|
||||
const base64 = await new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result.split(',')[1]);
|
||||
@@ -264,47 +263,37 @@ async function transcribe(blob) {
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
|
||||
const CHUNK = 4096; // 4KB base64 на чанк
|
||||
const CHUNK = 3000; // ~4KB base64 на чанк (POST <10KB — DPI не режет)
|
||||
const total = Math.ceil(base64.length / CHUNK);
|
||||
const mime = blob.type || 'audio/webm';
|
||||
console.log('[WS] blob=' + (blob.size/1024).toFixed(1) + 'KB b64=' + base64.length + ' chunks=' + total);
|
||||
console.log('[CHUNK] blob=' + (blob.size/1024).toFixed(1) + 'KB b64=' + base64.length + ' chunks=' + total);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
const ws = new WebSocket('wss://lang.kube5s.ru/ws');
|
||||
const timeout = setTimeout(() => {
|
||||
console.log('[WS] timeout'); ws.close(); resolve({ text: '', error: 'timeout' });
|
||||
}, 60000);
|
||||
|
||||
ws.onopen = async () => {
|
||||
console.log('[WS] connected, sending ' + total + ' chunks');
|
||||
for (let i = 0; i < base64.length; i += CHUNK) {
|
||||
ws.send(JSON.stringify({
|
||||
c: Math.floor(i / CHUNK),
|
||||
b: base64.slice(i, i + CHUNK),
|
||||
t: GROQ_API_KEY,
|
||||
m: mime,
|
||||
n: total
|
||||
}));
|
||||
// Даём браузеру протолкнуть буфер — Chrome может терять фреймы без этого
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
}
|
||||
ws.send(JSON.stringify({ c: -1 }));
|
||||
console.log('[WS] all sent');
|
||||
};
|
||||
|
||||
ws.onmessage = (e) => {
|
||||
clearTimeout(timeout); ws.close();
|
||||
const data = JSON.parse(e.data);
|
||||
console.log('[WS] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||
resolve(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null });
|
||||
};
|
||||
|
||||
ws.onerror = () => { clearTimeout(timeout); ws.close(); resolve({ text: '', error: 'network' }); };
|
||||
} catch (err) {
|
||||
resolve({ text: '', error: 'network' });
|
||||
let sid = '';
|
||||
try {
|
||||
for (let i = 0; i < base64.length; i += CHUNK) {
|
||||
const idx = Math.floor(i / CHUNK);
|
||||
const body = JSON.stringify({
|
||||
idx, total,
|
||||
chunk: base64.slice(i, i + CHUNK),
|
||||
mime, token: GROQ_API_KEY,
|
||||
sid
|
||||
});
|
||||
const r = await fetch('https://lang.kube5s.ru/openai/v1/transcribe', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body
|
||||
});
|
||||
const data = await r.json();
|
||||
if (idx === total - 1) {
|
||||
console.log('[CHUNK] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||
return data.error ? { text: '', error: data.error } : { text: data.text || '', error: null };
|
||||
}
|
||||
sid = data.sid || '';
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
console.log('[CHUNK] ERR', e);
|
||||
return { text: '', error: 'network' };
|
||||
}
|
||||
}
|
||||
|
||||
async function translateToRussian(text) {
|
||||
@@ -805,7 +794,7 @@ async function doCompare(blob, originalText, duration) {
|
||||
<script>
|
||||
// ---------- Глобальные переменные и инициализация ----------
|
||||
const GROQ_API_KEY = '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';
|
||||
const VERSION = 'v88';
|
||||
const VERSION = 'v89';
|
||||
|
||||
let mediaRecorder, audioChunks = [], userAudioBlob = null, ttsText = '';
|
||||
let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null;
|
||||
|
||||
Reference in New Issue
Block a user