362 lines
19 KiB
Python
362 lines
19 KiB
Python
"""配置驱动的通用 HTTP 适配器(多模型适配层)。
|
||
|
||
不同供应商的字段名/鉴权/轮询协议差异很大,这里用「端点 + 字段路径 + 值映射」的声明式配置描述,
|
||
把新供应商接入成本降到「填一段 JSON」;供应商专属签名/SDK 可另写 adapter 并在 registry 注册。
|
||
|
||
config 示例(视频,异步任务式):
|
||
{
|
||
"submit_url": "https://api.vendor.com/v1/videos",
|
||
"poll_url": "https://api.vendor.com/v1/videos/{job_id}",
|
||
"headers": {"X-Project": "prod"},
|
||
"auth": "bearer",
|
||
"body": {"model": "{{model}}", "prompt": "{{prompt}}", "seconds": "{{duration}}",
|
||
"image": "{{first_frame}}", "ratio": "{{aspect_ratio}}"},
|
||
"job_id_path": "id",
|
||
"status_path": "status",
|
||
"progress_path": "progress",
|
||
"result_url_path": "output.url",
|
||
"error_message_path": "error.message",
|
||
"success_values": ["succeeded", "completed"],
|
||
"failure_values": ["failed", "cancelled"],
|
||
"poll_interval": 6, "timeout": 1800,
|
||
"upload_url": "https://api.vendor.com/v1/files", "upload_url_path": "url"
|
||
}
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import re
|
||
import time
|
||
from pathlib import Path
|
||
from typing import Any
|
||
from urllib.parse import quote
|
||
|
||
import httpx
|
||
|
||
from app.config import settings
|
||
from app.providers.base import (Capabilities, FatalProviderError, ProviderError, RateLimitedError,
|
||
RetryableProviderError, VideoGenRequest, VideoGenResult,
|
||
VoiceGenRequest, VoiceGenResult, VideoProviderAdapter, VoiceProviderAdapter)
|
||
from app.services import ffmpeg as ff
|
||
|
||
_RETRYABLE_STATUS = {408, 409, 425, 429, 500, 502, 503, 504, 522, 524}
|
||
|
||
|
||
def get_path(payload: Any, dotted: str | None, default: Any = None) -> Any:
|
||
"""按 'a.b.0.c' 形式取嵌套字段。"""
|
||
if not dotted:
|
||
return payload
|
||
cur = payload
|
||
for part in str(dotted).split("."):
|
||
if isinstance(cur, list):
|
||
try:
|
||
cur = cur[int(part)]
|
||
except (ValueError, IndexError):
|
||
return default
|
||
elif isinstance(cur, dict):
|
||
if part in cur:
|
||
cur = cur[part]
|
||
elif part.rstrip("]").endswith("]"):
|
||
key, _, idx = part.partition("[")
|
||
key = key.rstrip("]")
|
||
if key not in cur:
|
||
return default
|
||
cur = cur[key]
|
||
try:
|
||
cur = cur[int(idx.rstrip("]"))]
|
||
except (ValueError, IndexError):
|
||
return default
|
||
else:
|
||
return default
|
||
else:
|
||
return default
|
||
return default if cur is None else cur
|
||
|
||
|
||
_TEMPLATE = re.compile(r"\{\{\s*([a-zA-Z0-9_.]+)\s*\}\}")
|
||
|
||
|
||
def render_template(value: Any, context: dict[str, Any]) -> Any:
|
||
"""递归渲染 {{field}} 模板;整体为占位符时保留原始类型。"""
|
||
if isinstance(value, dict):
|
||
return {k: render_template(v, context) for k, v in value.items()}
|
||
if isinstance(value, list):
|
||
return [render_template(v, context) for v in value]
|
||
if not isinstance(value, str):
|
||
return value
|
||
whole = _TEMPLATE.fullmatch(value.strip())
|
||
if whole:
|
||
return context.get(whole.group(1))
|
||
return _TEMPLATE.sub(lambda m: "" if context.get(m.group(1)) is None else str(context[m.group(1)]), value)
|
||
|
||
|
||
def _raise_for_response(resp: httpx.Response, *, context: str) -> None:
|
||
if resp.status_code < 400:
|
||
return
|
||
body = resp.text[:1200]
|
||
if resp.status_code == 429:
|
||
retry_after = None
|
||
try:
|
||
retry_after = float(resp.headers.get("Retry-After", ""))
|
||
except (TypeError, ValueError):
|
||
retry_after = None
|
||
raise RateLimitedError(f"{context} 被供应商限流(HTTP 429):{body}", retry_after=retry_after)
|
||
if resp.status_code in _RETRYABLE_STATUS:
|
||
raise RetryableProviderError(f"{context} 供应商暂时不可用(HTTP {resp.status_code}):{body}",
|
||
code=f"http_{resp.status_code}")
|
||
raise FatalProviderError(f"{context} 请求被供应商拒绝(HTTP {resp.status_code}):{body}",
|
||
code=f"http_{resp.status_code}")
|
||
|
||
|
||
def _headers(config: dict, api_key: str) -> dict[str, str]:
|
||
headers = {"Content-Type": "application/json"}
|
||
headers.update({str(k): str(v) for k, v in (config.get("headers") or {}).items()})
|
||
auth = str(config.get("auth", "bearer")).lower()
|
||
key = config.get("api_key") or api_key
|
||
if key:
|
||
if auth == "bearer":
|
||
headers.setdefault("Authorization", f"Bearer {key}")
|
||
elif auth == "header":
|
||
headers[config.get("auth_header", "X-Api-Key")] = key
|
||
elif auth == "basic":
|
||
import base64
|
||
|
||
headers.setdefault("Authorization", "Basic " + base64.b64encode(key.encode()).decode())
|
||
return headers
|
||
|
||
|
||
def _client(config: dict) -> httpx.Client:
|
||
return httpx.Client(timeout=float(config.get("timeout_seconds", 90)),
|
||
follow_redirects=bool(config.get("follow_redirects", True)))
|
||
|
||
|
||
class HttpVideoProvider(VideoProviderAdapter):
|
||
_name = "http"
|
||
|
||
@property
|
||
def name(self) -> str:
|
||
return self._name
|
||
|
||
def is_available(self, config: dict | None = None) -> bool:
|
||
config = config or {}
|
||
return bool(config.get("base_url") or config.get("url") or settings.video_api_base_url)
|
||
|
||
def capabilities(self, config: dict | None = None) -> Capabilities:
|
||
config = config or {}
|
||
caps = config.get("capabilities") or {}
|
||
return Capabilities(
|
||
modes=tuple(caps.get("modes") or ("t2v", "i2v")),
|
||
resolutions=tuple(caps.get("resolutions") or ("720p", "1080p")),
|
||
aspect_ratios=tuple(caps.get("aspect_ratios") or ("16:9", "9:16")),
|
||
min_duration=float(caps.get("min_duration", 1.0)),
|
||
max_duration=float(caps.get("max_duration", 10.0)),
|
||
max_reference_images=int(caps.get("max_reference_images", 1)),
|
||
needs_public_media=bool(caps.get("needs_public_media", True)),
|
||
notes=str(caps.get("notes") or "通用 HTTP 契约适配器,需按供应商文档填写 config_json"),
|
||
)
|
||
|
||
def prepare_media(self, local_path: str, config: dict | None = None) -> str:
|
||
"""关键帧/定妆图需以供应商可访问地址下发;配置 upload_url 时自动上传。"""
|
||
config = config or {}
|
||
upload_url = config.get("upload_url")
|
||
if not upload_url:
|
||
return local_path
|
||
key = config.get("api_key") or settings.video_api_key
|
||
headers = {k: v for k, v in _headers(config, key).items() if k.lower() != "content-type"}
|
||
with _client(config) as client:
|
||
with open(local_path, "rb") as fh:
|
||
resp = client.post(upload_url, headers=headers, files={"file": (Path(local_path).name, fh)})
|
||
_raise_for_response(resp, context="素材上传")
|
||
data = resp.json()
|
||
url = get_path(data, config.get("upload_url_path", "url"))
|
||
if not url:
|
||
raise FatalProviderError(f"素材上传响应缺少可访问地址:{data}", code="upload_response")
|
||
return str(url)
|
||
|
||
def _context(self, request: VideoGenRequest, model: str, config: dict) -> dict[str, Any]:
|
||
first = request.first_frame or ""
|
||
last = request.last_frame or ""
|
||
refs = list(request.reference_images)
|
||
if config.get("upload_media", True):
|
||
first = self.prepare_media(first, config) if first else ""
|
||
last = self.prepare_media(last, config) if last else ""
|
||
refs = [self.prepare_media(p, config) for p in refs]
|
||
return {
|
||
"prompt": request.prompt, "negative_prompt": request.negative_prompt,
|
||
"duration": request.duration_seconds, "seconds": request.duration_seconds,
|
||
"aspect_ratio": request.aspect_ratio, "resolution": request.resolution, "fps": request.fps,
|
||
"mode": request.mode, "model": model, "seed": request.seed,
|
||
"first_frame": first, "last_frame": last, "references": refs,
|
||
"reference_images": refs, "idempotency_key": request.idempotency_key, "label": request.label,
|
||
"params": request.params or {},
|
||
}
|
||
|
||
def generate(self, request: VideoGenRequest, config: dict | None = None) -> VideoGenResult:
|
||
config = dict(config or {})
|
||
base_url = str(config.get("base_url") or settings.video_api_base_url or "").rstrip("/")
|
||
if not base_url:
|
||
raise FatalProviderError("HTTP 适配器缺少 base_url / VIDEO_API_BASE_URL", code="provider_not_configured")
|
||
config["base_url"] = base_url
|
||
model = str(request.params.get("model") or config.get("model") or settings.video_model or "")
|
||
submit_url = str(render_template(config.get("submit_url") or f"{base_url}/videos",
|
||
{"base_url": base_url, "model": model}))
|
||
ctx = self._context(request, model or "", config)
|
||
body = render_template(config.get("body") or {
|
||
"model": "{{model}}", "prompt": "{{prompt}}", "negative_prompt": "{{negative_prompt}}",
|
||
"duration": "{{duration}}", "aspect_ratio": "{{aspect_ratio}}", "resolution": "{{resolution}}",
|
||
"image_url": "{{first_frame}}", "last_image_url": "{{last_frame}}", "reference_images": "{{references}}",
|
||
}, ctx)
|
||
body = {k: v for k, v in body.items() if v not in (None, "", [], {})} if isinstance(body, dict) else body
|
||
headers = _headers(config, settings.video_api_key)
|
||
timeout = float(config.get("timeout", 1800))
|
||
interval = float(config.get("poll_interval", settings.video_poll_interval_seconds))
|
||
|
||
with _client(config) as client:
|
||
try:
|
||
resp = client.post(submit_url, headers=headers, json=body)
|
||
except httpx.HTTPError as exc:
|
||
raise RetryableProviderError(f"提交生成任务网络异常:{exc}") from exc
|
||
_raise_for_response(resp, context="提交生成任务")
|
||
try:
|
||
data = resp.json()
|
||
except json.JSONDecodeError as exc:
|
||
raise FatalProviderError(f"提交生成任务响应不是 JSON:{resp.text[:400]}", code="bad_response") from exc
|
||
|
||
job_id = get_path(data, config.get("job_id_path", "id")) or get_path(data, "job_id") or get_path(data, "task_id")
|
||
if not job_id:
|
||
raise FatalProviderError(f"提交响应缺少任务 ID(job_id_path={config.get('job_id_path', 'id')}):{data}",
|
||
code="missing_job_id")
|
||
deadline = time.time() + timeout
|
||
last_progress = -1.0
|
||
while time.time() < deadline:
|
||
poll_tpl = config.get("poll_url") or f"{base_url}/videos/{quote(str(job_id))}"
|
||
poll_url = str(render_template(poll_tpl, {"base_url": base_url, "job_id": job_id, "id": job_id}))
|
||
try:
|
||
pr = client.get(poll_url, headers=headers)
|
||
except httpx.HTTPError as exc:
|
||
raise RetryableProviderError(f"轮询任务状态网络异常:{exc}") from exc
|
||
_raise_for_response(pr, context="轮询任务状态")
|
||
state = pr.json()
|
||
status = str(get_path(state, config.get("status_path", "status"), "")).lower()
|
||
progress = get_path(state, config.get("progress_path"))
|
||
if progress is not None:
|
||
try:
|
||
last_progress = float(progress)
|
||
except (TypeError, ValueError):
|
||
pass
|
||
fail_values = {str(v).lower() for v in (config.get("failure_values") or ["failed", "error", "cancelled"])}
|
||
if status in fail_values:
|
||
msg = get_path(state, config.get("error_message_path", "error.message")) or state
|
||
raise FatalProviderError(f"供应商生成失败:{msg}", code="provider_reported_failure",
|
||
retryable=bool(config.get("failure_retryable", False)))
|
||
ok_values = {str(v).lower() for v in (config.get("success_values") or ["succeeded", "completed", "success"])}
|
||
url = get_path(state, config.get("result_url_path", "output.url")) or get_path(state, "result_url") \
|
||
or get_path(state, "video_url")
|
||
if status in ok_values and url:
|
||
out = Path(request.output_path or f"{settings.media_root}/http/out.mp4")
|
||
out.parent.mkdir(parents=True, exist_ok=True)
|
||
self._download(client, str(url), out, config, str(job_id))
|
||
duration = ff.duration_ms(out) / 1000 if ff.available() else request.duration_seconds
|
||
cost = float(get_path(state, config.get("cost_path", "cost"), 0.0) or 0.0)
|
||
return VideoGenResult(file_path=str(out), provider_job_id=str(job_id),
|
||
duration_seconds=duration or request.duration_seconds, cost=cost,
|
||
metadata={"state": _trim(state), "progress": last_progress})
|
||
if status not in ok_values and not url:
|
||
time.sleep(interval)
|
||
continue
|
||
time.sleep(interval)
|
||
raise ProviderError(f"供应商任务 {job_id} 轮询超时({timeout:.0f}s)", code="provider_timeout", retryable=True)
|
||
|
||
def _download(self, client: httpx.Client, url: str, out: Path, config: dict, job_id: str) -> None:
|
||
dl_headers = {k: v for k, v in (config.get("download_headers") or {}).items()}
|
||
try:
|
||
with client.stream("GET", url, headers=dl_headers, timeout=float(config.get("download_timeout", 300))) as r:
|
||
if r.status_code >= 400:
|
||
_raise_for_response(r, context="下载生成结果")
|
||
with out.open("wb") as fh:
|
||
for chunk in r.iter_bytes():
|
||
fh.write(chunk)
|
||
except httpx.HTTPError as exc:
|
||
raise RetryableProviderError(f"下载生成结果失败:{exc}", code="download_failed") from exc
|
||
if not out.exists() or out.stat().st_size < 1024:
|
||
raise RetryableProviderError(f"下载的产物文件异常(job={job_id})", code="downloaded_file_invalid")
|
||
|
||
@staticmethod
|
||
def _trim(state: Any, limit: int = 4000) -> dict:
|
||
raw = json.dumps(state, ensure_ascii=False, default=str)
|
||
return {"raw": raw[:limit]}
|
||
|
||
|
||
class HttpVoiceProvider(VoiceProviderAdapter):
|
||
"""通用 HTTP TTS:支持「直接返回音频二进制」与「异步任务 + 轮询下载」两种协议。"""
|
||
|
||
_name = "http"
|
||
|
||
@property
|
||
def name(self) -> str:
|
||
return self._name
|
||
|
||
def is_available(self, config: dict | None = None) -> bool:
|
||
config = config or {}
|
||
return bool(config.get("base_url") or config.get("url") or settings.tts_api_base_url)
|
||
|
||
def synthesize(self, request: VoiceGenRequest, config: dict | None = None) -> VoiceGenResult:
|
||
config = dict(config or {})
|
||
base_url = str(config.get("base_url") or settings.tts_api_base_url or "").rstrip("/")
|
||
if not base_url:
|
||
raise FatalProviderError("HTTP TTS 适配器缺少 base_url / TTS_API_BASE_URL", code="provider_not_configured")
|
||
url = str(config.get("url") or f"{base_url}/audio/speech")
|
||
model = config.get("model") or settings.tts_voice
|
||
ctx = {"text": request.text, "voice": request.provider_voice_id or model,
|
||
"voice_id": request.provider_voice_id, "model": config.get("model", ""),
|
||
"language": request.language, "speed": request.speed, "pitch": request.pitch,
|
||
"volume": request.volume, "sample_rate": request.sample_rate, "format": "wav",
|
||
"params": request.params or {}}
|
||
body = render_template(config.get("body") or {
|
||
"model": "{{model}}", "voice": "{{voice}}", "input": "{{text}}",
|
||
"speed": "{{speed}}", "response_format": "mp3"}, ctx)
|
||
body = {k: v for k, v in body.items() if v not in (None, "")} if isinstance(body, dict) else body
|
||
headers = _headers(config, settings.tts_api_key)
|
||
out = Path(request.output_path or f"{settings.media_root}/tts/voice.wav")
|
||
out.parent.mkdir(parents=True, exist_ok=True)
|
||
with _client(config) as client:
|
||
try:
|
||
resp = client.post(url, headers=headers, json=body)
|
||
except httpx.HTTPError as exc:
|
||
raise RetryableProviderError(f"TTS 请求网络异常:{exc}") from exc
|
||
_raise_for_response(resp, context="TTS 请求")
|
||
ctype = resp.headers.get("content-type", "")
|
||
if not ctype.startswith("application/json") and len(resp.content) > 256:
|
||
out.write_bytes(resp.content)
|
||
else:
|
||
data = resp.json()
|
||
job_id = get_path(data, config.get("job_id_path", "id"))
|
||
audio = get_path(data, config.get("audio_path", "url"))
|
||
deadline = time.time() + float(config.get("timeout", 600))
|
||
while not audio and job_id:
|
||
time.sleep(float(config.get("poll_interval", 3)))
|
||
if time.time() > deadline:
|
||
raise ProviderError(f"TTS 任务 {job_id} 超时", code="provider_timeout", retryable=True)
|
||
poll_url = str(render_template(config.get("poll_url") or f"{base_url}/audio/{job_id}",
|
||
{"base_url": base_url, "job_id": job_id, "id": job_id}))
|
||
pr = client.get(poll_url, headers=headers)
|
||
_raise_for_response(pr, context="TTS 轮询")
|
||
state = pr.json()
|
||
status = str(get_path(state, config.get("status_path", "status"), "")).lower()
|
||
if status in {str(v).lower() for v in (config.get("failure_values") or ["failed", "error"])}:
|
||
raise FatalProviderError(f"TTS 供应商返回失败:{state}", code="provider_reported_failure")
|
||
audio = get_path(state, config.get("audio_path", "url")) or get_path(state, "audio_url")
|
||
if not audio:
|
||
raise FatalProviderError(f"TTS 响应缺少音频地址:{data}", code="missing_audio")
|
||
dl_headers = {k: v for k, v in (config.get("download_headers") or {}).items()}
|
||
with client.stream("GET", str(audio), headers=dl_headers) as r:
|
||
_raise_for_response(r, context="下载配音")
|
||
with out.open("wb") as fh:
|
||
for chunk in r.iter_bytes():
|
||
fh.write(chunk)
|
||
if out.stat().st_size < 256:
|
||
raise RetryableProviderError("配音文件为空", code="empty_audio")
|
||
duration = ff.duration_ms(out)
|
||
return VoiceGenResult(file_path=str(out), duration_ms=duration, provider_job_id="", cost=0.0,
|
||
metadata={"provider": "http", "url": url})
|