|
| 1 | +# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import List, Optional |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | +from ..._base_client import make_request_options |
| 11 | +from ..._resource import AsyncAPIResource, SyncAPIResource |
| 12 | +from ..._types import NOT_GIVEN, Body, Headers, NotGiven, Query |
| 13 | +from ..._utils import async_with_sts_token, with_sts_token |
| 14 | +from ...types.audio_generation.audio_config_param import AudioConfigParam |
| 15 | +from ...types.audio_generation.audio_generation_response import AudioGenerationResponse |
| 16 | +from ...types.audio_generation.content_param import ContentParam |
| 17 | +from ...types.audio_generation.dubbing_config_param import DubbingConfigParam |
| 18 | +from ...types.audio_generation.include import Include |
| 19 | +from ...types.audio_generation.output_format import OutputFormat |
| 20 | + |
| 21 | +__all__ = ["AudioGeneration", "AsyncAudioGeneration"] |
| 22 | + |
| 23 | +_PATH = "/contents/generations" |
| 24 | + |
| 25 | + |
| 26 | +class AudioGeneration(SyncAPIResource): |
| 27 | + @with_sts_token |
| 28 | + def create( |
| 29 | + self, |
| 30 | + *, |
| 31 | + model: str, |
| 32 | + content: List[ContentParam], |
| 33 | + dubbing_config: Optional[DubbingConfigParam] | NotGiven = NOT_GIVEN, |
| 34 | + output_format: Optional[OutputFormat] | NotGiven = NOT_GIVEN, |
| 35 | + audio_config: Optional[AudioConfigParam] | NotGiven = NOT_GIVEN, |
| 36 | + include: Optional[List[Include]] | NotGiven = NOT_GIVEN, |
| 37 | + watermark: Optional[bool] | NotGiven = NOT_GIVEN, |
| 38 | + extra_headers: Headers | None = None, |
| 39 | + extra_query: Query | None = None, |
| 40 | + extra_body: Body | None = None, |
| 41 | + timeout: float | httpx.Timeout | None = None, |
| 42 | + ) -> AudioGenerationResponse: |
| 43 | + """Generate reference audio, separate audio, or dub a video. |
| 44 | +
|
| 45 | + This endpoint returns a complete JSON response, not a task or SSE stream. |
| 46 | + Omitted optional arguments are not sent; explicit None is sent as null. |
| 47 | + Scene-specific parameter validation is performed by the server. |
| 48 | + """ |
| 49 | + return self._post( |
| 50 | + _PATH, |
| 51 | + # RequestOptions removes NOT_GIVEN while preserving explicit nulls, |
| 52 | + # including nested nulls. maybe_transform would discard those nulls. |
| 53 | + body={ |
| 54 | + "model": model, |
| 55 | + "content": content, |
| 56 | + "dubbing_config": dubbing_config, |
| 57 | + "output_format": output_format, |
| 58 | + "audio_config": audio_config, |
| 59 | + "include": include, |
| 60 | + "watermark": watermark, |
| 61 | + }, |
| 62 | + options=make_request_options( |
| 63 | + extra_headers=extra_headers, |
| 64 | + extra_query=extra_query, |
| 65 | + extra_body=extra_body, |
| 66 | + timeout=timeout, |
| 67 | + ), |
| 68 | + cast_to=AudioGenerationResponse, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +class AsyncAudioGeneration(AsyncAPIResource): |
| 73 | + @async_with_sts_token |
| 74 | + async def create( |
| 75 | + self, |
| 76 | + *, |
| 77 | + model: str, |
| 78 | + content: List[ContentParam], |
| 79 | + dubbing_config: Optional[DubbingConfigParam] | NotGiven = NOT_GIVEN, |
| 80 | + output_format: Optional[OutputFormat] | NotGiven = NOT_GIVEN, |
| 81 | + audio_config: Optional[AudioConfigParam] | NotGiven = NOT_GIVEN, |
| 82 | + include: Optional[List[Include]] | NotGiven = NOT_GIVEN, |
| 83 | + watermark: Optional[bool] | NotGiven = NOT_GIVEN, |
| 84 | + extra_headers: Headers | None = None, |
| 85 | + extra_query: Query | None = None, |
| 86 | + extra_body: Body | None = None, |
| 87 | + timeout: float | httpx.Timeout | None = None, |
| 88 | + ) -> AudioGenerationResponse: |
| 89 | + """Asynchronously call the non-streaming audio generation endpoint. |
| 90 | +
|
| 91 | + Omitted optional arguments are not sent; explicit None is sent as null. |
| 92 | + Scene-specific parameter validation is performed by the server. |
| 93 | + """ |
| 94 | + return await self._post( |
| 95 | + _PATH, |
| 96 | + body={ |
| 97 | + "model": model, |
| 98 | + "content": content, |
| 99 | + "dubbing_config": dubbing_config, |
| 100 | + "output_format": output_format, |
| 101 | + "audio_config": audio_config, |
| 102 | + "include": include, |
| 103 | + "watermark": watermark, |
| 104 | + }, |
| 105 | + options=make_request_options( |
| 106 | + extra_headers=extra_headers, |
| 107 | + extra_query=extra_query, |
| 108 | + extra_body=extra_body, |
| 109 | + timeout=timeout, |
| 110 | + ), |
| 111 | + cast_to=AudioGenerationResponse, |
| 112 | + ) |
0 commit comments