Skip to content

Commit

Permalink
refactor: Rename 'translations' to 'transcriptions'
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Jan 8, 2025
1 parent b6d5261 commit ceb1885
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .audio.rooms import CreateRoomResp
from .audio.speech import AudioFormat
from .audio.translations import CreateTranslationResp
from .audio.transcriptions import CreateTranscriptionsResp
from .audio.voices import Voice
from .auth import (
AsyncDeviceOAuthApp,
Expand Down Expand Up @@ -110,8 +110,8 @@
# audio.voices
"Voice",
"AudioFormat",
# audio.translations
"CreateTranslationResp",
# audio.transcriptions
"CreateTranscriptionsResp",
# auth
"AsyncDeviceOAuthApp",
"AsyncJWTOAuthApp",
Expand Down
28 changes: 15 additions & 13 deletions cozepy/audio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if TYPE_CHECKING:
from .rooms import AsyncRoomsClient, RoomsClient
from .speech import AsyncSpeechClient, SpeechClient
from .translations import AsyncTranslationsClient, TranslationsClient
from .transcriptions import AsyncTranscriptionsClient, TranscriptionsClient
from .voices import AsyncVoicesClient, VoicesClient


Expand All @@ -20,7 +20,7 @@ def __init__(self, base_url: str, auth: Auth, requester: Requester):
self._rooms: Optional[RoomsClient] = None
self._voices: Optional[VoicesClient] = None
self._speech: Optional[SpeechClient] = None
self._translations: Optional[TranslationsClient] = None
self._transcriptions: Optional[TranscriptionsClient] = None

@property
def rooms(self) -> "RoomsClient":
Expand All @@ -39,12 +39,14 @@ def speech(self) -> "SpeechClient":
return self._speech

@property
def translations(self) -> "TranslationsClient":
if self._translations is None:
from .translations import TranslationsClient
def transcriptions(self) -> "TranscriptionsClient":
if self._transcriptions is None:
from .transcriptions import TranscriptionsClient

self._translations = TranslationsClient(base_url=self._base_url, auth=self._auth, requester=self._requester)
return self._translations
self._transcriptions = TranscriptionsClient(
base_url=self._base_url, auth=self._auth, requester=self._requester
)
return self._transcriptions

@property
def voices(self) -> "VoicesClient":
Expand All @@ -64,7 +66,7 @@ def __init__(self, base_url: str, auth: Auth, requester: Requester):
self._rooms: Optional[AsyncRoomsClient] = None
self._voices: Optional[AsyncVoicesClient] = None
self._speech: Optional[AsyncSpeechClient] = None
self._translations: Optional[AsyncTranslationsClient] = None
self._transcriptions: Optional[AsyncTranscriptionsClient] = None

@property
def rooms(self) -> "AsyncRoomsClient":
Expand All @@ -91,11 +93,11 @@ def voices(self) -> "AsyncVoicesClient":
return self._voices

@property
def translations(self) -> "AsyncTranslationsClient":
if self._translations is None:
from .translations import AsyncTranslationsClient
def transcriptions(self) -> "AsyncTranscriptionsClient":
if self._transcriptions is None:
from .transcriptions import AsyncTranscriptionsClient

self._translations = AsyncTranslationsClient(
self._transcriptions = AsyncTranscriptionsClient(
base_url=self._base_url, auth=self._auth, requester=self._requester
)
return self._translations
return self._transcriptions
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from cozepy.util import remove_url_trailing_slash


class CreateTranslationResp(CozeModel):
class CreateTranscriptionsResp(CozeModel):
# The text of translation
text: str


class TranslationsClient(object):
class TranscriptionsClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
self._base_url = remove_url_trailing_slash(base_url)
self._auth = auth
Expand All @@ -23,22 +23,22 @@ def create(
*,
file: FileTypes,
**kwargs,
) -> CreateTranslationResp:
) -> CreateTranscriptionsResp:
"""
create translation
create transcriptions
:param file: The file to be translated.
:return: create translation result
:return: create transcriptions result
"""
url = f"{self._base_url}/v1/audio/translations"
url = f"{self._base_url}/v1/audio/transcriptions"
headers: Optional[dict] = kwargs.get("headers")
files = {"file": _try_fix_file(file)}
return self._requester.request(
"post", url, stream=False, cast=CreateTranslationResp, headers=headers, files=files
"post", url, stream=False, cast=CreateTranscriptionsResp, headers=headers, files=files
)


class AsyncTranslationsClient(object):
class AsyncTranscriptionsClient(object):
"""
Room service async client.
"""
Expand All @@ -53,16 +53,16 @@ async def create(
*,
file: FileTypes,
**kwargs,
) -> CreateTranslationResp:
) -> CreateTranscriptionsResp:
"""
create translation
create transcriptions
:param file: The file to be translated.
:return: create translation result
:return: create transcriptions result
"""
url = f"{self._base_url}/v1/audio/translations"
url = f"{self._base_url}/v1/audio/transcriptions"
files = {"file": _try_fix_file(file)}
headers: Optional[dict] = kwargs.get("headers")
return await self._requester.arequest(
"post", url, stream=False, cast=CreateTranslationResp, headers=headers, files=files
"post", url, stream=False, cast=CreateTranscriptionsResp, headers=headers, files=files
)
20 changes: 10 additions & 10 deletions tests/test_audio_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tests.test_util import logid_key


def mock_create_translation(respx_mock):
def mock_create_transcriptions(respx_mock):
logid = random_hex(10)
raw_response = httpx.Response(
200,
Expand All @@ -19,32 +19,32 @@ def mock_create_translation(respx_mock):
},
)

respx_mock.post("/v1/audio/translations").mock(raw_response)
respx_mock.post("/v1/audio/transcriptions").mock(raw_response)

return logid


@pytest.mark.respx(base_url="https://api.coze.com")
class TestAudioTranslation:
def test_sync_translation_create(self, respx_mock):
class TestSyncAudioTranscriptions:
def test_sync_transcriptions_create(self, respx_mock):
coze = Coze(auth=TokenAuth(token="token"))

mock_logid = mock_create_translation(respx_mock)
mock_logid = mock_create_transcriptions(respx_mock)

res = coze.audio.translations.create(file=("filename", "content"))
res = coze.audio.transcriptions.create(file=("filename", "content"))
assert res
assert res.response.logid is not None
assert res.response.logid == mock_logid


@pytest.mark.respx(base_url="https://api.coze.com")
@pytest.mark.asyncio
class TestAsyncAudioTranslation:
async def test_async_translation_create(self, respx_mock):
class TestAsyncAudioTranscriptions:
async def test_async_transcriptions_create(self, respx_mock):
coze = AsyncCoze(auth=TokenAuth(token="token"))

mock_logid = mock_create_translation(respx_mock)
mock_logid = mock_create_transcriptions(respx_mock)

res = await coze.audio.translations.create(file=("filename", "content"))
res = await coze.audio.transcriptions.create(file=("filename", "content"))
assert res
assert res.response.logid == mock_logid

0 comments on commit ceb1885

Please sign in to comment.