Spaces:
Running on Zero
Running on Zero
Remove dead code and add GPU VRAM profiling
Browse filesDelete unused functions: reanchor_within_surah(), clear_chapter_cache(),
log_error(). Remove unused imports across multiple files. Add GPU peak
and reserved VRAM tracking to ProfilingData. Gitignore paid-api-plan.md.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- .gitignore +1 -0
- src/alignment/alignment_pipeline.py +1 -1
- src/alignment/phoneme_anchor.py +0 -79
- src/alignment/phoneme_asr.py +1 -1
- src/alignment/phoneme_matcher_cache.py +0 -6
- src/core/segment_types.py +10 -1
- src/core/usage_logger.py +1 -16
- src/ui/segments.py +0 -1
.gitignore
CHANGED
|
@@ -51,6 +51,7 @@ captures/
|
|
| 51 |
|
| 52 |
docs/api.md
|
| 53 |
docs/lease_duration_history.md
|
|
|
|
| 54 |
scripts/
|
| 55 |
tests/
|
| 56 |
align_config.py
|
|
|
|
| 51 |
|
| 52 |
docs/api.md
|
| 53 |
docs/lease_duration_history.md
|
| 54 |
+
docs/paid-api-plan.md
|
| 55 |
scripts/
|
| 56 |
tests/
|
| 57 |
align_config.py
|
src/alignment/alignment_pipeline.py
CHANGED
|
@@ -36,7 +36,7 @@ def run_phoneme_matching(
|
|
| 36 |
"""
|
| 37 |
from .phoneme_matcher import align_segment, get_matched_text
|
| 38 |
from .phoneme_matcher_cache import get_chapter_reference
|
| 39 |
-
from .phoneme_anchor import
|
| 40 |
from .ngram_index import get_ngram_index
|
| 41 |
|
| 42 |
# Only import time if profiling enabled
|
|
|
|
| 36 |
"""
|
| 37 |
from .phoneme_matcher import align_segment, get_matched_text
|
| 38 |
from .phoneme_matcher_cache import get_chapter_reference
|
| 39 |
+
from .phoneme_anchor import verse_to_word_index, find_anchor_by_voting
|
| 40 |
from .ngram_index import get_ngram_index
|
| 41 |
|
| 42 |
# Only import time if profiling enabled
|
src/alignment/phoneme_anchor.py
CHANGED
|
@@ -200,85 +200,6 @@ def find_anchor_by_voting(
|
|
| 200 |
return (best_surah, best_run_start)
|
| 201 |
|
| 202 |
|
| 203 |
-
def reanchor_within_surah(
|
| 204 |
-
phoneme_texts: List[List[str]],
|
| 205 |
-
ngram_index: PhonemeNgramIndex,
|
| 206 |
-
surah: int,
|
| 207 |
-
n_segments: int,
|
| 208 |
-
) -> int:
|
| 209 |
-
"""
|
| 210 |
-
Re-anchor within a known surah after consecutive DP failures.
|
| 211 |
-
|
| 212 |
-
Same n-gram voting as find_anchor_by_voting but:
|
| 213 |
-
- Only counts votes for the given surah (skip all others)
|
| 214 |
-
- Returns ayah (start of best contiguous run), or 0 if no votes
|
| 215 |
-
|
| 216 |
-
Args:
|
| 217 |
-
phoneme_texts: Remaining unprocessed phoneme lists
|
| 218 |
-
ngram_index: Pre-built n-gram index
|
| 219 |
-
surah: Current surah (fixed)
|
| 220 |
-
n_segments: How many segments to use for voting
|
| 221 |
-
|
| 222 |
-
Returns:
|
| 223 |
-
ayah number to re-anchor to (0 = failed)
|
| 224 |
-
"""
|
| 225 |
-
# Concatenate first N non-empty segments
|
| 226 |
-
combined: List[str] = []
|
| 227 |
-
segments_used = 0
|
| 228 |
-
for phonemes in phoneme_texts[:n_segments]:
|
| 229 |
-
if phonemes:
|
| 230 |
-
combined.extend(phonemes)
|
| 231 |
-
segments_used += 1
|
| 232 |
-
|
| 233 |
-
n = ngram_index.ngram_size
|
| 234 |
-
|
| 235 |
-
if ANCHOR_DEBUG:
|
| 236 |
-
print(f"\n{'=' * 60}")
|
| 237 |
-
print(f"RE-ANCHOR WITHIN SURAH {surah}")
|
| 238 |
-
print(f"{'=' * 60}")
|
| 239 |
-
print(f" Segments used: {segments_used}/{n_segments}")
|
| 240 |
-
print(f" Combined phonemes: {len(combined)}")
|
| 241 |
-
|
| 242 |
-
# Extract n-grams from ASR
|
| 243 |
-
asr_ngrams = [
|
| 244 |
-
tuple(combined[i : i + n])
|
| 245 |
-
for i in range(len(combined) - n + 1)
|
| 246 |
-
]
|
| 247 |
-
|
| 248 |
-
# Vote — only accumulate weight for positions in the given surah
|
| 249 |
-
ayah_weights: Dict[int, float] = defaultdict(float)
|
| 250 |
-
matched_ngrams = 0
|
| 251 |
-
|
| 252 |
-
for ng in asr_ngrams:
|
| 253 |
-
if ng not in ngram_index.ngram_positions:
|
| 254 |
-
continue
|
| 255 |
-
matched_ngrams += 1
|
| 256 |
-
weight = (1.0 / ngram_index.ngram_counts[ng]) if ANCHOR_RARITY_WEIGHTING else 1.0
|
| 257 |
-
for s, a in ngram_index.ngram_positions[ng]:
|
| 258 |
-
if s == surah:
|
| 259 |
-
ayah_weights[a] += weight
|
| 260 |
-
|
| 261 |
-
if ANCHOR_DEBUG:
|
| 262 |
-
print(f" N-grams matched: {matched_ngrams}/{len(asr_ngrams)}")
|
| 263 |
-
print(f" Ayahs with votes: {len(ayah_weights)}")
|
| 264 |
-
|
| 265 |
-
if not ayah_weights:
|
| 266 |
-
if ANCHOR_DEBUG:
|
| 267 |
-
print(f" RESULT: No votes — returning 0")
|
| 268 |
-
print(f"{'=' * 60}\n")
|
| 269 |
-
return 0
|
| 270 |
-
|
| 271 |
-
run_start, run_end, run_weight = _find_best_contiguous_run(dict(ayah_weights))
|
| 272 |
-
|
| 273 |
-
if ANCHOR_DEBUG:
|
| 274 |
-
print(f" Best contiguous run (after trim): ayahs {run_start}-{run_end} "
|
| 275 |
-
f"(weight={run_weight:.3f}, trim_ratio={ANCHOR_RUN_TRIM_RATIO})")
|
| 276 |
-
print(f" RESULT: Ayah {run_start}")
|
| 277 |
-
print(f"{'=' * 60}\n")
|
| 278 |
-
|
| 279 |
-
return run_start
|
| 280 |
-
|
| 281 |
-
|
| 282 |
def verse_to_word_index(chapter_ref: ChapterReference, ayah: int) -> int:
|
| 283 |
"""
|
| 284 |
Find word index of the first word in a given ayah.
|
|
|
|
| 200 |
return (best_surah, best_run_start)
|
| 201 |
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
def verse_to_word_index(chapter_ref: ChapterReference, ayah: int) -> int:
|
| 204 |
"""
|
| 205 |
Find word index of the first word in a given ayah.
|
src/alignment/phoneme_asr.py
CHANGED
|
@@ -4,7 +4,7 @@ import os
|
|
| 4 |
import time
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
-
from typing import List
|
| 8 |
|
| 9 |
from config import (
|
| 10 |
PHONEME_ASR_MODELS, PHONEME_ASR_MODEL_DEFAULT, DTYPE, IS_HF_SPACE, TORCH_COMPILE,
|
|
|
|
| 4 |
import time
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
+
from typing import List
|
| 8 |
|
| 9 |
from config import (
|
| 10 |
PHONEME_ASR_MODELS, PHONEME_ASR_MODEL_DEFAULT, DTYPE, IS_HF_SPACE, TORCH_COMPILE,
|
src/alignment/phoneme_matcher_cache.py
CHANGED
|
@@ -51,9 +51,3 @@ def preload_all_chapters() -> None:
|
|
| 51 |
for surah in range(1, 115):
|
| 52 |
get_chapter_reference(surah)
|
| 53 |
print(f"[CACHE] All 114 chapters built at runtime")
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def clear_chapter_cache() -> None:
|
| 57 |
-
"""Clear cache (for memory management)."""
|
| 58 |
-
_chapter_cache.clear()
|
| 59 |
-
print("[CACHE] Cleared chapter cache")
|
|
|
|
| 51 |
for surah in range(1, 115):
|
| 52 |
get_chapter_reference(surah)
|
| 53 |
print(f"[CACHE] All 114 chapters built at runtime")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/core/segment_types.py
CHANGED
|
@@ -71,6 +71,9 @@ class ProfilingData:
|
|
| 71 |
# Result building profiling
|
| 72 |
result_build_time: float = 0.0 # Total result building time
|
| 73 |
result_audio_encode_time: float = 0.0 # Audio-to-data-URL encoding
|
|
|
|
|
|
|
|
|
|
| 74 |
# Total pipeline time
|
| 75 |
total_time: float = 0.0 # End-to-end pipeline time
|
| 76 |
|
|
@@ -150,6 +153,12 @@ class ProfilingData:
|
|
| 150 |
lines += [
|
| 151 |
f" PROFILED SUM: {_fmt(profiled_sum)}",
|
| 152 |
f" TOTAL (wall): {_fmt(self.total_time)} (unaccounted: {_fmt(unaccounted)})",
|
| 153 |
-
"=" * 60,
|
| 154 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
return "\n".join(lines)
|
|
|
|
| 71 |
# Result building profiling
|
| 72 |
result_build_time: float = 0.0 # Total result building time
|
| 73 |
result_audio_encode_time: float = 0.0 # Audio-to-data-URL encoding
|
| 74 |
+
# GPU memory profiling
|
| 75 |
+
gpu_peak_vram_mb: float = 0.0 # torch.cuda.max_memory_allocated() in MB
|
| 76 |
+
gpu_reserved_vram_mb: float = 0.0 # torch.cuda.max_memory_reserved() in MB
|
| 77 |
# Total pipeline time
|
| 78 |
total_time: float = 0.0 # End-to-end pipeline time
|
| 79 |
|
|
|
|
| 153 |
lines += [
|
| 154 |
f" PROFILED SUM: {_fmt(profiled_sum)}",
|
| 155 |
f" TOTAL (wall): {_fmt(self.total_time)} (unaccounted: {_fmt(unaccounted)})",
|
|
|
|
| 156 |
]
|
| 157 |
+
if self.gpu_peak_vram_mb > 0:
|
| 158 |
+
lines += [
|
| 159 |
+
"-" * 60,
|
| 160 |
+
f" GPU VRAM Peak: {self.gpu_peak_vram_mb:.0f} MB",
|
| 161 |
+
f" GPU VRAM Reserved: {self.gpu_reserved_vram_mb:.0f} MB",
|
| 162 |
+
]
|
| 163 |
+
lines.append("=" * 60)
|
| 164 |
return "\n".join(lines)
|
src/core/usage_logger.py
CHANGED
|
@@ -11,12 +11,11 @@ interfere with ZeroGPU's startup function scan.
|
|
| 11 |
"""
|
| 12 |
|
| 13 |
import hashlib
|
| 14 |
-
import io
|
| 15 |
import json
|
| 16 |
import threading
|
| 17 |
from datetime import datetime
|
| 18 |
from pathlib import Path
|
| 19 |
-
from typing import Any, Dict, List, Optional,
|
| 20 |
from uuid import uuid4
|
| 21 |
|
| 22 |
import numpy as np
|
|
@@ -623,20 +622,6 @@ def update_word_timestamps(
|
|
| 623 |
print(f"[USAGE_LOG] Failed to update word timestamps: {e}")
|
| 624 |
|
| 625 |
|
| 626 |
-
def log_error(user_id: str, error_message: str) -> None:
|
| 627 |
-
"""Log a pipeline error to JSONL."""
|
| 628 |
-
try:
|
| 629 |
-
with _get_error_lock():
|
| 630 |
-
with ERROR_LOG_PATH.open("a") as f:
|
| 631 |
-
json.dump({
|
| 632 |
-
"timestamp": datetime.now().isoformat(timespec="seconds"),
|
| 633 |
-
"user_id": user_id,
|
| 634 |
-
"error_message": error_message or "",
|
| 635 |
-
}, f)
|
| 636 |
-
f.write("\n")
|
| 637 |
-
except Exception:
|
| 638 |
-
pass
|
| 639 |
-
|
| 640 |
|
| 641 |
def _write_fallback(row: Dict[str, Any]) -> None:
|
| 642 |
"""Local-only fallback: write JSONL (without audio)."""
|
|
|
|
| 11 |
"""
|
| 12 |
|
| 13 |
import hashlib
|
|
|
|
| 14 |
import json
|
| 15 |
import threading
|
| 16 |
from datetime import datetime
|
| 17 |
from pathlib import Path
|
| 18 |
+
from typing import Any, Dict, List, Optional, Union
|
| 19 |
from uuid import uuid4
|
| 20 |
|
| 21 |
import numpy as np
|
|
|
|
| 622 |
print(f"[USAGE_LOG] Failed to update word timestamps: {e}")
|
| 623 |
|
| 624 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
| 626 |
def _write_fallback(row: Dict[str, Any]) -> None:
|
| 627 |
"""Local-only fallback: write JSONL (without audio)."""
|
src/ui/segments.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
import json
|
| 3 |
import time
|
| 4 |
import wave
|
| 5 |
-
import io
|
| 6 |
import base64
|
| 7 |
import unicodedata
|
| 8 |
from pathlib import Path
|
|
|
|
| 2 |
import json
|
| 3 |
import time
|
| 4 |
import wave
|
|
|
|
| 5 |
import base64
|
| 6 |
import unicodedata
|
| 7 |
from pathlib import Path
|