feat: align subtitles from first 10 seconds audio
This commit is contained in:
1 parent
93e06ca40a
commit
5228765473
11 files changed
+248
-22
No files matched your search
@@ -1,8 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
from .ai_config import DeepSeekConfig
|
||||
|
||||
@@ -52,3 +54,53 @@ class DeepSeekClient:
|
||||
return json.loads(content)
|
||||
except json.JSONDecodeError as error:
|
||||
raise DeepSeekError("DeepSeek 返回内容不是有效 JSON。") from error
|
||||
|
||||
def audio_json(self, system_prompt: str, audio_path: Path, user_text: str) -> dict[str, object]:
|
||||
if not self.config.api_key:
|
||||
raise DeepSeekError("未配置 AI_API_KEY。")
|
||||
|
||||
audio_data = base64.b64encode(audio_path.read_bytes()).decode("ascii")
|
||||
body = json.dumps({
|
||||
"model": self.config.audio_model,
|
||||
"messages": [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "text", "text": user_text},
|
||||
{
|
||||
"type": "input_audio",
|
||||
"input_audio": {
|
||||
"data": f"data:audio/wav;base64,{audio_data}",
|
||||
"format": "wav",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
"response_format": {"type": "json_object"},
|
||||
}).encode("utf-8")
|
||||
request = urllib.request.Request(
|
||||
f"{self.config.base_url}/chat/completions",
|
||||
data=body,
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.config.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method="POST",
|
||||
)
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=self.timeout_seconds) as response:
|
||||
payload = json.loads(response.read().decode("utf-8"))
|
||||
except urllib.error.HTTPError as error:
|
||||
detail = error.read().decode("utf-8", errors="replace")
|
||||
raise DeepSeekError(f"Qwen 音频识别请求失败:{error.code} {detail}") from error
|
||||
except Exception as error:
|
||||
raise DeepSeekError(f"Qwen 音频识别请求失败:{error}") from error
|
||||
|
||||
content = payload.get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
try:
|
||||
return json.loads(content)
|
||||
except json.JSONDecodeError as error:
|
||||
raise DeepSeekError("Qwen 音频识别返回内容不是有效 JSON。") from error
|
||||
Reference in new issue
Block a user