v43: WebSocket upload for large files (bypasses HTTP POST buffering)
This commit is contained in:
+49
-8
@@ -53,7 +53,7 @@ let mediaRecorder, audioChunks = [], userAudioBlob = null, ttsText = '';
|
|||||||
let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null;
|
let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null;
|
||||||
let recordStartTime = 0, recordDuration = 0;
|
let recordStartTime = 0, recordDuration = 0;
|
||||||
// ---------- Версия ----------
|
// ---------- Версия ----------
|
||||||
const VERSION = 'v40';
|
const VERSION = 'v43';
|
||||||
document.getElementById('ver').textContent = VERSION;
|
document.getElementById('ver').textContent = VERSION;
|
||||||
|
|
||||||
// ---------- Перевод на русский (Groq) ----------
|
// ---------- Перевод на русский (Groq) ----------
|
||||||
@@ -608,28 +608,69 @@ document.getElementById('btnPlayUser').onclick = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const CHUNK_SIZE = 4096;
|
||||||
|
|
||||||
async function transcribe(blob) {
|
async function transcribe(blob) {
|
||||||
if (!GROQ_API_KEY) return { text: '', error: 'no_key' };
|
if (!GROQ_API_KEY) return { text: '', error: 'no_key' };
|
||||||
|
const tStart = performance.now();
|
||||||
|
|
||||||
|
// Small file: direct POST
|
||||||
|
if (blob.size <= 14000) {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', new File([blob], 'audio.webm', { type: 'audio/webm' }));
|
formData.append('file', new File([blob], 'audio.webm', { type: 'audio/webm' }));
|
||||||
formData.append('model', 'whisper-large-v3');
|
formData.append('model', 'whisper-large-v3');
|
||||||
formData.append('language', 'it');
|
formData.append('language', 'it');
|
||||||
try {
|
try {
|
||||||
const controller = new AbortController();
|
const ctl = new AbortController();
|
||||||
const timer = setTimeout(() => controller.abort(), 60000);
|
const t = setTimeout(() => ctl.abort(), 60000);
|
||||||
const res = await fetch('https://proxy.kube5s.ru/openai/v1/audio/transcriptions', {
|
const res = await fetch('https://proxy.kube5s.ru/openai/v1/audio/transcriptions', {
|
||||||
method: 'POST',
|
method: 'POST', headers: { 'Authorization': `Bearer ${GROQ_API_KEY}` },
|
||||||
headers: { 'Authorization': `Bearer ${GROQ_API_KEY}` },
|
body: formData, signal: ctl.signal
|
||||||
body: formData,
|
|
||||||
signal: controller.signal
|
|
||||||
});
|
});
|
||||||
clearTimeout(timer);
|
clearTimeout(t);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB direct ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||||
if (data.error) return { text: '', error: data.error.message || 'forbidden' };
|
if (data.error) return { text: '', error: data.error.message || 'forbidden' };
|
||||||
return { text: data.text || '', error: null };
|
return { text: data.text || '', error: null };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return { text: '', error: err.name === 'AbortError' ? 'timeout' : 'network' };
|
return { text: '', error: err.name === 'AbortError' ? 'timeout' : 'network' };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Large file: WebSocket
|
||||||
|
const total = Math.ceil(blob.size / CHUNK_SIZE);
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
try {
|
||||||
|
const ws = new WebSocket('wss://proxy.kube5s.ru/ws');
|
||||||
|
const timeout = setTimeout(() => { ws.close(); resolve({ text: '', error: 'timeout' }); }, 60000);
|
||||||
|
|
||||||
|
ws.onopen = () => {
|
||||||
|
ws.send(JSON.stringify({ token: GROQ_API_KEY, chunks: total }));
|
||||||
|
(async () => {
|
||||||
|
for (let i = 0; i < total; i++) {
|
||||||
|
const chunk = blob.slice(i * CHUNK_SIZE, Math.min((i + 1) * CHUNK_SIZE, blob.size));
|
||||||
|
ws.send(await chunk.arrayBuffer());
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = (e) => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
ws.close();
|
||||||
|
const data = JSON.parse(e.data);
|
||||||
|
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws/' + total + 'chunks ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||||
|
if (data.error) resolve({ text: '', error: data.error });
|
||||||
|
else resolve({ text: data.text || '', error: null });
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onerror = () => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
resolve({ text: '', error: 'network' });
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
resolve({ text: '', error: 'network' });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalize(text) {
|
function normalize(text) {
|
||||||
|
|||||||
Reference in New Issue
Block a user