Spaces:
Runtime error
Runtime error
Isaacyn commited on
Commit ·
cd6e083
1
Parent(s): 3e094b4
async queue pipeline
Browse files
asr.py
CHANGED
|
@@ -58,18 +58,18 @@ class ASRPipeline:
|
|
| 58 |
|
| 59 |
self.last_text = ""
|
| 60 |
self.is_active = True
|
| 61 |
-
self.is_processing = False
|
| 62 |
self.key_index = 0
|
|
|
|
| 63 |
|
| 64 |
print("ASR Pipeline siap ✓")
|
| 65 |
|
| 66 |
def get_groq_client(self):
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
return client
|
| 70 |
|
| 71 |
def stop(self):
|
| 72 |
-
self.is_processing = False
|
| 73 |
self.is_active = True
|
| 74 |
print("Pipeline stopped ✓")
|
| 75 |
|
|
@@ -141,22 +141,16 @@ class ASRPipeline:
|
|
| 141 |
if not self.is_active:
|
| 142 |
return None
|
| 143 |
|
| 144 |
-
if self.is_processing:
|
| 145 |
-
return None
|
| 146 |
-
|
| 147 |
vad_conf = self.get_vad_confidence(audio_chunk)
|
| 148 |
if vad_conf <= VAD_THRESHOLD:
|
| 149 |
return None
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
return text.strip()
|
| 161 |
-
finally:
|
| 162 |
-
self.is_processing = False
|
|
|
|
| 58 |
|
| 59 |
self.last_text = ""
|
| 60 |
self.is_active = True
|
|
|
|
| 61 |
self.key_index = 0
|
| 62 |
+
self.key_lock = threading.Lock()
|
| 63 |
|
| 64 |
print("ASR Pipeline siap ✓")
|
| 65 |
|
| 66 |
def get_groq_client(self):
|
| 67 |
+
with self.key_lock:
|
| 68 |
+
client = Groq(api_key=GROQ_KEYS[self.key_index])
|
| 69 |
+
self.key_index = (self.key_index + 1) % len(GROQ_KEYS)
|
| 70 |
return client
|
| 71 |
|
| 72 |
def stop(self):
|
|
|
|
| 73 |
self.is_active = True
|
| 74 |
print("Pipeline stopped ✓")
|
| 75 |
|
|
|
|
| 141 |
if not self.is_active:
|
| 142 |
return None
|
| 143 |
|
|
|
|
|
|
|
|
|
|
| 144 |
vad_conf = self.get_vad_confidence(audio_chunk)
|
| 145 |
if vad_conf <= VAD_THRESHOLD:
|
| 146 |
return None
|
| 147 |
|
| 148 |
+
text = self.groq_transcribe(audio_chunk)
|
| 149 |
+
if not text:
|
| 150 |
+
return None
|
| 151 |
+
if not self.is_valid_text(text):
|
| 152 |
+
print(f"Filtered: {text}")
|
| 153 |
+
return None
|
| 154 |
+
|
| 155 |
+
self.last_text = text
|
| 156 |
+
return text.strip()
|
|
|
|
|
|
|
|
|
main.py
CHANGED
|
@@ -3,6 +3,7 @@ import numpy as np
|
|
| 3 |
import logging
|
| 4 |
import warnings
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
logging.getLogger("nemo_logger").setLevel(logging.ERROR)
|
| 8 |
logging.getLogger("nemo").setLevel(logging.ERROR)
|
|
@@ -22,7 +23,7 @@ app.add_middleware(
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
-
MODEL_DIR = "/
|
| 26 |
pipeline = ASRPipeline(MODEL_DIR)
|
| 27 |
|
| 28 |
@app.get("/")
|
|
@@ -36,26 +37,45 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 36 |
print("Client connected")
|
| 37 |
print("=" * 50)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
while True:
|
| 41 |
data = await websocket.receive_bytes()
|
| 42 |
audio_chunk = np.frombuffer(data, dtype=np.float32)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
await websocket.send_text(result)
|
| 50 |
-
else:
|
| 51 |
-
await websocket.send_text("")
|
| 52 |
|
| 53 |
except WebSocketDisconnect:
|
| 54 |
pipeline.stop()
|
|
|
|
|
|
|
| 55 |
print("=" * 50)
|
| 56 |
print("Client disconnected")
|
| 57 |
print("=" * 50)
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error: {e}")
|
| 60 |
pipeline.stop()
|
|
|
|
| 61 |
await websocket.close()
|
|
|
|
| 3 |
import logging
|
| 4 |
import warnings
|
| 5 |
import os
|
| 6 |
+
import threading
|
| 7 |
|
| 8 |
logging.getLogger("nemo_logger").setLevel(logging.ERROR)
|
| 9 |
logging.getLogger("nemo").setLevel(logging.ERROR)
|
|
|
|
| 23 |
allow_headers=["*"],
|
| 24 |
)
|
| 25 |
|
| 26 |
+
MODEL_DIR = "C:/Users/YEREMIA/Documents/captionai_server/models"
|
| 27 |
pipeline = ASRPipeline(MODEL_DIR)
|
| 28 |
|
| 29 |
@app.get("/")
|
|
|
|
| 37 |
print("Client connected")
|
| 38 |
print("=" * 50)
|
| 39 |
|
| 40 |
+
loop = asyncio.get_event_loop()
|
| 41 |
+
caption_queue = asyncio.Queue()
|
| 42 |
+
|
| 43 |
+
async def send_results():
|
| 44 |
+
while True:
|
| 45 |
+
text = await caption_queue.get()
|
| 46 |
+
if text is None:
|
| 47 |
+
break
|
| 48 |
+
try:
|
| 49 |
+
await websocket.send_text(text)
|
| 50 |
+
except:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
sender = asyncio.create_task(send_results())
|
| 54 |
+
|
| 55 |
try:
|
| 56 |
while True:
|
| 57 |
data = await websocket.receive_bytes()
|
| 58 |
audio_chunk = np.frombuffer(data, dtype=np.float32)
|
| 59 |
|
| 60 |
+
def process(chunk=audio_chunk):
|
| 61 |
+
result = pipeline.transcribe_chunk(chunk)
|
| 62 |
+
if result:
|
| 63 |
+
asyncio.run_coroutine_threadsafe(
|
| 64 |
+
caption_queue.put(result),
|
| 65 |
+
loop
|
| 66 |
+
)
|
| 67 |
|
| 68 |
+
threading.Thread(target=process, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
except WebSocketDisconnect:
|
| 71 |
pipeline.stop()
|
| 72 |
+
await caption_queue.put(None)
|
| 73 |
+
await sender
|
| 74 |
print("=" * 50)
|
| 75 |
print("Client disconnected")
|
| 76 |
print("=" * 50)
|
| 77 |
except Exception as e:
|
| 78 |
print(f"Error: {e}")
|
| 79 |
pipeline.stop()
|
| 80 |
+
await caption_queue.put(None)
|
| 81 |
await websocket.close()
|