Update run.py
Browse files
run.py
CHANGED
|
@@ -1 +1,110 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
if __name__ == '__main__':
|
| 2 |
+
print("Starting server, please wait...")
|
| 3 |
+
|
| 4 |
+
from RealtimeSTT import AudioToTextRecorder
|
| 5 |
+
import asyncio
|
| 6 |
+
import websockets
|
| 7 |
+
import threading
|
| 8 |
+
import numpy as np
|
| 9 |
+
from scipy.signal import resample
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
recorder = None
|
| 13 |
+
recorder_ready = threading.Event()
|
| 14 |
+
client_websocket = None
|
| 15 |
+
|
| 16 |
+
async def send_to_client(message):
|
| 17 |
+
if client_websocket:
|
| 18 |
+
await client_websocket.send(message)
|
| 19 |
+
|
| 20 |
+
def text_detected(text):
|
| 21 |
+
asyncio.new_event_loop().run_until_complete(
|
| 22 |
+
send_to_client(
|
| 23 |
+
json.dumps({
|
| 24 |
+
'type': 'realtime',
|
| 25 |
+
'text': text
|
| 26 |
+
})
|
| 27 |
+
)
|
| 28 |
+
)
|
| 29 |
+
print(f"\r{text}", flush=True, end='')
|
| 30 |
+
|
| 31 |
+
recorder_config = {
|
| 32 |
+
'spinner': False,
|
| 33 |
+
'use_microphone': False,
|
| 34 |
+
'model': 'large-v2',
|
| 35 |
+
'language': 'en',
|
| 36 |
+
'silero_sensitivity': 0.4,
|
| 37 |
+
'webrtc_sensitivity': 2,
|
| 38 |
+
'post_speech_silence_duration': 0.7,
|
| 39 |
+
'min_length_of_recording': 0,
|
| 40 |
+
'min_gap_between_recordings': 0,
|
| 41 |
+
'enable_realtime_transcription': True,
|
| 42 |
+
'realtime_processing_pause': 0,
|
| 43 |
+
'realtime_model_type': 'tiny.en',
|
| 44 |
+
'on_realtime_transcription_stabilized': text_detected,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
def recorder_thread():
|
| 48 |
+
global recorder
|
| 49 |
+
print("Initializing RealtimeSTT...")
|
| 50 |
+
recorder = AudioToTextRecorder(**recorder_config)
|
| 51 |
+
print("RealtimeSTT initialized")
|
| 52 |
+
recorder_ready.set()
|
| 53 |
+
while True:
|
| 54 |
+
full_sentence = recorder.text()
|
| 55 |
+
asyncio.new_event_loop().run_until_complete(
|
| 56 |
+
send_to_client(
|
| 57 |
+
json.dumps({
|
| 58 |
+
'type': 'fullSentence',
|
| 59 |
+
'text': full_sentence
|
| 60 |
+
})
|
| 61 |
+
)
|
| 62 |
+
)
|
| 63 |
+
print(f"\rSentence: {full_sentence}")
|
| 64 |
+
|
| 65 |
+
def decode_and_resample(
|
| 66 |
+
audio_data,
|
| 67 |
+
original_sample_rate,
|
| 68 |
+
target_sample_rate):
|
| 69 |
+
|
| 70 |
+
# Decode 16-bit PCM data to numpy array
|
| 71 |
+
audio_np = np.frombuffer(audio_data, dtype=np.int16)
|
| 72 |
+
|
| 73 |
+
# Calculate the number of samples after resampling
|
| 74 |
+
num_original_samples = len(audio_np)
|
| 75 |
+
num_target_samples = int(num_original_samples * target_sample_rate /
|
| 76 |
+
original_sample_rate)
|
| 77 |
+
|
| 78 |
+
# Resample the audio
|
| 79 |
+
resampled_audio = resample(audio_np, num_target_samples)
|
| 80 |
+
|
| 81 |
+
return resampled_audio.astype(np.int16).tobytes()
|
| 82 |
+
|
| 83 |
+
async def echo(websocket, path):
|
| 84 |
+
print("Client connected")
|
| 85 |
+
global client_websocket
|
| 86 |
+
client_websocket = websocket
|
| 87 |
+
async for message in websocket:
|
| 88 |
+
|
| 89 |
+
if not recorder_ready.is_set():
|
| 90 |
+
print("Recorder not ready")
|
| 91 |
+
continue
|
| 92 |
+
|
| 93 |
+
metadata_length = int.from_bytes(message[:4], byteorder='little')
|
| 94 |
+
metadata_json = message[4:4+metadata_length].decode('utf-8')
|
| 95 |
+
metadata = json.loads(metadata_json)
|
| 96 |
+
sample_rate = metadata['sampleRate']
|
| 97 |
+
chunk = message[4+metadata_length:]
|
| 98 |
+
resampled_chunk = decode_and_resample(chunk, sample_rate, 16000)
|
| 99 |
+
recorder.feed_audio(resampled_chunk)
|
| 100 |
+
|
| 101 |
+
# start_server = websockets.serve(echo, "0.0.0.0", 9001)
|
| 102 |
+
start_server = websockets.serve(echo, "localhost", 8001)
|
| 103 |
+
|
| 104 |
+
recorder_thread = threading.Thread(target=recorder_thread)
|
| 105 |
+
recorder_thread.start()
|
| 106 |
+
recorder_ready.wait()
|
| 107 |
+
|
| 108 |
+
print("Server started. Press Ctrl+C to stop the server.")
|
| 109 |
+
asyncio.get_event_loop().run_until_complete(start_server)
|
| 110 |
+
asyncio.get_event_loop().run_forever()
|