diff --git a/js/main.js b/js/main.js index fe68feb..67bcc99 100644 --- a/js/main.js +++ b/js/main.js @@ -1,6 +1,6 @@ // ---------- Глобальные переменные и инициализация ---------- const GROQ_API_KEY = ''; -const VERSION = 'v88'; +const VERSION = 'v89'; let mediaRecorder, audioChunks = [], userAudioBlob = null, ttsText = ''; let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null; diff --git a/js/transcribe.js b/js/transcribe.js index a120f37..5489e61 100644 --- a/js/transcribe.js +++ b/js/transcribe.js @@ -1,10 +1,9 @@ -// ---------- 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]); @@ -12,47 +11,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) { diff --git a/server/lang.kube5s.ru.conf b/server/lang.kube5s.ru.conf index 605b06c..7055322 100644 --- a/server/lang.kube5s.ru.conf +++ b/server/lang.kube5s.ru.conf @@ -25,7 +25,7 @@ server { # Groq API proxy (multipart POST) location /openai/ { - proxy_pass http://127.0.0.1:8765; + proxy_pass http://127.0.0.1:8765/; proxy_http_version 1.1; proxy_set_header Host $host; proxy_read_timeout 120s; diff --git a/server/proxy.py b/server/proxy.py index 863b61b..78cd179 100644 --- a/server/proxy.py +++ b/server/proxy.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 from flask import Flask, request, Response import requests +import base64 +import uuid +import time app = Flask(__name__) GROQ_BASE = "https://api.groq.com" @@ -11,10 +14,72 @@ CORS = { "Access-Control-Allow-Headers": "Authorization, Content-Type", } +# Чанковая сборка аудио (DPI bypass — каждый POST <10KB) +_sessions = {} # session_id -> {chunks, mime, total, token, expires} + @app.route("/", methods=["OPTIONS"]) def options(path): return Response(status=204, headers=CORS) +@app.route("/v1/transcribe", methods=["POST", "OPTIONS"]) +def transcribe_chunked(): + if request.method == "OPTIONS": + return Response(status=204, headers=CORS) + + data = request.get_json(force=True) or {} + idx = data.get("idx", 0) + total = data.get("total", 1) + chunk = data.get("chunk", "") + mime = data.get("mime", "audio/webm") + token = data.get("token", "") + sid = data.get("sid", "") + + # Очистка старых сессий + now = time.time() + for k in list(_sessions.keys()): + if _sessions[k]["expires"] < now: + del _sessions[k] + + if not sid: + sid = uuid.uuid4().hex[:12] + _sessions[sid] = {"chunks": {}, "mime": mime, "total": total, "token": token, "expires": now + 120} + else: + s = _sessions.get(sid) + if not s: + return Response('{"error":"session not found"}', status=404, headers=CORS, content_type="application/json") + + _sessions[sid]["chunks"][idx] = chunk + _sessions[sid]["expires"] = now + 120 + + if len(_sessions[sid]["chunks"]) >= total: + s = _sessions.pop(sid) + audio_b64 = "".join(s["chunks"][i] for i in sorted(s["chunks"])) + audio_data = base64.b64decode(audio_b64) + + ext = "webm" + if "ogg" in s["mime"]: ext = "ogg" + elif "mp4" in s["mime"] or "aac" in s["mime"]: ext = "mp4" + elif "wav" in s["mime"]: ext = "wav" + + boundary = "----ChunkedBoundary" + uuid.uuid4().hex[:16] + payload = b"".join([ + f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="audio.{ext}"\r\nContent-Type: {s["mime"]}\r\n\r\n'.encode(), + audio_data, + f'\r\n--{boundary}\r\nContent-Disposition: form-data; name="model"\r\n\r\nwhisper-large-v3'.encode(), + f'\r\n--{boundary}\r\nContent-Disposition: form-data; name="language"\r\n\r\nit'.encode(), + f'\r\n--{boundary}--\r\n'.encode(), + ]) + + r = requests.post(f"{GROQ_BASE}/openai/v1/audio/transcriptions", + headers={"Authorization": f"Bearer {s['token']}", "Content-Type": f"multipart/form-data; boundary={boundary}"}, + data=payload, timeout=60) + result = r.json() + out = dict(CORS) + out["Content-Type"] = "application/json" + return Response(r.text, status=r.status_code, headers=out) + else: + return Response(f'{{"ok":true,"sid":"{sid}"}}', status=200, headers=CORS, content_type="application/json") + @app.route("/", methods=["GET", "POST", "PUT", "DELETE", "PATCH"]) def proxy(subpath): url = f"{GROQ_BASE}/{subpath}"