auto detect language ppython client

This commit is contained in:
makaveli10
2023-07-28 15:34:50 +08:00
parent ecfc2b2c88
commit eddd615a9d
2 changed files with 26 additions and 6 deletions
+8 -6
View File
@@ -24,11 +24,18 @@ language = None
def on_message(ws, message): def on_message(ws, message):
global START_RECORDING global START_RECORDING, language
message = json.loads(message) message = json.loads(message)
if message == "SERVER_READY": if message == "SERVER_READY":
START_RECORDING = True START_RECORDING = True
return return
if isinstance(message, dict):
language = message.get("language")
lang_prob = message.get("language_prob")
print(f"Server detected language {language} with probability {lang_prob}")
return
text = [] text = []
if len(message): if len(message):
for seg in message: for seg in message:
@@ -67,7 +74,6 @@ def on_open(ws):
})) }))
class Client: class Client:
def __init__(self, host=None, port=None): def __init__(self, host=None, port=None):
self.timestamp_offset = 0.0 self.timestamp_offset = 0.0
@@ -254,10 +260,6 @@ if __name__=="__main__":
while not START_RECORDING: while not START_RECORDING:
pass pass
print("Server Ready!") print("Server Ready!")
if os.name=='nt':
os.system('cls')
else:
os.system('clear')
if opt.audio is not None: if opt.audio is not None:
resampled_file = resample(opt.audio) resampled_file = resample(opt.audio)
+18
View File
@@ -140,6 +140,24 @@ class ServeClient:
""" """
Process audio stream in an infinite loop. Process audio stream in an infinite loop.
""" """
# detect language
if self.language is None:
# wait for 30s of audio
while self.frames_np is None or self.frames_np.shape[0] < 30*self.RATE:
time.sleep(1)
input_bytes = self.frames_np[-30*self.RATE:].copy()
self.frames_np = None
duration = input_bytes.shape[0] / self.RATE
self.language, lang_prob = self.transcriber.transcribe(
input_bytes,
initial_prompt=None,
language=self.language,
task=self.task
)
logging.info(f"Detected language {self.language} with probability {lang_prob}")
self.websocket.send(json.dumps({"language": self.language, "language_prob": lang_prob}))
while True: while True:
if self.exit: if self.exit:
logging.info("Exiting speech to text thread") logging.info("Exiting speech to text thread")