Spaces:
Runtime error
Runtime error
import hashlib | |
import os | |
from pathlib import Path | |
from typing import Any, cast | |
import pandoc | |
class Unspecified: | |
pass | |
UNSPECIFIED = Unspecified() | |
FALSISH_STRINGS = {"", "0", "false", "n", "no", "off", "disabled"} | |
def env_str(name: str, default: str | Unspecified = UNSPECIFIED) -> str: | |
value = os.environ.get(name) | |
if value is None: | |
if isinstance(default, Unspecified): | |
raise ValueError(f"environment variable {name} is not set") | |
return default | |
return value | |
def env_bool(name: str, default: bool | Unspecified = UNSPECIFIED) -> bool: | |
value = os.environ.get(name) | |
if value is None: | |
if isinstance(default, Unspecified): | |
raise ValueError(f"environment variable {name} is not set") | |
return default | |
return value.lower() not in FALSISH_STRINGS | |
def data_dir(name: str) -> Path: | |
rv = Path(env_str("DATA_DIR", "/var/tts-service")) / name | |
if not rv.exists(): | |
os.makedirs(rv) | |
return Path(rv) | |
def cache_path(*keys: Any, extension: str | None = None) -> Path: | |
key = "\0".join(str(k) for k in keys) | |
hash = hashlib.md5(key.encode()).hexdigest() | |
dir_path = data_dir("cache") / hash[0:2] | |
if not dir_path.exists(): | |
os.makedirs(dir_path) | |
name = f"{hash[2:]}" | |
if extension: | |
name += f".{extension}" | |
return dir_path / name | |
def html_to_markdown(html: str) -> str: | |
doc = pandoc.read(html, format="html") | |
md = pandoc.write(doc, format="markdown-smart", options=["--wrap=none"]) | |
return cast(str, md) | |
def markdown_to_text(md: str) -> str: | |
doc = pandoc.read(md, format="markdown") | |
txt = pandoc.write(doc, format="plain-smart", options=["--wrap=none"]) | |
return cast(str, txt) | |
def seconds_to_ms(seconds: float) -> str: | |
minutes = int(seconds) // 60 | |
seconds = int(seconds) % 60 | |
ds = int((seconds - int(seconds)) * 10) | |
return f"{minutes:02}:{seconds:02}.{ds:01}" | |