File size: 4,501 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Helpers for clip cleanup settings and normalization.
"""

from __future__ import annotations

from typing import Any

# Pauses shorter than this stay in the cut. 700ms was eating ordinary breaths
# in a conversation (about a fifth of a talking-head clip) and the junctions
# sounded chopped. 1.2s still removes real dead air.
DEFAULT_PAUSE_THRESHOLD_MS = 1200
# New tasks clean up pacing unless the request explicitly opts out. Settings
# loaded for existing tasks keep treating a missing value as off.
NEW_TASK_CUT_LONG_PAUSES = True
NEW_TASK_REMOVE_FILLER_WORDS = True
# Pure disfluencies only: phrases like "kind of" or "you know" often carry
# meaning, so they are left to the user's own filtered_words list. "um" and
# "er" are English hesitations but ordinary words elsewhere (German "um" is a
# preposition, "er" means "he"), so English fillers only apply to English.
DEFAULT_FILTERED_WORDS = [
    "um",
    "umm",
    "uh",
    "uhh",
    "uhm",
    "erm",
    "hmm",
    "mm",
]
# Hesitation sounds that aren't ordinary words in any supported language.
LANGUAGE_NEUTRAL_FILTERED_WORDS = ["uh", "uhh", "uhm", "hmm", "mm"]
LANGUAGE_FILTERED_WORDS = {
    "en": DEFAULT_FILTERED_WORDS,
    "de": [*LANGUAGE_NEUTRAL_FILTERED_WORDS, "äh", "ähm", "öhm"],
    "fr": [*LANGUAGE_NEUTRAL_FILTERED_WORDS, "euh", "heu"],
}


def default_filtered_words(language_code: Any) -> list[str]:
    """Built-in filler words for a transcript language (e.g. "en_us", "de").

    Unknown languages, including transcripts cached before the language was
    recorded, get only the language-neutral hesitation sounds.
    """
    if isinstance(language_code, str) and language_code.strip():
        base = language_code.strip().lower().replace("-", "_").split("_", 1)[0]
        if base in LANGUAGE_FILTERED_WORDS:
            return list(LANGUAGE_FILTERED_WORDS[base])
    return list(LANGUAGE_NEUTRAL_FILTERED_WORDS)


def normalize_pause_threshold_ms(
    value: Any, default: int = DEFAULT_PAUSE_THRESHOLD_MS
) -> int:
    try:
        parsed = int(value)
    except (TypeError, ValueError):
        parsed = default
    return max(250, min(3000, parsed))


def normalize_filtered_words(value: Any) -> list[str]:
    if isinstance(value, str):
        raw_items = value.split(",")
    elif isinstance(value, list):
        raw_items = value
    else:
        raw_items = []

    normalized: list[str] = []
    seen: set[str] = set()
    for item in raw_items:
        if not isinstance(item, str):
            continue
        cleaned = " ".join(item.strip().lower().split())
        if not cleaned or cleaned in seen:
            continue
        seen.add(cleaned)
        normalized.append(cleaned)
    return normalized


def normalize_clip_cleanup_settings(
    cut_long_pauses: Any = False,
    pause_threshold_ms: Any = DEFAULT_PAUSE_THRESHOLD_MS,
    remove_filler_words: Any = False,
    filtered_words: Any = None,
) -> dict[str, Any]:
    return {
        "cut_long_pauses": bool(cut_long_pauses),
        "pause_threshold_ms": normalize_pause_threshold_ms(pause_threshold_ms),
        "remove_filler_words": bool(remove_filler_words),
        "filtered_words": normalize_filtered_words(filtered_words),
    }


def new_task_cleanup_settings(data: dict[str, Any]) -> dict[str, Any]:
    """Cleanup settings for a new task request.

    Omitted toggles (API key / MCP clients) get the new-task defaults; an
    explicit false still turns the cleanup off.
    """
    return normalize_clip_cleanup_settings(
        data.get("cut_long_pauses", NEW_TASK_CUT_LONG_PAUSES),
        data.get("pause_threshold_ms"),
        data.get("remove_filler_words", NEW_TASK_REMOVE_FILLER_WORDS),
        data.get("filtered_words"),
    )


def updated_cleanup_settings(
    payload: dict[str, Any], stored: dict[str, Any] | None
) -> dict[str, Any]:
    """Cleanup settings for a task update; omitted fields keep stored values."""
    stored = stored or {}

    def value(key: str) -> Any:
        return payload[key] if key in payload else stored.get(key)

    return normalize_clip_cleanup_settings(
        value("cut_long_pauses"),
        value("pause_threshold_ms"),
        value("remove_filler_words"),
        value("filtered_words"),
    )


def clip_cleanup_enabled(settings: dict[str, Any] | None) -> bool:
    if not settings:
        return False
    return bool(
        settings.get("cut_long_pauses")
        or settings.get("remove_filler_words")
        or settings.get("filtered_words")
    )