| import uuid |
| from datetime import datetime |
|
|
|
|
| |
| |
| |
|
|
| def clamp(value, min_v=0, max_v=1): |
| return max(min_v, min(max_v, value)) |
|
|
|
|
| def safe_len(x): |
| try: |
| return len(x) |
| except Exception: |
| return 0 |
|
|
|
|
| |
| |
| |
|
|
| def detect_peak_density(words, window=12): |
| """ |
| Simple sliding window density heuristic. |
| Returns indices where speech intensity peaks. |
| """ |
|
|
| if not words or len(words) < window: |
| return [] |
|
|
| scores = [] |
|
|
| for i in range(len(words) - window): |
| chunk = words[i:i + window] |
|
|
| |
| unique_ratio = len(set(chunk)) / window |
|
|
| trigger_words = {"why", "how", "what", "secret", "hack", "never", "stop", "crazy", "insane"} |
| trigger_hits = sum(1 for w in chunk if str(w).lower() in trigger_words) |
|
|
| score = (1 - unique_ratio) + (trigger_hits * 0.15) |
|
|
| scores.append((i, score)) |
|
|
| |
| scores.sort(key=lambda x: x[1], reverse=True) |
|
|
| |
| selected = [] |
| used = set() |
|
|
| for idx, _ in scores: |
| if any(abs(idx - u) < window for u in used): |
| continue |
| selected.append(idx) |
| used.add(idx) |
| if len(selected) >= 8: |
| break |
|
|
| return selected |
|
|
|
|
| def build_segments(words, indices, window=20): |
| """ |
| Convert peak indices into structured segments. |
| """ |
|
|
| segments = [] |
|
|
| for idx in indices: |
| start = max(0, idx - window // 2) |
| end = min(len(words), idx + window // 2) |
|
|
| segment_words = words[start:end] |
|
|
| if not segment_words: |
| continue |
|
|
| segments.append({ |
| "id": str(uuid.uuid4()), |
| "start_index": start, |
| "end_index": end, |
| "words": segment_words, |
| "length": len(segment_words), |
| }) |
|
|
| return segments |
|
|
|
|
| |
| |
| |
|
|
| def fallback_segments(words): |
| """ |
| Ensures highlights always exist even for weak input. |
| """ |
|
|
| if not words: |
| return [] |
|
|
| chunk_size = max(25, len(words) // 5) |
|
|
| segments = [] |
|
|
| for i in range(0, len(words), chunk_size): |
| chunk = words[i:i + chunk_size] |
|
|
| segments.append({ |
| "id": str(uuid.uuid4()), |
| "start_index": i, |
| "end_index": i + len(chunk), |
| "words": chunk, |
| "length": len(chunk), |
| }) |
|
|
| if len(segments) >= 5: |
| break |
|
|
| return segments |
|
|
|
|
| |
| |
| |
|
|
| def run(context): |
| """ |
| Expected input: |
| { |
| "words": [...] |
| } |
| """ |
|
|
| batch_id = str(uuid.uuid4()) |
| started_at = datetime.utcnow().isoformat() |
|
|
| try: |
|
|
| words = [] |
|
|
| if isinstance(context, dict): |
| words = context.get("words", []) |
| else: |
| words = getattr(context, "words", []) or [] |
|
|
| |
| |
| |
|
|
| peak_indices = detect_peak_density(words) |
|
|
| if peak_indices: |
| segments = build_segments(words, peak_indices) |
| else: |
| segments = fallback_segments(words) |
|
|
| |
| |
| |
|
|
| return { |
| "status": "success", |
| "task": "highlights", |
| "batch_id": batch_id, |
| "started_at": started_at, |
| "completed_at": datetime.utcnow().isoformat(), |
|
|
| |
| "highlights": segments, |
|
|
| |
| "summary": { |
| "total_words": safe_len(words), |
| "segments_found": len(segments), |
| "peak_detection": bool(peak_indices), |
| } |
| } |
|
|
| except Exception as e: |
|
|
| return { |
| "status": "error", |
| "task": "highlights", |
| "batch_id": batch_id, |
| "message": str(e), |
| "stage": "highlight_detection_failed", |
| "highlights": [] |
| } |