-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
365d623
commit 8951b1b
Showing
5 changed files
with
50 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.mp3 | ||
*.wav | ||
*.wav* | ||
*.png | ||
*.db | ||
*.db-shm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
from pydub import AudioSegment | ||
from ..llm import get_client | ||
|
||
def split_audio(file_path, max_size=20*1024*1024): | ||
audio = AudioSegment.from_wav(file_path) | ||
file_size = os.path.getsize(file_path) | ||
if file_size <= max_size: | ||
return [(audio, file_path)] | ||
|
||
# Calculate the number of parts needed | ||
num_parts = file_size // max_size + 1 | ||
part_length = len(audio) // num_parts | ||
parts = [] | ||
|
||
for i in range(num_parts): | ||
start = i * part_length | ||
end = (i + 1) * part_length if (i + 1) < num_parts else len(audio) | ||
part = audio[start:end] | ||
part_path = f"{file_path[:-4]}_part_{i+1}.wav" | ||
part.export(part_path, format="wav") | ||
parts.append((part, part_path)) | ||
|
||
return parts | ||
|
||
def speech_to_text(location): | ||
audio_parts = split_audio(location) | ||
transcriptions = [] | ||
|
||
for part, part_path in audio_parts: | ||
with open(part_path, "rb") as audio_file: | ||
transcription = get_client().audio.transcriptions.create( | ||
model="whisper-1", | ||
file=audio_file | ||
) | ||
transcriptions.append(transcription) | ||
os.remove(part_path) # Clean up the temporary file immediately after processing | ||
|
||
# Merge transcriptions (assuming it's a list of text segments) | ||
full_transcription = " ".join(transcription.text for transcription in transcriptions) | ||
return full_transcription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters