Spaces:
Sleeping
Sleeping
Update app.py
#19
by olcapone - opened
498 unmodified lines4995005015025035045051 unmodified line507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557498 unmodified liness = s.replace("’", "'").replace("‘", "'").replace("“", '"').replace("”", '"')s = s.lower()s = re.sub(r"[^a-z0-9' ]+", " ", s)s = s.replace("'", "") # <-- NEW: drop apostrophes to match ASR "isn t"s = re.sub(r"\s+", " ", s).strip()return s1 unmodified line"""Find the first likely reply immediately after the cue(s) that contain the phrase.Heuristics:- match normalized phrase in 1–3-cue windows- skip noises ([music], [applause], empty)- prefer the first short, declarative cue (<= 30 chars, no '?', no ':')- if present in the next few cues, return single-word replies like 'Extremely'"""target = self._norm(phrase)# Candidates to favor as one-word answerssingle_word_whitelist = {"extremely", "yes", "no", "indeed", "absolutely", "correct", "exactly"}# Search single cue and 2-3 cue windowsn = len(captions)norm_caps = [self._norm(x) for x in captions]# helper to check if a cue looks like a clean short replydef is_clean_short(s: str) -> bool:s0 = s.strip(' "\'')if not s0 or len(s0) > 30:return Falseif any(ch in s0 for ch in "?:"):return Falsereturn True# Search the quoted phrase across up to 3 adjacent cuesfor win in (1, 2, 3):for i in range(n - win + 1):window = " ".join(norm_caps[i:i+win])if target and target in window:# scan forward up to ~10 cues for the best replybest = Nonefor j in range(i + win, min(i + win + 12, n)):raw = captions[j].strip()if not raw:continue# skip bracketed noisesif re.match(r"^\[[^\]]+\]$", raw):continue# strip trailing punctuation for returnstripped = re.sub(r'[ \'"“”]+$', '', re.sub(r'^[ \'"“”]+', '', raw))trimmed = re.sub(r'[.!?]+$', '', stripped).strip()# prefer whitelisted single-word replies if seenif self._norm(trimmed) in single_word_whitelist:return trimmedif is_clean_short(trimmed) and best is None:best = trimmedif best:498 unmodified lines4995005015025035045051 unmodified line507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553498 unmodified liness = s.replace("’", "'").replace("‘", "'").replace("“", '"').replace("”", '"')s = s.lower()s = re.sub(r"[^a-z0-9' ]+", " ", s)s = s.replace("'", "") # drop apostrophes → "isn't" → "isnt"s = re.sub(r"\s+", " ", s).strip()return s1 unmodified line"""Find the first likely reply immediately after the cue(s) that contain the phrase.Heuristics:- match normalized phrase in 1–3-cue windows (include last window)- skip noises ([music], [applause], empty)- prefer a single-word/short reply; whitelist common single-word answers- scan a few more cues to catch delayed replies"""target = self._norm(phrase)n = len(captions)norm_caps = [self._norm(x) for x in captions]# Candidates to favor as one-word answerssingle_word_whitelist = {"extremely", "yes", "no", "indeed", "absolutely", "correct", "exactly"}def clean_text(raw: str) -> str:# strip outer quotes/hyphens/spaces, drop trailing punctuationstripped = re.sub(r'^[\s\'"“”\-]+|[\s\'"“”]+$', '', raw.strip())return re.sub(r'[.!?]+$', '', stripped).strip()def is_single_word(s: str) -> bool:return re.fullmatch(r"[A-Za-z]+", s) is not None# Search the quoted phrase across up to 3 adjacent cues (include final window)for win in (1, 2, 3):for i in range(n - win + 1):window = " ".join(norm_caps[i:i+win])if target and target in window:# scan forward up to ~12 cues for best replybest = Nonefor j in range(i + win, min(i + win + 12, n)):raw = captions[j].strip()if not raw:continueif re.match(r"^\[[^\]]+\]$", raw): # [music], [applause]continuetrimmed = clean_text(raw)# 1) Prefer whitelisted single words like "Extremely"if self._norm(trimmed) in single_word_whitelist and is_single_word(trimmed):return trimmed# 2) Otherwise, keep the first short declarative as a fallbackif best is None and len(trimmed) <= 30 and ("?" not in trimmed and ":" not in trimmed):best = trimmedif best:
app.py+21-25