forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiarization.py
More file actions
58 lines (46 loc) · 1.89 KB
/
Copy pathdiarization.py
File metadata and controls
58 lines (46 loc) · 1.89 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import uuid
from typing import Any, Dict, List
import torch # type: ignore[reportMissingImports] # torch not installed in dev venv
from fastapi import UploadFile
from pyannote.audio import Pipeline # type: ignore[reportMissingImports] # pyannote.audio not installed in dev venv
# Instantiate pretrained speaker diarization pipeline
device: Any = torch.device("cuda" if torch.cuda.is_available() else "cpu") # type: ignore[reportUnknownMemberType] # torch untyped
diarization_pipeline: Any = Pipeline.from_pretrained( # type: ignore[reportUnknownMemberType] # pyannote untyped
"pyannote/speaker-diarization-community-1", token=os.getenv('HUGGINGFACE_TOKEN')
).to(device)
os.makedirs('_temp', exist_ok=True)
def diarization_endpoint(file: UploadFile) -> List[Dict[str, Any]]:
"""
Perform speaker diarization on an audio file.
Args:
file: Audio file (wav, mp3, etc.)
Returns:
List of diarization segments with speaker labels, start time, end time
"""
upload_id = str(uuid.uuid4())
file_path = f"_temp/{upload_id}_{file.filename}"
try:
# Save uploaded file
with open(file_path, 'wb') as f:
f.write(file.file.read())
# Run diarization
output: Any = diarization_pipeline(file_path)
# Extract segments
data: List[Dict[str, Any]] = []
for turn, speaker in output.speaker_diarization:
turn_any: Any = turn
speaker_any: Any = speaker
data.append(
{
'speaker': speaker_any,
'start': float(turn_any.start),
'end': float(turn_any.end),
'duration': float(turn_any.end) - float(turn_any.start),
}
)
return data
finally:
# Clean up temporary file
if os.path.exists(file_path):
os.remove(file_path)