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