add volcengine OSS support
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
def _bool_env(name: str, default: bool) -> bool:
|
||||
@@ -26,6 +27,13 @@ class Settings:
|
||||
moss_max_new_tokens: int
|
||||
moss_end_padding_ms: int
|
||||
pass_score: float
|
||||
volcano_oss_access_key: str
|
||||
volcano_oss_secret_key: str
|
||||
volcano_oss_bucket: str
|
||||
volcano_oss_endpoint: str
|
||||
volcano_oss_region: str
|
||||
volcano_oss_key_prefix: str
|
||||
volcano_oss_public_base_url: str
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "Settings":
|
||||
@@ -54,6 +62,19 @@ class Settings:
|
||||
moss_max_new_tokens=int(os.getenv("MOSS_MAX_NEW_TOKENS", "65536")),
|
||||
moss_end_padding_ms=int(os.getenv("MOSS_END_PADDING_MS", "300")),
|
||||
pass_score=float(os.getenv("ASSESSMENT_PASS_SCORE", "70")),
|
||||
volcano_oss_access_key=os.getenv("VOLCANO_OSS_ACCESS_KEY", "").strip(),
|
||||
volcano_oss_secret_key=os.getenv("VOLCANO_OSS_SECRET_KEY", "").strip(),
|
||||
volcano_oss_bucket=os.getenv("VOLCANO_OSS_BUCKET", "").strip(),
|
||||
volcano_oss_endpoint=os.getenv(
|
||||
"VOLCANO_OSS_ENDPOINT", "tos-cn-beijing.volces.com"
|
||||
).strip().strip("/"),
|
||||
volcano_oss_region=os.getenv("VOLCANO_OSS_REGION", "cn-beijing").strip(),
|
||||
volcano_oss_key_prefix=Settings._normalize_key_prefix(
|
||||
os.getenv("VOLCANO_OSS_KEY_PREFIX", "videos/")
|
||||
),
|
||||
volcano_oss_public_base_url=os.getenv(
|
||||
"VOLCANO_OSS_PUBLIC_BASE_URL", ""
|
||||
).strip().rstrip("/"),
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -76,6 +97,34 @@ class Settings:
|
||||
def database_path(self) -> Path:
|
||||
return self.data_dir / "oral_trainer.sqlite3"
|
||||
|
||||
@property
|
||||
def volcano_oss_enabled(self) -> bool:
|
||||
return all(
|
||||
(
|
||||
self.volcano_oss_access_key,
|
||||
self.volcano_oss_secret_key,
|
||||
self.volcano_oss_bucket,
|
||||
self.volcano_oss_endpoint,
|
||||
self.volcano_oss_region,
|
||||
)
|
||||
)
|
||||
|
||||
def volcano_oss_public_url(self, object_key: str) -> str:
|
||||
quoted_key = quote(object_key)
|
||||
if self.volcano_oss_public_base_url:
|
||||
return f"{self.volcano_oss_public_base_url}/{quoted_key}"
|
||||
endpoint = self.volcano_oss_endpoint
|
||||
if "://" in endpoint:
|
||||
scheme, host = endpoint.split("://", 1)
|
||||
else:
|
||||
scheme, host = "https", endpoint
|
||||
return f"{scheme}://{self.volcano_oss_bucket}.{host}/{quoted_key}"
|
||||
|
||||
@staticmethod
|
||||
def _normalize_key_prefix(value: str) -> str:
|
||||
normalized = value.strip().strip("/")
|
||||
return f"{normalized}/" if normalized else ""
|
||||
|
||||
def ensure_directories(self) -> None:
|
||||
for path in (self.data_dir, self.videos_dir, self.work_dir, self.attempts_dir, self.dub_shares_dir):
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user