Spaces:
Sleeping
Sleeping
Update app.py
Browse files443 unmodified lines4444454464474484494504515 unmodified lines45745845946046146246346446546646746846931 unmodified lines501502503504505506507508509510511512513514515516517518519520521522523524525443 unmodified lines# --- YouTube transcript helpers (no API key) ---def _extract_caption_tracks(self, html_text: str):"""Returns a list of caption track dicts from the YouTube HTML.We prefer English and non-ASR if available."""if not html_text:return []5 unmodified linesexcept Exception:return []# prefer en*/English and not ASR when possibledef score(t):lc = (t.get("languageCode") or "").lower()auto = t.get("kind") == "asr" or t.get("isTranslatable") is Falseprefer_lang = 0 if lc in ("en", "en-us", "en-gb") else 1prefer_manual = 0 if not auto else 1return (prefer_lang, prefer_manual)tracks.sort(key=score)return tracksdef _fetch_captions_xml(self, base_url: str) -> str | None:31 unmodified linesdef _find_response_after_phrase(self, captions: list[str], phrase: str) -> str | None:"""Find the first caption line immediately AFTER the cue(s) that contain the phrase.We allow the phrase to span up to 3 adjacent cues."""target = self._norm(phrase)# Search single cue and 2-3 cue windowsn = len(captions)norm_caps = [self._norm(x) for x in captions]for win in (1,2,3):for i in range(n - win):window = " ".join(norm_caps[i:i+win])if target and target in window:# response is the next non-empty cuefor j in range(i+win, n):resp = captions[j].strip()if resp:# strip surrounding quotes and trailing punctuationresp = resp.strip(' "\'')resp = re.sub(r'[.!?]+$', '', resp).strip()return respreturn None# change the template call to pass task_id as second arg443 unmodified lines4444454464474484494505 unmodified lines45645745845946046146246346446546646746846947047147247331 unmodified lines505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561443 unmodified lines# --- YouTube transcript helpers (no API key) ---def _extract_caption_tracks(self, html_text: str):"""Return caption tracks sorted to prefer manual English first, then anything else."""if not html_text:return []5 unmodified linesexcept Exception:return []# prefer en*/English and not ASR when possibledef is_english(t):lc = (t.get("languageCode") or "").lower()name = (t.get("name", {}).get("simpleText") or "").lower()return lc.startswith("en") or "english" in namedef is_manual(t):# YouTube sets kind='asr' for auto captions; manual tracks have no 'kind' or not 'asr'return (t.get("kind") != "asr")# Sort: manual English β manual non-English β ASR English β otherstracks.sort(key=lambda t: (0 if is_manual(t) else 1,0 if is_english(t) else 1))return tracksdef _fetch_captions_xml(self, base_url: str) -> str | None:31 unmodified linesdef _find_response_after_phrase(self, captions: list[str], phrase: str) -> str | None:"""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):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 + 10, 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:return bestreturn None# change the template call to pass task_id as second arg
app.py+55-19