Skip to content

Commit bea82db

Browse files
committed
Add support for HuggingFace transformers
1 parent 85bd1e7 commit bea82db

File tree

9 files changed

+306
-83
lines changed

9 files changed

+306
-83
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.5.0
4+
5+
- Add support for HuggingFace transformers Whisper models (--use-transformers)
6+
37
## 2.4.0
48

59
- Add "auto" for model and beam size (0) to select values based on CPU

MANIFEST.in

Lines changed: 0 additions & 1 deletion
This file was deleted.

pyproject.toml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[project]
2+
name = "wyoming-faster-whisper"
3+
version = "2.5.0"
4+
description = "Wyoming Server for Faster Whisper"
5+
readme = "README.md"
6+
requires-python = ">=3.8.1,<3.13"
7+
license = {text = "MIT"}
8+
authors = [
9+
{name = "Michael Hansen", email = "[email protected]"}
10+
]
11+
keywords = ["rhasspy", "wyoming", "whisper", "stt"]
12+
classifiers = [
13+
"Development Status :: 3 - Alpha",
14+
"Intended Audience :: Developers",
15+
"Topic :: Text Processing :: Linguistic",
16+
"License :: OSI Approved :: MIT License",
17+
"Programming Language :: Python :: 3.7",
18+
"Programming Language :: Python :: 3.8",
19+
"Programming Language :: Python :: 3.9",
20+
"Programming Language :: Python :: 3.10",
21+
"Programming Language :: Python :: 3.11",
22+
"Programming Language :: Python :: 3.12",
23+
]
24+
dependencies = [
25+
"wyoming>=1.5.3",
26+
"faster-whisper>=1.1.0,<2"
27+
]
28+
29+
[project.urls]
30+
Homepage = "http://github.com/rhasspy/wyoming-faster-whisper"
31+
32+
[tool.setuptools]
33+
platforms = ["any"]
34+
zip-safe = true
35+
include-package-data = true
36+
37+
[tool.setuptools.packages.find]
38+
include = ["wyoming_faster_whisper"]
39+
exclude = ["tests"]
40+
41+
[project.scripts]
42+
wyoming-faster-whisper = "wyoming_faster_whisper.__main__:run"
43+
44+
[build-system]
45+
requires = ["setuptools>=42", "wheel"]
46+
build-backend = "setuptools.build_meta"
47+
48+
[tool.black]
49+
line-length = 88
50+
51+
[tool.isort]
52+
profile = "black"
53+
54+
[tool.pytest.ini_options]
55+
asyncio_mode = "auto"
56+
57+
[tool.mypy]
58+
check_untyped_defs = true
59+
disallow_untyped_defs = true
60+
61+
[project.optional-dependencies]
62+
dev = [
63+
"black==22.12.0",
64+
"flake8==6.0.0",
65+
"isort==5.11.3",
66+
"mypy==0.991",
67+
"pylint==2.15.9",
68+
"pytest==7.4.4",
69+
"pytest-asyncio==0.23.3",
70+
]
71+
transformers = [
72+
"transformers[torch]==4.52.4",
73+
]

requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

requirements_dev.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

script/setup

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ _VENV_DIR = _PROGRAM_DIR / ".venv"
1010

1111
parser = argparse.ArgumentParser()
1212
parser.add_argument("--dev", action="store_true", help="Install dev requirements")
13+
parser.add_argument(
14+
"--transformers", action="store_true", help="Install transformers requirements"
15+
)
1316
args = parser.parse_args()
1417

1518
# Create virtual environment
@@ -22,11 +25,16 @@ pip = [context.env_exe, "-m", "pip"]
2225
subprocess.check_call(pip + ["install", "--upgrade", "pip"])
2326
subprocess.check_call(pip + ["install", "--upgrade", "setuptools", "wheel"])
2427

25-
# Install requirements
26-
subprocess.check_call(pip + ["install", "-r", str(_PROGRAM_DIR / "requirements.txt")])
27-
28+
extras = []
2829
if args.dev:
29-
# Install dev requirements
30-
subprocess.check_call(
31-
pip + ["install", "-r", str(_PROGRAM_DIR / "requirements_dev.txt")]
32-
)
30+
extras.append("dev")
31+
32+
if args.transformers:
33+
extras.append("transformers")
34+
35+
extras_str = ""
36+
if extras:
37+
extras_str = "[" + ",".join(extras) + "]"
38+
39+
# Install requirements
40+
subprocess.check_call(pip + ["install", "-e", f"{_PROGRAM_DIR}{extras_str}"])

setup.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

wyoming_faster_whisper/__main__.py

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import re
77
from functools import partial
8+
from typing import Any
89

910
import faster_whisper
1011
from wyoming.info import AsrModel, AsrProgram, Attribution, Info
@@ -59,6 +60,16 @@ async def main() -> None:
5960
"--initial-prompt",
6061
help="Optional text to provide as a prompt for the first window",
6162
)
63+
parser.add_argument(
64+
"--use-transformers",
65+
action="store_true",
66+
help="Use HuggingFace transformers library (requires transformers extras)",
67+
)
68+
parser.add_argument(
69+
"--local-files-only",
70+
action="store_true",
71+
help="Don't check HuggingFace hub for updates every time",
72+
)
6273
#
6374
parser.add_argument("--debug", action="store_true", help="Log DEBUG messages")
6475
parser.add_argument(
@@ -135,26 +146,61 @@ async def main() -> None:
135146

136147
# Load model
137148
_LOGGER.debug("Loading %s", args.model)
138-
whisper_model = faster_whisper.WhisperModel(
139-
args.model,
140-
download_root=args.download_dir,
141-
device=args.device,
142-
compute_type=args.compute_type,
143-
)
149+
whisper_model: Any = None
150+
151+
if args.use_transformers:
152+
# Use HuggingFace transformers
153+
from .transformers_whisper import TransformersWhisperModel
154+
155+
whisper_model = TransformersWhisperModel(
156+
args.model, args.download_dir, args.local_files_only
157+
)
158+
else:
159+
# Use faster-whisper
160+
whisper_model = faster_whisper.WhisperModel(
161+
args.model,
162+
download_root=args.download_dir,
163+
device=args.device,
164+
compute_type=args.compute_type,
165+
)
144166

145167
server = AsyncServer.from_uri(args.uri)
146168
_LOGGER.info("Ready")
147169
model_lock = asyncio.Lock()
148-
await server.run(
149-
partial(
150-
FasterWhisperEventHandler,
151-
wyoming_info,
152-
args,
153-
whisper_model,
154-
model_lock,
155-
initial_prompt=args.initial_prompt,
170+
171+
if args.use_transformers:
172+
# Use HuggingFace transformers
173+
from .transformers_whisper import (
174+
TransformersWhisperEventHandler,
175+
TransformersWhisperModel,
176+
)
177+
178+
assert isinstance(whisper_model, TransformersWhisperModel)
179+
180+
# TODO: initial prompt
181+
await server.run(
182+
partial(
183+
TransformersWhisperEventHandler,
184+
wyoming_info,
185+
args.language,
186+
args.beam_size,
187+
whisper_model,
188+
model_lock,
189+
)
190+
)
191+
else:
192+
# Use faster-whisper
193+
assert isinstance(whisper_model, faster_whisper.WhisperModel)
194+
await server.run(
195+
partial(
196+
FasterWhisperEventHandler,
197+
wyoming_info,
198+
args,
199+
whisper_model,
200+
model_lock,
201+
initial_prompt=args.initial_prompt,
202+
)
156203
)
157-
)
158204

159205

160206
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)