Update run.py
Browse files
run.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, WebSocket,
|
| 2 |
import numpy as np
|
| 3 |
from scipy.signal import resample
|
| 4 |
import json
|
|
@@ -9,14 +9,18 @@ app = FastAPI()
|
|
| 9 |
|
| 10 |
recorder = None
|
| 11 |
recorder_ready = asyncio.Event()
|
| 12 |
-
|
| 13 |
|
| 14 |
-
async def
|
| 15 |
-
if
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def text_detected(text):
|
| 19 |
-
asyncio.
|
| 20 |
print(f"\r{text}", flush=True, end='')
|
| 21 |
|
| 22 |
recorder_config = {
|
|
@@ -43,7 +47,7 @@ async def recorder_task():
|
|
| 43 |
recorder_ready.set()
|
| 44 |
while True:
|
| 45 |
full_sentence = recorder.text()
|
| 46 |
-
|
| 47 |
print(f"\rSentence: {full_sentence}")
|
| 48 |
|
| 49 |
def decode_and_resample(audio_data, original_sample_rate, target_sample_rate):
|
|
@@ -55,9 +59,8 @@ def decode_and_resample(audio_data, original_sample_rate, target_sample_rate):
|
|
| 55 |
|
| 56 |
@app.websocket("/ws")
|
| 57 |
async def websocket_endpoint(websocket: WebSocket):
|
| 58 |
-
global client_websocket
|
| 59 |
await websocket.accept()
|
| 60 |
-
|
| 61 |
try:
|
| 62 |
while True:
|
| 63 |
message = await websocket.receive_bytes()
|
|
@@ -69,10 +72,15 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 69 |
metadata = json.loads(metadata_json)
|
| 70 |
sample_rate = metadata['sampleRate']
|
| 71 |
chunk = message[4+metadata_length:]
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
except WebSocketDisconnect:
|
| 75 |
print("Client disconnected")
|
|
|
|
|
|
|
| 76 |
|
| 77 |
@app.on_event("startup")
|
| 78 |
async def startup_event():
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 2 |
import numpy as np
|
| 3 |
from scipy.signal import resample
|
| 4 |
import json
|
|
|
|
| 9 |
|
| 10 |
recorder = None
|
| 11 |
recorder_ready = asyncio.Event()
|
| 12 |
+
clients = set()
|
| 13 |
|
| 14 |
+
async def send_to_clients(message):
|
| 15 |
+
if clients:
|
| 16 |
+
for ws in clients:
|
| 17 |
+
try:
|
| 18 |
+
await ws.send_text(message)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Failed to send message: {e}")
|
| 21 |
|
| 22 |
def text_detected(text):
|
| 23 |
+
asyncio.create_task(send_to_clients(json.dumps({'type': 'realtime', 'text': text})))
|
| 24 |
print(f"\r{text}", flush=True, end='')
|
| 25 |
|
| 26 |
recorder_config = {
|
|
|
|
| 47 |
recorder_ready.set()
|
| 48 |
while True:
|
| 49 |
full_sentence = recorder.text()
|
| 50 |
+
asyncio.create_task(send_to_clients(json.dumps({'type': 'fullSentence', 'text': full_sentence})))
|
| 51 |
print(f"\rSentence: {full_sentence}")
|
| 52 |
|
| 53 |
def decode_and_resample(audio_data, original_sample_rate, target_sample_rate):
|
|
|
|
| 59 |
|
| 60 |
@app.websocket("/ws")
|
| 61 |
async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
| 62 |
await websocket.accept()
|
| 63 |
+
clients.add(websocket)
|
| 64 |
try:
|
| 65 |
while True:
|
| 66 |
message = await websocket.receive_bytes()
|
|
|
|
| 72 |
metadata = json.loads(metadata_json)
|
| 73 |
sample_rate = metadata['sampleRate']
|
| 74 |
chunk = message[4+metadata_length:]
|
| 75 |
+
try:
|
| 76 |
+
resampled_chunk = decode_and_resample(chunk, sample_rate, 16000)
|
| 77 |
+
recorder.feed_audio(resampled_chunk)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error processing audio chunk: {e}")
|
| 80 |
except WebSocketDisconnect:
|
| 81 |
print("Client disconnected")
|
| 82 |
+
finally:
|
| 83 |
+
clients.remove(websocket)
|
| 84 |
|
| 85 |
@app.on_event("startup")
|
| 86 |
async def startup_event():
|