v47: WS base64 chunks — только текстовые фреймы, DPI не режет
This commit is contained in:
+23
-34
@@ -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 = 'v46';
|
const VERSION = 'v47';
|
||||||
document.getElementById('ver').textContent = VERSION;
|
document.getElementById('ver').textContent = VERSION;
|
||||||
|
|
||||||
// ---------- Перевод на русский (Groq) ----------
|
// ---------- Перевод на русский (Groq) ----------
|
||||||
@@ -612,56 +612,45 @@ 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();
|
const tStart = performance.now();
|
||||||
|
|
||||||
// Small file: direct POST
|
// Кодируем в base64 — только текстовые WS-фреймы, DPI не режет
|
||||||
if (blob.size <= 14000) {
|
const base64 = await new Promise((resolve, reject) => {
|
||||||
const formData = new FormData();
|
const reader = new FileReader();
|
||||||
formData.append('file', new File([blob], 'audio.webm', { type: 'audio/webm' }));
|
reader.onload = () => resolve(reader.result.split(',')[1]);
|
||||||
formData.append('model', 'whisper-large-v3');
|
reader.onerror = reject;
|
||||||
formData.append('language', 'it');
|
reader.readAsDataURL(blob);
|
||||||
try {
|
});
|
||||||
const ctl = new AbortController();
|
|
||||||
const t = setTimeout(() => ctl.abort(), 60000);
|
const CHUNK = 4096; // 4KB на чанк
|
||||||
const res = await fetch('https://proxy.kube5s.ru/openai/v1/audio/transcriptions', {
|
const total = Math.ceil(base64.length / CHUNK);
|
||||||
method: 'POST', headers: { 'Authorization': `Bearer ${GROQ_API_KEY}` },
|
console.log('[WS] blob=' + (blob.size/1024).toFixed(1) + 'KB b64=' + base64.length + ' chunks=' + total);
|
||||||
body: formData, signal: ctl.signal
|
|
||||||
});
|
|
||||||
clearTimeout(t);
|
|
||||||
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' };
|
|
||||||
return { text: data.text || '', error: null };
|
|
||||||
} catch (err) {
|
|
||||||
return { text: '', error: err.name === 'AbortError' ? 'timeout' : 'network' };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Large file: WebSocket (entire blob, no chunks)
|
|
||||||
console.log('[WS] opening, blob=' + (blob.size/1024).toFixed(1) + 'KB');
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
try {
|
try {
|
||||||
const ws = new WebSocket('wss://proxy.kube5s.ru/ws');
|
const ws = new WebSocket('wss://proxy.kube5s.ru/ws');
|
||||||
const timeout = setTimeout(() => { console.log('[WS] timeout'); ws.close(); resolve({ text: '', error: 'timeout' }); }, 60000);
|
const timeout = setTimeout(() => {
|
||||||
|
console.log('[WS] timeout'); ws.close(); resolve({ text: '', error: 'timeout' });
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
console.log('[WS] connected');
|
console.log('[WS] connected, chunks=' + total);
|
||||||
ws.send(JSON.stringify({ token: GROQ_API_KEY }));
|
ws.send(JSON.stringify({ token: GROQ_API_KEY, total, mime: blob.type || 'audio/webm' }));
|
||||||
ws.send(blob); // Blob directly — no async/await needed
|
for (let i = 0; i < base64.length; i += CHUNK) {
|
||||||
console.log('[WS] blob sent ' + (blob.size/1024).toFixed(1) + 'KB');
|
ws.send(JSON.stringify({ chunk: base64.slice(i, i + CHUNK) }));
|
||||||
|
}
|
||||||
|
console.log('[WS] all sent');
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onmessage = (e) => {
|
ws.onmessage = (e) => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
ws.close();
|
ws.close();
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws-b64 ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||||
if (data.error) resolve({ text: '', error: data.error });
|
if (data.error) resolve({ text: '', error: data.error });
|
||||||
else resolve({ text: data.text || '', error: null });
|
else resolve({ text: data.text || '', error: null });
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onerror = () => {
|
ws.onerror = () => {
|
||||||
console.log('[WS] error');
|
console.log('[WS] error'); clearTimeout(timeout); resolve({ text: '', error: 'network' });
|
||||||
clearTimeout(timeout);
|
|
||||||
resolve({ text: '', error: 'network' });
|
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
resolve({ text: '', error: 'network' });
|
resolve({ text: '', error: 'network' });
|
||||||
|
|||||||
Reference in New Issue
Block a user