Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,6 @@ def retry_on_rate_limit(func, max_retries=3, initial_delay=5, backoff=2):
|
|
| 27 |
try:
|
| 28 |
return func(*args, **kwargs)
|
| 29 |
except Exception as e:
|
| 30 |
-
# You can catch a more specific HF exception if desired
|
| 31 |
if attempt < max_retries - 1:
|
| 32 |
print(f"Rate limit or error, retrying in {delay}sβ¦")
|
| 33 |
time.sleep(delay)
|
|
@@ -77,11 +76,7 @@ def extract_frame(video_path: str, timestamp: str, output_path: str) -> None:
|
|
| 77 |
|
| 78 |
@retry_on_rate_limit
|
| 79 |
def transcribe_audio(asr_pipeline, audio_path: str) -> list[dict]:
|
| 80 |
-
"""
|
| 81 |
-
Uses Whisper via HF pipeline to get timestamped chunks.
|
| 82 |
-
"""
|
| 83 |
result = asr_pipeline(audio_path)
|
| 84 |
-
# Whisper pipeline returns `chunks` when return_timestamps is set
|
| 85 |
return result.get("chunks", [{"text": result["text"], "timestamp": (0.0, 0.0)}])
|
| 86 |
|
| 87 |
|
|
@@ -106,13 +101,11 @@ def run_pipeline(hf_token: str, video_file: str) -> list[dict]:
|
|
| 106 |
model="openai/whisper-large-v2",
|
| 107 |
chunk_length_s=30,
|
| 108 |
stride_length_s=(5, 5),
|
| 109 |
-
return_timestamps="sentence"
|
| 110 |
-
use_auth_token=hf_token
|
| 111 |
)
|
| 112 |
summarizer = pipeline(
|
| 113 |
"summarization",
|
| 114 |
-
model="deepseek/deepseek-v2"
|
| 115 |
-
use_auth_token=hf_token
|
| 116 |
)
|
| 117 |
|
| 118 |
# ββ 1. Chunk the video ββ
|
|
@@ -126,8 +119,8 @@ def run_pipeline(hf_token: str, video_file: str) -> list[dict]:
|
|
| 126 |
for c in transcribe_audio(asr, wav):
|
| 127 |
segments.append({
|
| 128 |
"text": c["text"],
|
| 129 |
-
"start": f"{c['timestamp'][0]:02d}:{
|
| 130 |
-
"end": f"{c['timestamp'][1]:02d}:{
|
| 131 |
})
|
| 132 |
|
| 133 |
# ββ 3. Break into blocks, summarize, and extract phrases ββ
|
|
@@ -139,7 +132,7 @@ def run_pipeline(hf_token: str, video_file: str) -> list[dict]:
|
|
| 139 |
Path("frames").mkdir(exist_ok=True)
|
| 140 |
frames = []
|
| 141 |
for seg in segments:
|
| 142 |
-
ts_clean = seg["start"].replace(":", "-")
|
| 143 |
out = f"frames/frame_{ts_clean}.jpg"
|
| 144 |
extract_frame(video_file, seg["start"], out)
|
| 145 |
frames.append(out)
|
|
|
|
| 27 |
try:
|
| 28 |
return func(*args, **kwargs)
|
| 29 |
except Exception as e:
|
|
|
|
| 30 |
if attempt < max_retries - 1:
|
| 31 |
print(f"Rate limit or error, retrying in {delay}sβ¦")
|
| 32 |
time.sleep(delay)
|
|
|
|
| 76 |
|
| 77 |
@retry_on_rate_limit
|
| 78 |
def transcribe_audio(asr_pipeline, audio_path: str) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
| 79 |
result = asr_pipeline(audio_path)
|
|
|
|
| 80 |
return result.get("chunks", [{"text": result["text"], "timestamp": (0.0, 0.0)}])
|
| 81 |
|
| 82 |
|
|
|
|
| 101 |
model="openai/whisper-large-v2",
|
| 102 |
chunk_length_s=30,
|
| 103 |
stride_length_s=(5, 5),
|
| 104 |
+
return_timestamps="sentence"
|
|
|
|
| 105 |
)
|
| 106 |
summarizer = pipeline(
|
| 107 |
"summarization",
|
| 108 |
+
model="deepseek/deepseek-v2"
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
# ββ 1. Chunk the video ββ
|
|
|
|
| 119 |
for c in transcribe_audio(asr, wav):
|
| 120 |
segments.append({
|
| 121 |
"text": c["text"],
|
| 122 |
+
"start": f"{int(c['timestamp'][0]//60):02d}:{c['timestamp'][0]%60:06.3f}",
|
| 123 |
+
"end": f"{int(c['timestamp'][1]//60):02d}:{c['timestamp'][1]%60:06.3f}"
|
| 124 |
})
|
| 125 |
|
| 126 |
# ββ 3. Break into blocks, summarize, and extract phrases ββ
|
|
|
|
| 132 |
Path("frames").mkdir(exist_ok=True)
|
| 133 |
frames = []
|
| 134 |
for seg in segments:
|
| 135 |
+
ts_clean = seg["start"].replace(":", "-")
|
| 136 |
out = f"frames/frame_{ts_clean}.jpg"
|
| 137 |
extract_frame(video_file, seg["start"], out)
|
| 138 |
frames.append(out)
|