mirror of
https://github.com/openai/whisper.git
synced 2025-11-24 06:26:03 +00:00
Merge 8c27cca65bf11e814488d3ac98849947fd5fd034 into 279133e3107392276dc509148da1f41bfb532c7e
This commit is contained in:
commit
2e63314696
53
tests/test_utils.py
Normal file
53
tests/test_utils.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import pytest
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from whisper.utils import optional_float, optional_int, str2bool
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(("provided", "expected"), [
|
||||||
|
("TRUE", True),
|
||||||
|
("True", True),
|
||||||
|
("true", True),
|
||||||
|
("YES", True),
|
||||||
|
("Yes", True),
|
||||||
|
("yes", True),
|
||||||
|
("Y", True),
|
||||||
|
("y", True),
|
||||||
|
("1", True),
|
||||||
|
|
||||||
|
("FALSE", False),
|
||||||
|
("False", False),
|
||||||
|
("false", False),
|
||||||
|
("NO", False),
|
||||||
|
("No", False),
|
||||||
|
("no", False),
|
||||||
|
("N", False),
|
||||||
|
("n", False),
|
||||||
|
("0", False),
|
||||||
|
])
|
||||||
|
def test_str2bool(provided: str, expected: bool) -> None:
|
||||||
|
assert str2bool(provided) is expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_str2bool_faulty_argument() -> None:
|
||||||
|
with pytest.raises(ValueError, match="Expected one of"):
|
||||||
|
str2bool("boom")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(("provided", "expected"), [
|
||||||
|
("1", 1),
|
||||||
|
("None", None),
|
||||||
|
("none", None),
|
||||||
|
])
|
||||||
|
def test_optional_int(provided: str, expected: Optional[int]) -> None:
|
||||||
|
assert optional_int(provided) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(("provided", "expected"), [
|
||||||
|
("1.23", 1.23),
|
||||||
|
("1", 1),
|
||||||
|
("None", None),
|
||||||
|
("none", None),
|
||||||
|
])
|
||||||
|
def test_optional_float(provided: str, expected: Optional[float]) -> None:
|
||||||
|
assert optional_float(provided) == expected
|
||||||
@ -26,20 +26,21 @@ def exact_div(x, y):
|
|||||||
return x // y
|
return x // y
|
||||||
|
|
||||||
|
|
||||||
def str2bool(string):
|
def str2bool(string: str) -> bool:
|
||||||
str2val = {"True": True, "False": False}
|
if string.lower() in {'true', 'yes', 'y', '1'}:
|
||||||
if string in str2val:
|
return True
|
||||||
return str2val[string]
|
if string.lower() in {'false', 'no', 'n', '0'}:
|
||||||
else:
|
return False
|
||||||
raise ValueError(f"Expected one of {set(str2val.keys())}, got {string}")
|
|
||||||
|
raise ValueError(f"Expected one of true/yes/1 or false/no/0, but got {string}")
|
||||||
|
|
||||||
|
|
||||||
def optional_int(string):
|
def optional_int(string):
|
||||||
return None if string == "None" else int(string)
|
return None if string.lower() == "none" else int(string)
|
||||||
|
|
||||||
|
|
||||||
def optional_float(string):
|
def optional_float(string):
|
||||||
return None if string == "None" else float(string)
|
return None if string.lower() == "none" else float(string)
|
||||||
|
|
||||||
|
|
||||||
def compression_ratio(text) -> float:
|
def compression_ratio(text) -> float:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user