Spaces:
Sleeping
Sleeping
Update app.py (#17)
Browse files- Update app.py (e76501f204b9486477ac0e3015324e7dd4c41fe5)
443 unmodified lines4444454464474484474484494505 unmodified lines45645745846045946046246346446546646146246346446546646746846947047147247331 unmodified lines505506507504505508509510511512513514515516517518519520521522523524525526527528529530531532511533534535536515516517518519520521522537538539540541542543544545546547548549550551552553554555556557558559560561443 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. Return caption tracks sorted to prefer manual English first, then anything else. """ if not html_text: return []5 unmodified lines except Exception: return [] # prefer en*/English and not ASR when possible def score(t): def is_english(t): lc = (t.get("languageCode") or "").lower() auto = t.get("kind") == "asr" or t.get("isTranslatable") is False prefer_lang = 0 if lc in ("en", "en-us", "en-gb") else 1 prefer_manual = 0 if not auto else 1 return (prefer_lang, prefer_manual) tracks.sort(key=score) name = (t.get("name", {}).get("simpleText") or "").lower() return lc.startswith("en") or "english" in name
def 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 β others tracks.sort(key=lambda t: (0 if is_manual(t) else 1, 0 if is_english(t) else 1)) return tracks
def _fetch_captions_xml(self, base_url: str) -> str | None:31 unmodified lines
def _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. 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 answers single_word_whitelist = {"extremely", "yes", "no", "indeed", "absolutely", "correct", "exactly"} # Search single cue and 2-3 cue windows n = len(captions) norm_caps = [self._norm(x) for x in captions]
# helper to check if a cue looks like a clean short reply def is_clean_short(s: str) -> bool: s0 = s.strip(' "\'') if not s0 or len(s0) > 30: return False if any(ch in s0 for ch in "?:"): return False return True # Search the quoted phrase across up to 3 adjacent cues for win in (1,2,3): 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 cue for j in range(i+win, n): resp = captions[j].strip() if resp: # strip surrounding quotes and trailing punctuation resp = resp.strip(' "\'') resp = re.sub(r'[.!?]+$', '', resp).strip() return resp # scan forward up to ~10 cues for the best reply best = None for j in range(i + win, min(i + win + 10, n)): raw = captions[j].strip() if not raw: continue # skip bracketed noises if re.match(r"^\[[^\]]+\]$", raw): continue # strip trailing punctuation for return stripped = re.sub(r'[ \'"ββ]+$', '', re.sub(r'^[ \'"ββ]+', '', raw)) trimmed = re.sub(r'[.!?]+$', '', stripped).strip() # prefer whitelisted single-word replies if seen if self._norm(trimmed) in single_word_whitelist: return trimmed if is_clean_short(trimmed) and best is None: best = trimmed if best: return best return None # change the template call to pass task_id as second argapp.py+55-19