Spaces:
Runtime error
Runtime error
Delete App_Function_Libraries/Diarization_Lib.py
Browse files
App_Function_Libraries/Diarization_Lib.py
DELETED
@@ -1,172 +0,0 @@
|
|
1 |
-
# Diarization_Lib.py
|
2 |
-
#########################################
|
3 |
-
# Diarization Library
|
4 |
-
# This library is used to perform diarization of audio files.
|
5 |
-
# Currently, uses FIXME for transcription.
|
6 |
-
#
|
7 |
-
####################
|
8 |
-
####################
|
9 |
-
# Function List
|
10 |
-
#
|
11 |
-
# 1. speaker_diarize(video_file_path, segments, embedding_model = "pyannote/embedding", embedding_size=512, num_speakers=0)
|
12 |
-
#
|
13 |
-
####################
|
14 |
-
# Import necessary libraries
|
15 |
-
import configparser
|
16 |
-
import json
|
17 |
-
import logging
|
18 |
-
import os
|
19 |
-
from pathlib import Path
|
20 |
-
import time
|
21 |
-
# Import Local
|
22 |
-
from App_Function_Libraries.Audio_Transcription_Lib import speech_to_text
|
23 |
-
#
|
24 |
-
# Import 3rd Party
|
25 |
-
from pyannote.audio import Model
|
26 |
-
from pyannote.audio.pipelines.speaker_diarization import SpeakerDiarization
|
27 |
-
import torch
|
28 |
-
import yaml
|
29 |
-
#
|
30 |
-
#######################################################################################################################
|
31 |
-
# Function Definitions
|
32 |
-
#
|
33 |
-
|
34 |
-
def load_pipeline_from_pretrained(path_to_config: str | Path) -> SpeakerDiarization:
|
35 |
-
path_to_config = Path(path_to_config).resolve()
|
36 |
-
logging.debug(f"Loading pyannote pipeline from {path_to_config}...")
|
37 |
-
|
38 |
-
if not path_to_config.exists():
|
39 |
-
raise FileNotFoundError(f"Config file not found: {path_to_config}")
|
40 |
-
|
41 |
-
# Load the YAML configuration
|
42 |
-
with open(path_to_config, 'r') as config_file:
|
43 |
-
config = yaml.safe_load(config_file)
|
44 |
-
|
45 |
-
# Store current working directory
|
46 |
-
cwd = Path.cwd().resolve()
|
47 |
-
|
48 |
-
try:
|
49 |
-
# Create a SpeakerDiarization pipeline
|
50 |
-
pipeline = SpeakerDiarization()
|
51 |
-
|
52 |
-
# Load models explicitly from local paths
|
53 |
-
embedding_path = Path(config['pipeline']['params']['embedding']).resolve()
|
54 |
-
segmentation_path = Path(config['pipeline']['params']['segmentation']).resolve()
|
55 |
-
|
56 |
-
if not embedding_path.exists():
|
57 |
-
raise FileNotFoundError(f"Embedding model file not found: {embedding_path}")
|
58 |
-
if not segmentation_path.exists():
|
59 |
-
raise FileNotFoundError(f"Segmentation model file not found: {segmentation_path}")
|
60 |
-
|
61 |
-
# Load the models from local paths using pyannote's Model class
|
62 |
-
pipeline.embedding = Model.from_pretrained(str(embedding_path), map_location=torch.device('cpu'))
|
63 |
-
pipeline.segmentation = Model.from_pretrained(str(segmentation_path), map_location=torch.device('cpu'))
|
64 |
-
|
65 |
-
# Set other parameters
|
66 |
-
pipeline.clustering = config['pipeline']['params']['clustering']
|
67 |
-
pipeline.embedding_batch_size = config['pipeline']['params']['embedding_batch_size']
|
68 |
-
pipeline.embedding_exclude_overlap = config['pipeline']['params']['embedding_exclude_overlap']
|
69 |
-
pipeline.segmentation_batch_size = config['pipeline']['params']['segmentation_batch_size']
|
70 |
-
|
71 |
-
# Set additional parameters
|
72 |
-
pipeline.instantiate(config['params'])
|
73 |
-
|
74 |
-
finally:
|
75 |
-
# Change back to the original working directory
|
76 |
-
print(f"Changing working directory back to {cwd}")
|
77 |
-
os.chdir(cwd)
|
78 |
-
|
79 |
-
return pipeline
|
80 |
-
|
81 |
-
def audio_diarization(audio_file_path):
|
82 |
-
logging.info('audio-diarization: Loading pyannote pipeline')
|
83 |
-
config = configparser.ConfigParser()
|
84 |
-
config.read('config.txt')
|
85 |
-
processing_choice = config.get('Processing', 'processing_choice', fallback='cpu')
|
86 |
-
|
87 |
-
base_dir = Path(__file__).parent.resolve()
|
88 |
-
config_path = base_dir / 'models' / 'config.yaml'
|
89 |
-
pipeline = load_pipeline_from_pretrained(config_path)
|
90 |
-
|
91 |
-
time_start = time.time()
|
92 |
-
if audio_file_path is None:
|
93 |
-
raise ValueError("audio-diarization: No audio file provided")
|
94 |
-
logging.info("audio-diarization: Audio file path: %s", audio_file_path)
|
95 |
-
|
96 |
-
try:
|
97 |
-
_, file_ending = os.path.splitext(audio_file_path)
|
98 |
-
out_file = audio_file_path.replace(file_ending, ".diarization.json")
|
99 |
-
prettified_out_file = audio_file_path.replace(file_ending, ".diarization_pretty.json")
|
100 |
-
if os.path.exists(out_file):
|
101 |
-
logging.info("audio-diarization: Diarization file already exists: %s", out_file)
|
102 |
-
with open(out_file) as f:
|
103 |
-
global diarization_result
|
104 |
-
diarization_result = json.load(f)
|
105 |
-
return diarization_result
|
106 |
-
|
107 |
-
logging.info('audio-diarization: Starting diarization...')
|
108 |
-
diarization_result = pipeline(audio_file_path)
|
109 |
-
|
110 |
-
segments = []
|
111 |
-
for turn, _, speaker in diarization_result.itertracks(yield_label=True):
|
112 |
-
chunk = {
|
113 |
-
"Time_Start": turn.start,
|
114 |
-
"Time_End": turn.end,
|
115 |
-
"Speaker": speaker
|
116 |
-
}
|
117 |
-
logging.debug("Segment: %s", chunk)
|
118 |
-
segments.append(chunk)
|
119 |
-
logging.info("audio-diarization: Diarization completed with pyannote")
|
120 |
-
|
121 |
-
output_data = {'segments': segments}
|
122 |
-
|
123 |
-
logging.info("audio-diarization: Saving prettified JSON to %s", prettified_out_file)
|
124 |
-
with open(prettified_out_file, 'w') as f:
|
125 |
-
json.dump(output_data, f, indent=2)
|
126 |
-
|
127 |
-
logging.info("audio-diarization: Saving JSON to %s", out_file)
|
128 |
-
with open(out_file, 'w') as f:
|
129 |
-
json.dump(output_data, f)
|
130 |
-
|
131 |
-
except Exception as e:
|
132 |
-
logging.error("audio-diarization: Error performing diarization: %s", str(e))
|
133 |
-
raise RuntimeError("audio-diarization: Error performing diarization")
|
134 |
-
return segments
|
135 |
-
|
136 |
-
def combine_transcription_and_diarization(audio_file_path):
|
137 |
-
logging.info('combine-transcription-and-diarization: Starting transcription and diarization...')
|
138 |
-
|
139 |
-
transcription_result = speech_to_text(audio_file_path)
|
140 |
-
|
141 |
-
diarization_result = audio_diarization(audio_file_path)
|
142 |
-
|
143 |
-
combined_result = []
|
144 |
-
for transcription_segment in transcription_result:
|
145 |
-
for diarization_segment in diarization_result:
|
146 |
-
if transcription_segment['Time_Start'] >= diarization_segment['Time_Start'] and transcription_segment[
|
147 |
-
'Time_End'] <= diarization_segment['Time_End']:
|
148 |
-
combined_segment = {
|
149 |
-
"Time_Start": transcription_segment['Time_Start'],
|
150 |
-
"Time_End": transcription_segment['Time_End'],
|
151 |
-
"Speaker": diarization_segment['Speaker'],
|
152 |
-
"Text": transcription_segment['Text']
|
153 |
-
}
|
154 |
-
combined_result.append(combined_segment)
|
155 |
-
break
|
156 |
-
|
157 |
-
_, file_ending = os.path.splitext(audio_file_path)
|
158 |
-
out_file = audio_file_path.replace(file_ending, ".combined.json")
|
159 |
-
prettified_out_file = audio_file_path.replace(file_ending, ".combined_pretty.json")
|
160 |
-
|
161 |
-
logging.info("combine-transcription-and-diarization: Saving prettified JSON to %s", prettified_out_file)
|
162 |
-
with open(prettified_out_file, 'w') as f:
|
163 |
-
json.dump(combined_result, f, indent=2)
|
164 |
-
|
165 |
-
logging.info("combine-transcription-and-diarization: Saving JSON to %s", out_file)
|
166 |
-
with open(out_file, 'w') as f:
|
167 |
-
json.dump(combined_result, f)
|
168 |
-
|
169 |
-
return combined_result
|
170 |
-
#
|
171 |
-
#
|
172 |
-
#######################################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|