fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解
This commit is contained in:
@@ -11,18 +11,18 @@ from typing import Any
|
||||
|
||||
class TingwuClient:
|
||||
def __init__(self) -> None:
|
||||
self.access_key = os.getenv("ALI_ACCESS_KEY", "")
|
||||
self.secret_key = os.getenv("ALI_SECRET_KEY", "")
|
||||
self.endpoint = "https://tingwu.cn-beijing.aliyuncs.com"
|
||||
self.access_key = os.getenv("ALI_ACCESS_KEY", "")
|
||||
self.secret_key = os.getenv("ALI_SECRET_KEY", "")
|
||||
self.endpoint = "https://tingwu.cn-beijing.aliyuncs.com"
|
||||
|
||||
if not self.access_key or not self.secret_key:
|
||||
raise ValueError("ALI_ACCESS_KEY and ALI_SECRET_KEY required")
|
||||
|
||||
def _sign_request(
|
||||
self, method: str, uri: str, query: str = "", body: str = ""
|
||||
self, method: str, uri: str, query: str = "", body: str = ""
|
||||
) -> dict[str, str]:
|
||||
"""阿里云签名 V3"""
|
||||
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
# 简化签名,实际生产需要完整实现
|
||||
# 这里使用基础认证头
|
||||
@@ -34,7 +34,7 @@ class TingwuClient:
|
||||
"Authorization": f"ACS3-HMAC-SHA256 Credential = {self.access_key}/acs/tingwu/cn-beijing",
|
||||
}
|
||||
|
||||
def create_task(self, audio_url: str, language: str = "zh") -> str:
|
||||
def create_task(self, audio_url: str, language: str = "zh") -> str:
|
||||
"""创建听悟任务"""
|
||||
try:
|
||||
# 导入移到文件顶部会导致循环导入,保持在这里
|
||||
@@ -42,23 +42,23 @@ class TingwuClient:
|
||||
from alibabacloud_tingwu20230930 import models as tingwu_models
|
||||
from alibabacloud_tingwu20230930.client import Client as TingwuSDKClient
|
||||
|
||||
config = open_api_models.Config(
|
||||
access_key_id = self.access_key, access_key_secret = self.secret_key
|
||||
config = open_api_models.Config(
|
||||
access_key_id=self.access_key, access_key_secret=self.secret_key
|
||||
)
|
||||
config.endpoint = "tingwu.cn-beijing.aliyuncs.com"
|
||||
client = TingwuSDKClient(config)
|
||||
config.endpoint = "tingwu.cn-beijing.aliyuncs.com"
|
||||
client = TingwuSDKClient(config)
|
||||
|
||||
request = tingwu_models.CreateTaskRequest(
|
||||
type = "offline",
|
||||
input = tingwu_models.Input(source = "OSS", file_url = audio_url),
|
||||
parameters = tingwu_models.Parameters(
|
||||
transcription = tingwu_models.Transcription(
|
||||
diarization_enabled = True, sentence_max_length = 20
|
||||
request = tingwu_models.CreateTaskRequest(
|
||||
type="offline",
|
||||
input=tingwu_models.Input(source="OSS", file_url=audio_url),
|
||||
parameters=tingwu_models.Parameters(
|
||||
transcription=tingwu_models.Transcription(
|
||||
diarization_enabled=True, sentence_max_length=20
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
response = client.create_task(request)
|
||||
response = client.create_task(request)
|
||||
if response.body.code == "0":
|
||||
return response.body.data.task_id
|
||||
else:
|
||||
@@ -73,26 +73,29 @@ class TingwuClient:
|
||||
return f"mock_task_{int(time.time())}"
|
||||
|
||||
def get_task_result(
|
||||
self, task_id: str, max_retries: int = 60, interval: int = 5
|
||||
self, task_id: str, max_retries: int = 60, interval: int = 5
|
||||
) -> dict[str, Any]:
|
||||
"""获取任务结果"""
|
||||
try:
|
||||
# 导入移到文件顶部会导致循环导入,保持在这里
|
||||
from alibabacloud_tingwu20230930 import models as tingwu_models
|
||||
from alibabacloud_tingwu20230930.client import Client as TingwuSDKClient
|
||||
from alibabacloud_openapi_util import models as open_api_models
|
||||
|
||||
config = open_api_models.Config(
|
||||
access_key_id = self.access_key, access_key_secret = self.secret_key
|
||||
config = open_api_models.Config(
|
||||
access_key_id=self.access_key, access_key_secret=self.secret_key
|
||||
)
|
||||
config.endpoint = "tingwu.cn-beijing.aliyuncs.com"
|
||||
client = TingwuSDKClient(config)
|
||||
config.endpoint = "tingwu.cn-beijing.aliyuncs.com"
|
||||
client = TingwuSDKClient(config)
|
||||
|
||||
for i in range(max_retries):
|
||||
request = tingwu_models.GetTaskInfoRequest()
|
||||
response = client.get_task_info(task_id, request)
|
||||
request = tingwu_models.GetTaskInfoRequest()
|
||||
response = client.get_task_info(task_id, request)
|
||||
|
||||
if response.body.code != "0":
|
||||
raise RuntimeError(f"Query failed: {response.body.message}")
|
||||
|
||||
status = response.body.data.task_status
|
||||
status = response.body.data.task_status
|
||||
|
||||
if status == "SUCCESS":
|
||||
return self._parse_result(response.body.data)
|
||||
@@ -113,11 +116,11 @@ class TingwuClient:
|
||||
|
||||
def _parse_result(self, data) -> dict[str, Any]:
|
||||
"""解析结果"""
|
||||
result = data.result
|
||||
transcription = result.transcription
|
||||
result = data.result
|
||||
transcription = result.transcription
|
||||
|
||||
full_text = ""
|
||||
segments = []
|
||||
full_text = ""
|
||||
segments = []
|
||||
|
||||
if transcription.paragraphs:
|
||||
for para in transcription.paragraphs:
|
||||
@@ -150,8 +153,8 @@ class TingwuClient:
|
||||
],
|
||||
}
|
||||
|
||||
def transcribe(self, audio_url: str, language: str = "zh") -> dict[str, Any]:
|
||||
def transcribe(self, audio_url: str, language: str = "zh") -> dict[str, Any]:
|
||||
"""一键转录"""
|
||||
task_id = self.create_task(audio_url, language)
|
||||
task_id = self.create_task(audio_url, language)
|
||||
print(f"Tingwu task: {task_id}")
|
||||
return self.get_task_result(task_id)
|
||||
|
||||
Reference in New Issue
Block a user