Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions dashscope/aigc/image_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def wait(
task: Union[str, ImageGenerationResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> DashScopeAPIResponse:
"""Wait for image(s) synthesis task to complete, and return the result.
Expand All @@ -186,11 +187,18 @@ def wait(
ImageGenerationResponse return by async_call().
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait.
Default is -1 (no timeout).

Returns:
DashScopeAPIResponse: The task result.
"""
response = super().wait(task, api_key, workspace=workspace)
response = super().wait(
task,
api_key,
workspace=workspace,
wait_timeout=wait_timeout,
)
return ImageGenerationResponse.from_api_response(response)

@classmethod
Expand Down Expand Up @@ -487,6 +495,7 @@ async def wait(
task: Union[str, ImageGenerationResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> DashScopeAPIResponse:
"""Wait for image(s) synthesis task to complete, and return the result.
Expand All @@ -496,11 +505,18 @@ async def wait(
ImageGenerationResponse return by async_call().
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait.
Default is -1 (no timeout).

Returns:
DashScopeAPIResponse: The task result.
"""
response = await super().wait(task, api_key, workspace=workspace)
response = await super().wait(
task,
api_key,
workspace=workspace,
wait_timeout=wait_timeout,
)
return ImageGenerationResponse.from_api_response(response)

@classmethod
Expand Down
10 changes: 9 additions & 1 deletion dashscope/aigc/image_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ async def wait(
task: Union[str, ImageSynthesisResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> ImageSynthesisResponse:
"""Wait for image(s) synthesis task to complete, and return the result.
Expand All @@ -748,11 +749,18 @@ async def wait(
ImageSynthesisResponse return by async_call().
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait.
Default is -1 (no timeout).

Returns:
ImageSynthesisResponse: The task result.
"""
response = await super().wait(task, api_key, workspace=workspace)
response = await super().wait(
task,
api_key,
workspace=workspace,
wait_timeout=wait_timeout,
)
return ImageSynthesisResponse.from_api_response(response)

@classmethod
Expand Down
10 changes: 9 additions & 1 deletion dashscope/aigc/video_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ async def wait(
task: Union[str, VideoSynthesisResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> VideoSynthesisResponse:
"""Wait for video synthesis task to complete, and return the result.
Expand All @@ -903,11 +904,18 @@ async def wait(
VideoSynthesisResponse return by async_call().
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait.
Default is -1 (no timeout).

Returns:
VideoSynthesisResponse: The task result.
"""
response = await super().wait(task, api_key, workspace=workspace)
response = await super().wait(
task,
api_key,
workspace=workspace,
wait_timeout=wait_timeout,
)
return VideoSynthesisResponse.from_api_response(response)

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions dashscope/api_entities/api_request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def get_batch_binary_data(self) -> bytes: # type: ignore[return]

def _only_parameters(self) -> str:
obj = {"model": self.model, "parameters": self.parameters, "input": {}}
parameters = self.parameters.copy()
if "raw_input" in parameters:
obj["input"] = parameters.pop("raw_input")
Comment thread
songguocola marked this conversation as resolved.
if self.task is not None:
obj["task"] = self.task
if self.task_group is not None:
Expand Down
7 changes: 7 additions & 0 deletions dashscope/audio/asr/transcription.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,20 @@ def wait(
task: Union[str, TranscriptionResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> TranscriptionResponse:
"""Poll task until the final results of transcription is obtained.

Args:
task (Union[str, TranscriptionResponse]): The task_id or
response including task_id returned from async_call().
api_key (str, optional): The api_key. Defaults to None.
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The timeout for waiting.
Defaults to -1.That means no timeout.
If set to a value > 0, the task does not complete
within this time, a timeout error response will be returned.

Returns:
TranscriptionResponse: The result of batch transcription.
Expand All @@ -183,6 +189,7 @@ def wait(
task,
api_key=api_key,
workspace=workspace,
wait_timeout=wait_timeout,
**kwargs,
)
return TranscriptionResponse.from_api_response(response)
Expand Down
4 changes: 4 additions & 0 deletions dashscope/audio/qwen_asr/qwen_transcription.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def wait(
task: Union[str, TranscriptionResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> TranscriptionResponse:
"""Poll task until the final results of transcription is obtained.
Expand All @@ -135,6 +136,8 @@ def wait(
task (Union[str, TranscriptionResponse]): The task_id or
response including task_id returned from async_call().
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait.
Default is -1 (no timeout).

Returns:
TranscriptionResponse: The result of batch transcription.
Expand All @@ -143,6 +146,7 @@ def wait(
task,
api_key=api_key,
workspace=workspace,
wait_timeout=wait_timeout,
**kwargs,
)
return TranscriptionResponse.from_api_response(response)
Expand Down
51 changes: 51 additions & 0 deletions dashscope/client/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ async def wait(
task: Union[str, DashScopeAPIResponse],
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> DashScopeAPIResponse:
"""Wait for async task completion and return task result.
Expand All @@ -199,6 +200,12 @@ async def wait(
task (Union[str, DashScopeAPIResponse]): The task_id, or
async_call response.
api_key (str, optional): The api_key. Defaults to None.
workspace (str, optional): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait
for the task to complete. Default is -1, which means no
timeout. When set to a value > 0, if the task does not
complete within this time, a timeout error response will
be returned.

Returns:
DashScopeAPIResponse: The async task information.
Expand All @@ -208,6 +215,7 @@ async def wait(
max_wait_seconds = 5
increment_steps = 3
step = 0
start_time = time.time()
while True:
step += 1
# we start by querying once every second, and double
Expand All @@ -217,6 +225,21 @@ async def wait(
# (server side return immediately when ready)
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
if 0 < wait_timeout <= (time.time() - start_time):
Comment thread
songguocola marked this conversation as resolved.
logger.warning(
"Wait task: %s timeout after %s seconds.",
task_id,
wait_timeout,
)
return DashScopeAPIResponse(
request_id=task_id,
status_code=HTTPStatus.REQUEST_TIMEOUT,
code="WaitTaskTimeout",
message=(
f"Wait task: {task_id} timeout after "
f"{wait_timeout} seconds."
),
)
rsp = await cls._get(
task_id,
api_key,
Expand Down Expand Up @@ -600,6 +623,10 @@ def call(
**kwargs,
) -> DashScopeAPIResponse:
"""Call service and get result."""
wait_timeout = -1
if "wait_timeout" in kwargs:
wait_timeout = kwargs.pop("wait_timeout")

task_response = cls.async_call( # type: ignore[misc]
*args,
api_key=api_key,
Expand All @@ -610,6 +637,7 @@ def call(
task_response,
api_key=api_key,
workspace=workspace,
wait_timeout=wait_timeout,
)
return response

Expand Down Expand Up @@ -767,6 +795,7 @@ def wait(
task: Union[str, DashScopeAPIResponse],
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> DashScopeAPIResponse:
"""Wait for async task completion and return task result.
Expand All @@ -775,6 +804,12 @@ def wait(
task (Union[str, DashScopeAPIResponse]): The task_id, or
async_call response.
api_key (str, optional): The api_key. Defaults to None.
workspace (str, optional): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait
for the task to complete. Default is -1, which means no
timeout. When set to a value > 0, if the task does not
complete within this time, a timeout error response will
be returned.

Returns:
DashScopeAPIResponse: The async task information.
Expand All @@ -784,6 +819,7 @@ def wait(
max_wait_seconds = 5
increment_steps = 3
step = 0
start_time = time.time()
while True:
step += 1
# we start by querying once every second, and double
Expand All @@ -794,6 +830,21 @@ def wait(
# (server side return immediately when ready)
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
if 0 < wait_timeout <= (time.time() - start_time):
Comment thread
songguocola marked this conversation as resolved.
logger.warning(
"Wait task: %s timeout after %s seconds.",
task_id,
wait_timeout,
)
return DashScopeAPIResponse(
request_id=task_id,
status_code=HTTPStatus.REQUEST_TIMEOUT,
code="WaitTaskTimeout",
message=(
f"Wait task: {task_id} timeout after "
f"{wait_timeout} seconds."
),
)
rsp = cls._get(task_id, api_key, workspace=workspace, **kwargs)
if rsp.status_code == HTTPStatus.OK:
if rsp.output is None:
Expand Down
Loading