Add options to --mode

This commit is contained in:
Yuta Hayashibe 2022-10-02 21:31:05 +09:00
parent 451db34c2d
commit e06067dbb9

View file

@ -4,6 +4,7 @@ import argparse
import asyncio import asyncio
import queue import queue
import sys import sys
from enum import Enum
from logging import DEBUG, INFO, basicConfig, getLogger from logging import DEBUG, INFO, basicConfig, getLogger
from typing import Optional, Union from typing import Optional, Union
@ -22,6 +23,15 @@ from whispering.websocket_client import run_websocket_client
logger = getLogger(__name__) logger = getLogger(__name__)
class Mode(Enum):
client = "client"
server = "server"
mic = "mic"
def __str__(self):
return self.value
def transcribe_from_mic( def transcribe_from_mic(
*, *,
wsp: WhisperStreamingTranscriber, wsp: WhisperStreamingTranscriber,
@ -151,7 +161,7 @@ def get_opts() -> argparse.Namespace:
) )
group_ctx.add_argument( group_ctx.add_argument(
"--mode", "--mode",
choices=["client"], choices=[v.value for v in Mode],
) )
group_misc = parser.add_argument_group("Other options") group_misc = parser.add_argument_group("Other options")
@ -225,31 +235,33 @@ def main() -> None:
if opts.show_devices: if opts.show_devices:
return show_devices() return show_devices()
if opts.host is not None and opts.port is not None: if opts.host is not None and opts.port is not None and opts.mode != Mode.client:
if opts.mode == "client": opts.mode = Mode.server
assert opts.language is None
assert opts.model is None if opts.mode == Mode.client:
try: assert opts.language is None
asyncio.run( assert opts.model is None
run_websocket_client( try:
opts=opts,
)
)
except KeyboardInterrupt:
pass
else:
assert opts.language is not None
assert opts.model is not None
wsp = get_wshiper(opts=opts)
ctx: Context = get_context(opts=opts)
asyncio.run( asyncio.run(
serve_with_websocket( run_websocket_client(
wsp=wsp, opts=opts,
host=opts.host,
port=opts.port,
ctx=ctx,
) )
) )
except KeyboardInterrupt:
pass
elif opts.mode == Mode.server:
assert opts.language is not None
assert opts.model is not None
wsp = get_wshiper(opts=opts)
ctx: Context = get_context(opts=opts)
asyncio.run(
serve_with_websocket(
wsp=wsp,
host=opts.host,
port=opts.port,
ctx=ctx,
)
)
else: else:
assert opts.language is not None assert opts.language is not None
assert opts.model is not None assert opts.model is not None