Skip to content

Commit 791659f

Browse files
authored
Merge pull request #145 from songguocola/dev/audio_0618
funasr support payload.input and bug fix in transcription
2 parents 54f65bf + 97e5cff commit 791659f

7 files changed

Lines changed: 107 additions & 4 deletions

File tree

dashscope/aigc/image_generation.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def wait(
177177
task: Union[str, ImageGenerationResponse], # type: ignore[override]
178178
api_key: str = None,
179179
workspace: str = None,
180+
wait_timeout: int = -1,
180181
**kwargs,
181182
) -> DashScopeAPIResponse:
182183
"""Wait for image(s) synthesis task to complete, and return the result.
@@ -186,11 +187,18 @@ def wait(
186187
ImageGenerationResponse return by async_call().
187188
api_key (str, optional): The api api_key. Defaults to None.
188189
workspace (str): The dashscope workspace id.
190+
wait_timeout (int, optional): The maximum seconds to wait.
191+
Default is -1 (no timeout).
189192
190193
Returns:
191194
DashScopeAPIResponse: The task result.
192195
"""
193-
response = super().wait(task, api_key, workspace=workspace)
196+
response = super().wait(
197+
task,
198+
api_key,
199+
workspace=workspace,
200+
wait_timeout=wait_timeout,
201+
)
194202
return ImageGenerationResponse.from_api_response(response)
195203

196204
@classmethod
@@ -487,6 +495,7 @@ async def wait(
487495
task: Union[str, ImageGenerationResponse], # type: ignore[override]
488496
api_key: str = None,
489497
workspace: str = None,
498+
wait_timeout: int = -1,
490499
**kwargs,
491500
) -> DashScopeAPIResponse:
492501
"""Wait for image(s) synthesis task to complete, and return the result.
@@ -496,11 +505,18 @@ async def wait(
496505
ImageGenerationResponse return by async_call().
497506
api_key (str, optional): The api api_key. Defaults to None.
498507
workspace (str): The dashscope workspace id.
508+
wait_timeout (int, optional): The maximum seconds to wait.
509+
Default is -1 (no timeout).
499510
500511
Returns:
501512
DashScopeAPIResponse: The task result.
502513
"""
503-
response = await super().wait(task, api_key, workspace=workspace)
514+
response = await super().wait(
515+
task,
516+
api_key,
517+
workspace=workspace,
518+
wait_timeout=wait_timeout,
519+
)
504520
return ImageGenerationResponse.from_api_response(response)
505521

506522
@classmethod

dashscope/aigc/image_synthesis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ async def wait(
739739
task: Union[str, ImageSynthesisResponse], # type: ignore[override]
740740
api_key: str = None,
741741
workspace: str = None,
742+
wait_timeout: int = -1,
742743
**kwargs,
743744
) -> ImageSynthesisResponse:
744745
"""Wait for image(s) synthesis task to complete, and return the result.
@@ -748,11 +749,18 @@ async def wait(
748749
ImageSynthesisResponse return by async_call().
749750
api_key (str, optional): The api api_key. Defaults to None.
750751
workspace (str): The dashscope workspace id.
752+
wait_timeout (int, optional): The maximum seconds to wait.
753+
Default is -1 (no timeout).
751754
752755
Returns:
753756
ImageSynthesisResponse: The task result.
754757
"""
755-
response = await super().wait(task, api_key, workspace=workspace)
758+
response = await super().wait(
759+
task,
760+
api_key,
761+
workspace=workspace,
762+
wait_timeout=wait_timeout,
763+
)
756764
return ImageSynthesisResponse.from_api_response(response)
757765

758766
@classmethod

dashscope/aigc/video_synthesis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ async def wait(
894894
task: Union[str, VideoSynthesisResponse], # type: ignore[override]
895895
api_key: str = None,
896896
workspace: str = None,
897+
wait_timeout: int = -1,
897898
**kwargs,
898899
) -> VideoSynthesisResponse:
899900
"""Wait for video synthesis task to complete, and return the result.
@@ -903,11 +904,18 @@ async def wait(
903904
VideoSynthesisResponse return by async_call().
904905
api_key (str, optional): The api api_key. Defaults to None.
905906
workspace (str): The dashscope workspace id.
907+
wait_timeout (int, optional): The maximum seconds to wait.
908+
Default is -1 (no timeout).
906909
907910
Returns:
908911
VideoSynthesisResponse: The task result.
909912
"""
910-
response = await super().wait(task, api_key, workspace=workspace)
913+
response = await super().wait(
914+
task,
915+
api_key,
916+
workspace=workspace,
917+
wait_timeout=wait_timeout,
918+
)
911919
return VideoSynthesisResponse.from_api_response(response)
912920

913921
@classmethod

dashscope/api_entities/api_request_data.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def get_batch_binary_data(self) -> bytes: # type: ignore[return]
157157
return content
158158

159159
def _only_parameters(self) -> str:
160+
temp_input = None
161+
if "raw_input" in self.parameters:
162+
temp_input = self.parameters.pop("raw_input")
160163
obj = {"model": self.model, "parameters": self.parameters, "input": {}}
164+
if temp_input is not None:
165+
obj["input"] = temp_input
161166
if self.task is not None:
162167
obj["task"] = self.task
163168
if self.task_group is not None:

dashscope/audio/asr/transcription.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,20 @@ def wait(
167167
task: Union[str, TranscriptionResponse], # type: ignore[override]
168168
api_key: str = None,
169169
workspace: str = None,
170+
wait_timeout: int = -1,
170171
**kwargs,
171172
) -> TranscriptionResponse:
172173
"""Poll task until the final results of transcription is obtained.
173174
174175
Args:
175176
task (Union[str, TranscriptionResponse]): The task_id or
176177
response including task_id returned from async_call().
178+
api_key (str, optional): The api_key. Defaults to None.
177179
workspace (str): The dashscope workspace id.
180+
wait_timeout (int, optional): The timeout for waiting.
181+
Defaults to -1.That means no timeout.
182+
If set to a value > 0, the task does not complete
183+
within this time, a timeout error response will be returned.
178184
179185
Returns:
180186
TranscriptionResponse: The result of batch transcription.
@@ -183,6 +189,7 @@ def wait(
183189
task,
184190
api_key=api_key,
185191
workspace=workspace,
192+
wait_timeout=wait_timeout,
186193
**kwargs,
187194
)
188195
return TranscriptionResponse.from_api_response(response)

dashscope/audio/qwen_asr/qwen_transcription.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def wait(
127127
task: Union[str, TranscriptionResponse], # type: ignore[override]
128128
api_key: str = None,
129129
workspace: str = None,
130+
wait_timeout: int = -1,
130131
**kwargs,
131132
) -> TranscriptionResponse:
132133
"""Poll task until the final results of transcription is obtained.
@@ -135,6 +136,8 @@ def wait(
135136
task (Union[str, TranscriptionResponse]): The task_id or
136137
response including task_id returned from async_call().
137138
workspace (str): The dashscope workspace id.
139+
wait_timeout (int, optional): The maximum seconds to wait.
140+
Default is -1 (no timeout).
138141
139142
Returns:
140143
TranscriptionResponse: The result of batch transcription.
@@ -143,6 +146,7 @@ def wait(
143146
task,
144147
api_key=api_key,
145148
workspace=workspace,
149+
wait_timeout=wait_timeout,
146150
**kwargs,
147151
)
148152
return TranscriptionResponse.from_api_response(response)

dashscope/client/base_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ async def wait(
191191
task: Union[str, DashScopeAPIResponse],
192192
api_key: str = None,
193193
workspace: str = None,
194+
wait_timeout: int = -1,
194195
**kwargs,
195196
) -> DashScopeAPIResponse:
196197
"""Wait for async task completion and return task result.
@@ -199,6 +200,12 @@ async def wait(
199200
task (Union[str, DashScopeAPIResponse]): The task_id, or
200201
async_call response.
201202
api_key (str, optional): The api_key. Defaults to None.
203+
workspace (str, optional): The dashscope workspace id.
204+
wait_timeout (int, optional): The maximum seconds to wait
205+
for the task to complete. Default is -1, which means no
206+
timeout. When set to a value > 0, if the task does not
207+
complete within this time, a timeout error response will
208+
be returned.
202209
203210
Returns:
204211
DashScopeAPIResponse: The async task information.
@@ -208,6 +215,7 @@ async def wait(
208215
max_wait_seconds = 5
209216
increment_steps = 3
210217
step = 0
218+
start_time = time.time()
211219
while True:
212220
step += 1
213221
# we start by querying once every second, and double
@@ -217,6 +225,23 @@ async def wait(
217225
# (server side return immediately when ready)
218226
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
219227
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
228+
if wait_timeout is not None and 0 < wait_timeout <= (
229+
time.time() - start_time
230+
):
231+
logger.warning(
232+
"Wait task: %s timeout after %s seconds.",
233+
task_id,
234+
wait_timeout,
235+
)
236+
return DashScopeAPIResponse(
237+
request_id=task_id,
238+
status_code=HTTPStatus.REQUEST_TIMEOUT,
239+
code="WaitTaskTimeout",
240+
message=(
241+
f"Wait task: {task_id} timeout after "
242+
f"{wait_timeout} seconds."
243+
),
244+
)
220245
rsp = await cls._get(
221246
task_id,
222247
api_key,
@@ -600,6 +625,10 @@ def call(
600625
**kwargs,
601626
) -> DashScopeAPIResponse:
602627
"""Call service and get result."""
628+
wait_timeout = -1
629+
if "wait_timeout" in kwargs:
630+
wait_timeout = kwargs.pop("wait_timeout")
631+
603632
task_response = cls.async_call( # type: ignore[misc]
604633
*args,
605634
api_key=api_key,
@@ -610,6 +639,7 @@ def call(
610639
task_response,
611640
api_key=api_key,
612641
workspace=workspace,
642+
wait_timeout=wait_timeout,
613643
)
614644
return response
615645

@@ -767,6 +797,7 @@ def wait(
767797
task: Union[str, DashScopeAPIResponse],
768798
api_key: str = None,
769799
workspace: str = None,
800+
wait_timeout: int = -1,
770801
**kwargs,
771802
) -> DashScopeAPIResponse:
772803
"""Wait for async task completion and return task result.
@@ -775,6 +806,12 @@ def wait(
775806
task (Union[str, DashScopeAPIResponse]): The task_id, or
776807
async_call response.
777808
api_key (str, optional): The api_key. Defaults to None.
809+
workspace (str, optional): The dashscope workspace id.
810+
wait_timeout (int, optional): The maximum seconds to wait
811+
for the task to complete. Default is -1, which means no
812+
timeout. When set to a value > 0, if the task does not
813+
complete within this time, a timeout error response will
814+
be returned.
778815
779816
Returns:
780817
DashScopeAPIResponse: The async task information.
@@ -784,6 +821,7 @@ def wait(
784821
max_wait_seconds = 5
785822
increment_steps = 3
786823
step = 0
824+
start_time = time.time()
787825
while True:
788826
step += 1
789827
# we start by querying once every second, and double
@@ -794,6 +832,23 @@ def wait(
794832
# (server side return immediately when ready)
795833
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
796834
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
835+
if wait_timeout is not None and 0 < wait_timeout <= (
836+
time.time() - start_time
837+
):
838+
logger.warning(
839+
"Wait task: %s timeout after %s seconds.",
840+
task_id,
841+
wait_timeout,
842+
)
843+
return DashScopeAPIResponse(
844+
request_id=task_id,
845+
status_code=HTTPStatus.REQUEST_TIMEOUT,
846+
code="WaitTaskTimeout",
847+
message=(
848+
f"Wait task: {task_id} timeout after "
849+
f"{wait_timeout} seconds."
850+
),
851+
)
797852
rsp = cls._get(task_id, api_key, workspace=workspace, **kwargs)
798853
if rsp.status_code == HTTPStatus.OK:
799854
if rsp.output is None:

0 commit comments

Comments
 (0)