53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import sys, json, base64
|
|
import numpy as np
|
|
import traceback
|
|
|
|
def main():
|
|
from RealtimeSTT import AudioToTextRecorder
|
|
from scipy.signal import resample
|
|
import websockets
|
|
import threading
|
|
import logging
|
|
import wave
|
|
import json
|
|
import time
|
|
|
|
# Inicializá el modelo (usa tu clase personalizada)
|
|
model = AudioToTextRecorder(
|
|
model="base",
|
|
compute_type="int8",
|
|
language="es"
|
|
)
|
|
|
|
print(json.dumps({"type": "log", "msg": "✅ Worker iniciado con modelo Whisper"}), flush=True)
|
|
|
|
for line in sys.stdin:
|
|
try:
|
|
request = json.loads(line.strip())
|
|
if request["event"] == "transcribe":
|
|
sample_rate = request.get("sample_rate", 16000)
|
|
audio_data = base64.b64decode(request["audio_base64"])
|
|
|
|
audio_float32 = (
|
|
np.frombuffer(audio_data, dtype=np.int16).astype(np.float32) / 32768.0
|
|
)
|
|
|
|
segments, _ = model.transcribe(audio_float32, sample_rate=sample_rate)
|
|
text = " ".join([s.text for s in segments if s.text.strip()])
|
|
print(json.dumps({"type": "realtime", "text": text}), flush=True)
|
|
elif request["event"] == "set_param":
|
|
setattr(model, request["param"], request["value"])
|
|
print(json.dumps({"type": "log", "msg": f"Set {request['param']} to {request['value']}"}), flush=True)
|
|
|
|
else:
|
|
print(json.dumps({"type": "log", "msg": f"Evento desconocido: {request.get('event')}"}), flush=True)
|
|
|
|
except Exception as e:
|
|
print(json.dumps({
|
|
"type": "error",
|
|
"error": str(e),
|
|
"trace": traceback.format_exc()
|
|
}), flush=True)
|
|
|
|
if __name__ == "__main__":
|
|
main() |