Update modules/langchain_assistant/voice_module.py
Browse files
modules/langchain_assistant/voice_module.py
CHANGED
|
@@ -142,14 +142,23 @@ INTERFACE_JS = """
|
|
| 142 |
async function initVoiceDuplex() {
|
| 143 |
console.log("🚀 Inicializando puente de audio WebRTC/WebSocket...");
|
| 144 |
|
| 145 |
-
// Cambiar
|
| 146 |
let wsProto = window.location.protocol === "https:" ? "wss:" : "ws:";
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
window.voiceSocket = new WebSocket(wsUrl);
|
| 150 |
window.audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 24000 });
|
| 151 |
|
| 152 |
-
//
|
| 153 |
window.voiceSocket.onmessage = async (event) => {
|
| 154 |
if (event.data instanceof Blob) {
|
| 155 |
const arrayBuffer = await event.data.arrayBuffer();
|
|
@@ -158,11 +167,11 @@ async function initVoiceDuplex() {
|
|
| 158 |
source.buffer = buffer;
|
| 159 |
source.connect(window.audioCtx.destination);
|
| 160 |
source.start(0);
|
| 161 |
-
}, (err) => { console.log("Ignorando
|
| 162 |
}
|
| 163 |
};
|
| 164 |
|
| 165 |
-
//
|
| 166 |
try {
|
| 167 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } });
|
| 168 |
const source = window.audioCtx.createMediaStreamSource(stream);
|
|
@@ -175,7 +184,7 @@ async function initVoiceDuplex() {
|
|
| 175 |
if (window.voiceSocket.readyState === WebSocket.OPEN) {
|
| 176 |
const float32Samples = e.inputBuffer.getChannelData(0);
|
| 177 |
|
| 178 |
-
// Conversión matemática rápida a Int16
|
| 179 |
const int16Buffer = new Int16Array(float32Samples.length);
|
| 180 |
for (let i = 0; i < float32Samples.length; i++) {
|
| 181 |
let s = Math.max(-1, Math.min(1, float32Samples[i]));
|
|
@@ -197,7 +206,6 @@ function closeVoiceDuplex() {
|
|
| 197 |
if (window.audioCtx) { window.audioCtx.close(); }
|
| 198 |
}
|
| 199 |
"""
|
| 200 |
-
|
| 201 |
# =====================================================================
|
| 202 |
# ── SECCIÓN 05: INTERFAZ VISUAL (GRADIO TAB - HUD CYBERNOVA) ──
|
| 203 |
# Renderiza los elementos estéticos y mapea los triggers hacia JavaScript.
|
|
|
|
| 142 |
async function initVoiceDuplex() {
|
| 143 |
console.log("🚀 Inicializando puente de audio WebRTC/WebSocket...");
|
| 144 |
|
| 145 |
+
// Cambiar el protocolo dinámicamente y redirigir el tráfico al puerto seguro de voz 7861
|
| 146 |
let wsProto = window.location.protocol === "https:" ? "wss:" : "ws:";
|
| 147 |
+
|
| 148 |
+
// Si estamos en Hugging Face, los puertos se mapean mediante subdominios o paths relativos directos
|
| 149 |
+
let wsUrl = "";
|
| 150 |
+
if (window.location.host.includes("hf.space")) {
|
| 151 |
+
// Formato directo para túneles multiplexados en Hugging Face Spaces
|
| 152 |
+
wsUrl = `${wsProto}//${window.location.host}/voice-stream`;
|
| 153 |
+
} else {
|
| 154 |
+
// Formato para ejecución local en tu entorno de desarrollo
|
| 155 |
+
wsUrl = `${wsProto}//${window.location.hostname}:7861/voice-stream`;
|
| 156 |
+
}
|
| 157 |
|
| 158 |
window.voiceSocket = new WebSocket(wsUrl);
|
| 159 |
window.audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 24000 });
|
| 160 |
|
| 161 |
+
// Configurar la reproducción en vivo (Audio de Salida)
|
| 162 |
window.voiceSocket.onmessage = async (event) => {
|
| 163 |
if (event.data instanceof Blob) {
|
| 164 |
const arrayBuffer = await event.data.arrayBuffer();
|
|
|
|
| 167 |
source.buffer = buffer;
|
| 168 |
source.connect(window.audioCtx.destination);
|
| 169 |
source.start(0);
|
| 170 |
+
}, (err) => { console.log("Ignorando buffers crudos de flujo continuo."); });
|
| 171 |
}
|
| 172 |
};
|
| 173 |
|
| 174 |
+
// Capturar Hardware del Micrófono (Audio de Entrada)
|
| 175 |
try {
|
| 176 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } });
|
| 177 |
const source = window.audioCtx.createMediaStreamSource(stream);
|
|
|
|
| 184 |
if (window.voiceSocket.readyState === WebSocket.OPEN) {
|
| 185 |
const float32Samples = e.inputBuffer.getChannelData(0);
|
| 186 |
|
| 187 |
+
// Conversión matemática rápida a Int16
|
| 188 |
const int16Buffer = new Int16Array(float32Samples.length);
|
| 189 |
for (let i = 0; i < float32Samples.length; i++) {
|
| 190 |
let s = Math.max(-1, Math.min(1, float32Samples[i]));
|
|
|
|
| 206 |
if (window.audioCtx) { window.audioCtx.close(); }
|
| 207 |
}
|
| 208 |
"""
|
|
|
|
| 209 |
# =====================================================================
|
| 210 |
# ── SECCIÓN 05: INTERFAZ VISUAL (GRADIO TAB - HUD CYBERNOVA) ──
|
| 211 |
# Renderiza los elementos estéticos y mapea los triggers hacia JavaScript.
|