fix(asr): avoid serializing requests session in dashscope calls
parent
31708df6cb
commit
aa99ee1f6a
|
|
@ -1,5 +1,4 @@
|
||||||
import uuid
|
import uuid
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import redis
|
import redis
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -39,6 +38,25 @@ class AsyncTranscriptionService:
|
||||||
normalized = normalized[: -len(suffix)]
|
normalized = normalized[: -len(suffix)]
|
||||||
return normalized or None
|
return normalized or None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _resolve_dashscope_api_key(audio_config: Optional[Dict[str, Any]] = None) -> str:
|
||||||
|
api_key = (audio_config or {}).get("api_key") or QWEN_API_KEY
|
||||||
|
if isinstance(api_key, str):
|
||||||
|
api_key = api_key.strip()
|
||||||
|
if not api_key:
|
||||||
|
raise Exception("未配置 DashScope API Key")
|
||||||
|
return api_key
|
||||||
|
|
||||||
|
def _build_dashscope_request_options(self, audio_config: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||||
|
request_options: Dict[str, Any] = {
|
||||||
|
"api_key": self._resolve_dashscope_api_key(audio_config),
|
||||||
|
}
|
||||||
|
endpoint_url = (audio_config or {}).get("endpoint_url")
|
||||||
|
base_address = self._normalize_dashscope_base_address(endpoint_url)
|
||||||
|
if base_address:
|
||||||
|
request_options["base_address"] = base_address
|
||||||
|
return request_options
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _build_dashscope_call_params(audio_config: Dict[str, Any], file_url: str) -> Dict[str, Any]:
|
def _build_dashscope_call_params(audio_config: Dict[str, Any], file_url: str) -> Dict[str, Any]:
|
||||||
model_name = audio_config.get("model") or "paraformer-v2"
|
model_name = audio_config.get("model") or "paraformer-v2"
|
||||||
|
|
@ -74,22 +92,14 @@ class AsyncTranscriptionService:
|
||||||
if provider != "dashscope":
|
if provider != "dashscope":
|
||||||
raise Exception(f"当前仅支持 DashScope 音频识别测试,暂不支持供应商: {provider}")
|
raise Exception(f"当前仅支持 DashScope 音频识别测试,暂不支持供应商: {provider}")
|
||||||
|
|
||||||
dashscope.api_key = audio_config.get("api_key") or QWEN_API_KEY
|
request_options = self._build_dashscope_request_options(audio_config)
|
||||||
|
dashscope.api_key = request_options["api_key"]
|
||||||
target_file_url = (
|
target_file_url = (
|
||||||
test_file_url
|
test_file_url
|
||||||
or "https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav"
|
or "https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav"
|
||||||
)
|
)
|
||||||
call_params = self._build_dashscope_call_params(audio_config, target_file_url)
|
call_params = self._build_dashscope_call_params(audio_config, target_file_url)
|
||||||
base_address = self._normalize_dashscope_base_address(audio_config.get("endpoint_url"))
|
response = Transcription.async_call(**request_options, **call_params)
|
||||||
|
|
||||||
session = self._create_requests_session()
|
|
||||||
try:
|
|
||||||
if base_address:
|
|
||||||
response = Transcription.async_call(base_address=base_address, session=session, **call_params)
|
|
||||||
else:
|
|
||||||
response = Transcription.async_call(session=session, **call_params)
|
|
||||||
finally:
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
if response.status_code != HTTPStatus.OK:
|
if response.status_code != HTTPStatus.OK:
|
||||||
raise Exception(response.message or "音频模型测试失败")
|
raise Exception(response.message or "音频模型测试失败")
|
||||||
|
|
@ -143,9 +153,9 @@ class AsyncTranscriptionService:
|
||||||
if provider != "dashscope":
|
if provider != "dashscope":
|
||||||
raise Exception(f"当前仅支持 DashScope 音频识别,暂不支持供应商: {provider}")
|
raise Exception(f"当前仅支持 DashScope 音频识别,暂不支持供应商: {provider}")
|
||||||
|
|
||||||
dashscope.api_key = audio_config.get("api_key") or QWEN_API_KEY
|
request_options = self._build_dashscope_request_options(audio_config)
|
||||||
|
dashscope.api_key = request_options["api_key"]
|
||||||
call_params = self._build_dashscope_call_params(audio_config, file_url)
|
call_params = self._build_dashscope_call_params(audio_config, file_url)
|
||||||
base_address = self._normalize_dashscope_base_address(audio_config.get("endpoint_url"))
|
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"Starting transcription for meeting_id: {meeting_id}, "
|
f"Starting transcription for meeting_id: {meeting_id}, "
|
||||||
|
|
@ -154,14 +164,7 @@ class AsyncTranscriptionService:
|
||||||
)
|
)
|
||||||
|
|
||||||
# 3. 调用Paraformer异步API
|
# 3. 调用Paraformer异步API
|
||||||
session = self._create_requests_session()
|
task_response = Transcription.async_call(**request_options, **call_params)
|
||||||
try:
|
|
||||||
if base_address:
|
|
||||||
task_response = Transcription.async_call(base_address=base_address, session=session, **call_params)
|
|
||||||
else:
|
|
||||||
task_response = Transcription.async_call(session=session, **call_params)
|
|
||||||
finally:
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
if task_response.status_code != HTTPStatus.OK:
|
if task_response.status_code != HTTPStatus.OK:
|
||||||
print(f"Failed to start transcription: {task_response.status_code}, {task_response.message}")
|
print(f"Failed to start transcription: {task_response.status_code}, {task_response.message}")
|
||||||
|
|
@ -235,11 +238,11 @@ class AsyncTranscriptionService:
|
||||||
|
|
||||||
# 2. 查询外部API获取状态
|
# 2. 查询外部API获取状态
|
||||||
try:
|
try:
|
||||||
session = self._create_requests_session()
|
request_options = self._build_dashscope_request_options(
|
||||||
try:
|
SystemConfigService.get_active_audio_model_config("asr")
|
||||||
paraformer_response = Transcription.fetch(task=paraformer_task_id, session=session)
|
)
|
||||||
finally:
|
dashscope.api_key = request_options["api_key"]
|
||||||
session.close()
|
paraformer_response = Transcription.fetch(task=paraformer_task_id, **request_options)
|
||||||
if paraformer_response.status_code != HTTPStatus.OK:
|
if paraformer_response.status_code != HTTPStatus.OK:
|
||||||
raise Exception(f"Failed to fetch task status from provider: {paraformer_response.message}")
|
raise Exception(f"Failed to fetch task status from provider: {paraformer_response.message}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue