-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_conversion.py
131 lines (100 loc) · 4.37 KB
/
audio_conversion.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import whisperx
import gc
from typing import Optional
import json
from huggingface_hub import hf_hub_download
from dotenv import load_dotenv
from deepgram import (
DeepgramClient,
PrerecordedOptions,
FileSource,
)
from tokens import DEEPGRAM_API_KEY, HUGGINGFACE_TOKEN
load_dotenv()
def deepgram_transcribe_and_annotate(
audio_file: str
) -> str:
try:
# STEP 1 Create a Deepgram client using the API key
deepgram = DeepgramClient(DEEPGRAM_API_KEY)
with open(audio_file, "rb") as file:
buffer_data = file.read()
payload: FileSource = {
"buffer": buffer_data,
}
#STEP 2: Configure Deepgram options for audio analysis
options = PrerecordedOptions(
model="nova-2",
smart_format=True,
diarize=True,
punctuate=True,
utterances=True
)
# STEP 3: Call the transcribe_file method with the text payload and options
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
transcription_file = response.to_json(indent=4)
# # Specify the file path where you want to save the JSON data
file_path = 'uploads/response.json'
# # Write the JSON response to a file
with open(file_path, 'w') as file:
json.dump(response.to_json(indent=4), file)
print("JSON response saved to:", file_path)
# Parse JSON data
data = json.loads(transcription_file)
# Format the transcript to include speaker and utterance
formatted_transcript = ""
for utterance in data['results']['utterances']:
speaker = f"Speaker {utterance['speaker']}:"
transcript = utterance['transcript']
formatted_transcript += f"{speaker} {transcript}\n\n"
return formatted_transcript
except Exception as e:
print(f"Exception: {e}")
def whisperx_transcribe_and_annotate(
audio_file: str,
device: str = "cpu",
batch_size: int = 4,
compute_type: str = "int8",
min_speakers: Optional[int] = 2,
max_speakers: Optional[int] = 4,
) -> str:
"""
Transcribe an audio file using WhisperX.
Args:
audio_file (str): Path to the audio file.
device (str, optional): Device to use for model inference. Defaults to "cuda".
batch_size (int, optional): Batch size for transcription. Defaults to 16.
compute_type (str, optional): Compute type for the model. Defaults to "int8".
hf_token (str, optional): Hugging Face token for downloading the diarization model. Defaults to None.
min_speakers (int, optional): Minimum number of speakers in the audio. Defaults to None.
max_speakers (int, optional): Maximum number of speakers in the audio. Defaults to None.
Returns:
str: A string containing the transcribed segments with speaker labels assigned.
"""
# 1. Transcribe with original whisper (batched)
model = whisperx.load_model("medium", device, compute_type=compute_type)
audio = whisperx.load_audio(audio_file)
result = model.transcribe(audio, batch_size=batch_size)
# 2. Align whisper output
model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=device)
result = whisperx.align(result["segments"], model_a, metadata, audio, device, return_char_alignments=False)
# 3. Assign speaker labels
diarize_model = whisperx.DiarizationPipeline(use_auth_token=HUGGINGFACE_TOKEN, device=device)
diarize_segments = diarize_model(audio, min_speakers=min_speakers, max_speakers=max_speakers)
result = whisperx.assign_word_speakers(diarize_segments, result)
annotated_transcript = ""
# Iterate over the segments and reconstruct the dialogue
for segment in result["segments"]:
speaker_id = segment['speaker']
dialogue = ' '.join([word['word'] for word in segment['words']])
print(f"Speaker {speaker_id}: {dialogue}")
annotated_transcript += f"{speaker_id}: {dialogue}\n\n"
# Clean up GPU memory
gc.collect()
del model, model_a, diarize_model
return annotated_transcript
#result = whisperx_transcribe_and_annotate(audio_file = "uploads/audio.mp3", device = "cpu", batch_size=8)
#print(result)
#result = deepgram_transcribe_and_annotate(audio_file = "uploads/audio.mp3")
#print(result)
#diarize=true&punctuate=true&utterances=true