|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import sys |
3 | 4 | from typing import TYPE_CHECKING, Any, Optional, cast
|
4 | 5 | from argparse import ArgumentParser
|
5 | 6 |
|
6 | 7 | from .._utils import get_client, print_model
|
7 | 8 | from ..._types import NOT_GIVEN
|
8 | 9 | from .._models import BaseModel
|
9 | 10 | from .._progress import BufferReader
|
| 11 | +from ...types.audio import Transcription |
10 | 12 |
|
11 | 13 | if TYPE_CHECKING:
|
12 | 14 | from argparse import _SubParsersAction
|
@@ -65,30 +67,42 @@ def transcribe(args: CLITranscribeArgs) -> None:
|
65 | 67 | with open(args.file, "rb") as file_reader:
|
66 | 68 | buffer_reader = BufferReader(file_reader.read(), desc="Upload progress")
|
67 | 69 |
|
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 | + ), |
77 | 82 | )
|
78 |
| - print_model(model) |
| 83 | + if isinstance(model, str): |
| 84 | + sys.stdout.write(model + "\n") |
| 85 | + else: |
| 86 | + print_model(model) |
79 | 87 |
|
80 | 88 | @staticmethod
|
81 | 89 | def translate(args: CLITranslationArgs) -> None:
|
82 | 90 | with open(args.file, "rb") as file_reader:
|
83 | 91 | buffer_reader = BufferReader(file_reader.read(), desc="Upload progress")
|
84 | 92 |
|
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 | + ), |
93 | 104 | )
|
94 |
| - print_model(model) |
| 105 | + if isinstance(model, str): |
| 106 | + sys.stdout.write(model + "\n") |
| 107 | + else: |
| 108 | + print_model(model) |
0 commit comments