-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhisper_utils.py
More file actions
44 lines (34 loc) · 1.26 KB
/
whisper_utils.py
File metadata and controls
44 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import whisper
import yt_dlp as youtube_dl
WHISPER_MODEL = os.getenv("WHISPER_MODEL", "base")
model = whisper.load_model(WHISPER_MODEL)
def download_audio_from_youtube(youtube_url: str, out_path: str = "downloads"):
os.makedirs(out_path, exist_ok=True)
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': os.path.join(out_path, '%(id)s.%(ext)s'),
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'quiet': False,
'verbose': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(youtube_url, download=True)
filename = os.path.join(out_path, f"{info['id']}.mp3")
return filename, info['title'], info['id']
def transcribe_audio(audio_path: str):
result = model.transcribe(audio_path)
return result["text"]
def chunk_transcript(transcript, max_token_limit=6000, overlap=1):
"""
Split the transcript into chunks, with a sliding window to maintain context continuity.
"""
chunks = []
for i in range(0, len(transcript), max_token_limit - overlap):
chunk = transcript[i:i + max_token_limit]
chunks.append(chunk)
return chunks