add multilingual, task option to python client & server
This commit is contained in:
@@ -18,6 +18,9 @@ CHANNELS = 1
|
|||||||
RATE = 16000
|
RATE = 16000
|
||||||
RECORD_SECONDS = 60000
|
RECORD_SECONDS = 60000
|
||||||
START_RECORDING = False
|
START_RECORDING = False
|
||||||
|
multilingual = False
|
||||||
|
language = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def on_message(ws, message):
|
def on_message(ws, message):
|
||||||
@@ -53,7 +56,16 @@ def on_close(ws, close_status_code, close_msg):
|
|||||||
print("### websocket connection closed ###")
|
print("### websocket connection closed ###")
|
||||||
|
|
||||||
def on_open(ws):
|
def on_open(ws):
|
||||||
|
global multilingual, language, task
|
||||||
|
print(multilingual, language, task)
|
||||||
|
|
||||||
print("Opened connection")
|
print("Opened connection")
|
||||||
|
ws.send(json.dumps({
|
||||||
|
'multilingual': multilingual[0],
|
||||||
|
'language': language[0],
|
||||||
|
'task': task
|
||||||
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
@@ -226,13 +238,22 @@ if __name__=="__main__":
|
|||||||
parser.add_argument('--audio', type=str, help='audio file to transcribe')
|
parser.add_argument('--audio', type=str, help='audio file to transcribe')
|
||||||
parser.add_argument('--host', default=None, type=str, help='websocket server address to connect to')
|
parser.add_argument('--host', default=None, type=str, help='websocket server address to connect to')
|
||||||
parser.add_argument('--port', default=None, type=str, help='websocket server port to connect to')
|
parser.add_argument('--port', default=None, type=str, help='websocket server port to connect to')
|
||||||
|
parser.add_argument('--multilingual', action="store_true", help='use multilingual model')
|
||||||
|
parser.add_argument('--language', default=None, type=str, help='languages to use')
|
||||||
|
parser.add_argument(
|
||||||
|
'--task', default="transcribe", type=str, help='task transcribe/translate (translates from any to english)')
|
||||||
opt = parser.parse_args()
|
opt = parser.parse_args()
|
||||||
|
print(opt)
|
||||||
|
multilingual=opt.multilingual,
|
||||||
|
language = opt.language,
|
||||||
|
task = opt.task
|
||||||
c = Client(host=opt.host, port=opt.port)
|
c = Client(host=opt.host, port=opt.port)
|
||||||
|
|
||||||
# while loop to wait for server to be ready
|
# while loop to wait for server to be ready
|
||||||
print("Waiting for server ready ...")
|
print("Waiting for server ready ...")
|
||||||
while not START_RECORDING:
|
while not START_RECORDING:
|
||||||
pass
|
pass
|
||||||
|
print("Server Ready!")
|
||||||
if os.name=='nt':
|
if os.name=='nt':
|
||||||
os.system('cls')
|
os.system('cls')
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -26,14 +26,20 @@ def recv_audio(websocket):
|
|||||||
Receive audio chunks from client in an infinite loop.
|
Receive audio chunks from client in an infinite loop.
|
||||||
"""
|
"""
|
||||||
global clients
|
global clients
|
||||||
client = ServeClient(websocket)
|
options = websocket.recv()
|
||||||
|
options = json.loads(options)
|
||||||
|
client = ServeClient(
|
||||||
|
websocket,
|
||||||
|
multilingual=options["multilingual"],
|
||||||
|
language=options["language"],
|
||||||
|
task=options["task"]
|
||||||
|
)
|
||||||
|
|
||||||
clients[websocket] = client
|
clients[websocket] = client
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
frame_data = websocket.recv()
|
frame_data = websocket.recv()
|
||||||
if isinstance(frame_data, str):
|
|
||||||
logging.info(frame_data)
|
|
||||||
continue
|
|
||||||
frame_np = np.frombuffer(frame_data, np.float32)
|
frame_np = np.frombuffer(frame_data, np.float32)
|
||||||
clients[websocket].add_frames(frame_np)
|
clients[websocket].add_frames(frame_np)
|
||||||
|
|
||||||
@@ -46,15 +52,16 @@ def recv_audio(websocket):
|
|||||||
|
|
||||||
class ServeClient:
|
class ServeClient:
|
||||||
RATE = 16000
|
RATE = 16000
|
||||||
def __init__(self, websocket, topic=None, device=None):
|
def __init__(self, websocket, task="transcribe", device=None, multilingual=False, language=None):
|
||||||
self.payload_size = struct.calcsize("Q")
|
|
||||||
self.data = b""
|
self.data = b""
|
||||||
self.frames = b""
|
self.frames = b""
|
||||||
|
self.language = language
|
||||||
|
self.task = task
|
||||||
self.transcriber = WhisperModel(
|
self.transcriber = WhisperModel(
|
||||||
"small.en",
|
"small" if multilingual else "small.en",
|
||||||
device="cuda",
|
device=device if device else "cuda",
|
||||||
compute_type="float16",
|
compute_type="float16",
|
||||||
local_files_only=False
|
local_files_only=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# voice activity detection model
|
# voice activity detection model
|
||||||
@@ -83,9 +90,6 @@ class ServeClient:
|
|||||||
self.wrapper = textwrap.TextWrapper(width=50)
|
self.wrapper = textwrap.TextWrapper(width=50)
|
||||||
self.pick_previous_segments = 2
|
self.pick_previous_segments = 2
|
||||||
|
|
||||||
# setup mqtt
|
|
||||||
self.topic = topic
|
|
||||||
|
|
||||||
# threading
|
# threading
|
||||||
self.websocket = websocket
|
self.websocket = websocket
|
||||||
self.trans_thread = threading.Thread(target=self.speech_to_text)
|
self.trans_thread = threading.Thread(target=self.speech_to_text)
|
||||||
@@ -164,7 +168,13 @@ class ServeClient:
|
|||||||
initial_prompt = None
|
initial_prompt = None
|
||||||
|
|
||||||
# whisper transcribe with prompt
|
# whisper transcribe with prompt
|
||||||
result = self.transcriber.transcribe(input_sample, initial_prompt=initial_prompt)
|
result = self.transcriber.transcribe(
|
||||||
|
input_sample,
|
||||||
|
initial_prompt=initial_prompt,
|
||||||
|
language=self.language,
|
||||||
|
task=self.task
|
||||||
|
)
|
||||||
|
|
||||||
if len(result):
|
if len(result):
|
||||||
self.t_start = None
|
self.t_start = None
|
||||||
last_segment = self.update_segments(result, duration)
|
last_segment = self.update_segments(result, duration)
|
||||||
|
|||||||
Reference in New Issue
Block a user