Skip to content

Commit 6f38455

Browse files
committed
handle the string case in audio directly
1 parent a5ad0a7 commit 6f38455

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/openai/cli/_api/audio.py

+33-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

3+
import sys
34
from typing import TYPE_CHECKING, Any, Optional, cast
45
from argparse import ArgumentParser
56

67
from .._utils import get_client, print_model
78
from ..._types import NOT_GIVEN
89
from .._models import BaseModel
910
from .._progress import BufferReader
11+
from ...types.audio import Transcription
1012

1113
if TYPE_CHECKING:
1214
from argparse import _SubParsersAction
@@ -65,30 +67,42 @@ def transcribe(args: CLITranscribeArgs) -> None:
6567
with open(args.file, "rb") as file_reader:
6668
buffer_reader = BufferReader(file_reader.read(), desc="Upload progress")
6769

68-
model = get_client().audio.transcriptions.create(
69-
file=(args.file, buffer_reader),
70-
model=args.model,
71-
language=args.language or NOT_GIVEN,
72-
temperature=args.temperature or NOT_GIVEN,
73-
prompt=args.prompt or NOT_GIVEN,
74-
# casts required because the API is typed for enums
75-
# but we don't want to validate that here for forwards-compat
76-
response_format=cast(Any, args.response_format),
70+
model = cast(
71+
"Transcription | str",
72+
get_client().audio.transcriptions.create(
73+
file=(args.file, buffer_reader),
74+
model=args.model,
75+
language=args.language or NOT_GIVEN,
76+
temperature=args.temperature or NOT_GIVEN,
77+
prompt=args.prompt or NOT_GIVEN,
78+
# casts required because the API is typed for enums
79+
# but we don't want to validate that here for forwards-compat
80+
response_format=cast(Any, args.response_format),
81+
),
7782
)
78-
print_model(model)
83+
if isinstance(model, str):
84+
sys.stdout.write(model + "\n")
85+
else:
86+
print_model(model)
7987

8088
@staticmethod
8189
def translate(args: CLITranslationArgs) -> None:
8290
with open(args.file, "rb") as file_reader:
8391
buffer_reader = BufferReader(file_reader.read(), desc="Upload progress")
8492

85-
model = get_client().audio.translations.create(
86-
file=(args.file, buffer_reader),
87-
model=args.model,
88-
temperature=args.temperature or NOT_GIVEN,
89-
prompt=args.prompt or NOT_GIVEN,
90-
# casts required because the API is typed for enums
91-
# but we don't want to validate that here for forwards-compat
92-
response_format=cast(Any, args.response_format),
93+
model = cast(
94+
"Transcription | str",
95+
get_client().audio.translations.create(
96+
file=(args.file, buffer_reader),
97+
model=args.model,
98+
temperature=args.temperature or NOT_GIVEN,
99+
prompt=args.prompt or NOT_GIVEN,
100+
# casts required because the API is typed for enums
101+
# but we don't want to validate that here for forwards-compat
102+
response_format=cast(Any, args.response_format),
103+
),
93104
)
94-
print_model(model)
105+
if isinstance(model, str):
106+
sys.stdout.write(model + "\n")
107+
else:
108+
print_model(model)

src/openai/cli/_utils.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ def organization_info() -> str:
3333

3434

3535
def print_model(model: BaseModel) -> None:
36-
if isinstance(model, BaseModel):
37-
sys.stdout.write(model_json(model, indent=2) + "\n")
38-
elif isinstance(model, str):
39-
sys.stdout.write(model)
36+
sys.stdout.write(model_json(model, indent=2) + "\n")
4037

4138

4239
def can_use_http2() -> bool:

0 commit comments

Comments
 (0)