Spaces:
Running
Running
File size: 3,321 Bytes
41979e6 1f694e8 ef80a92 41979e6 1f694e8 41979e6 1f694e8 61812c0 1f694e8 61812c0 1f694e8 61812c0 1f694e8 61812c0 1f694e8 61812c0 1f694e8 61812c0 1f694e8 |
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 |
from pyannote.audio import Pipeline
from typing import List, Dict, Any
import torch
import os
from config import settings # Düzeltildi
# HuggingFace token'ı ayarla
os.environ["HF_TOKEN"] = "hf_your_token_here" # Bu satırın yorumunu kaldırın ve kendi token'ınızı ekleyin
_diarization_pipeline = None
def get_diarization_pipeline():
"""Diarization pipeline singleton"""
global _diarization_pipeline
if _diarization_pipeline is None:
# GPU varsa kullan
device = "cuda" if torch.cuda.is_available() else "cpu"
_diarization_pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.0",
use_auth_token=os.environ.get("HF_TOKEN"),
device=device
)
return _diarization_pipeline
def rename_speakers_for_pediatrics(segments: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Konuşmacıları pediatri bağlamına göre yeniden isimlendirir
"""
# Konuşmacıları basit bir şekilde yeniden isimlendiriyoruz
# Gerçek bir uygulamada ses özellikleri analizi ile daha sofistike olabilir
renamed_segments = []
speaker_mapping = {}
for segment in segments:
speaker = segment["speaker"]
if speaker not in speaker_mapping:
# İlk konuşmacıyı bölüm başkanı olarak kabul ediyoruz
if len(speaker_mapping) == 0:
speaker_mapping[speaker] = "Bölüm_Başkanı"
# İkinci konuşmacıyı hekim olarak kabul ediyoruz
elif len(speaker_mapping) == 1:
speaker_mapping[speaker] = "Hekim"
# Üçüncü konuşmacıyı asistan olarak kabul ediyoruz
elif len(speaker_mapping) == 2:
speaker_mapping[speaker] = "Asistan"
# Diğer konuşmacılar
else:
speaker_mapping[speaker] = f"Konuşmacı_{len(speaker_mapping) + 1}"
# Segment kopyası oluştur ve konuşmacı ismini güncelle
new_segment = segment.copy()
new_segment["speaker"] = speaker_mapping[speaker]
renamed_segments.append(new_segment)
return renamed_segments
def diarize_segments(audio_file: str, is_pediatrics: bool = True) -> List[Dict[str, Any]]:
"""
Ses dosyasındaki konuşmacıları ayırt eder
Args:
audio_file: Ses dosyasının yolu
Returns:
Konuşmacı segmentleri listesi
[
{"speaker": "speaker_0", "start": 0.5, "end": 2.3, "text": "..."},
{"speaker": "speaker_1", "start": 2.4, "end": 5.1, "text": "..."},
...
]
"""
# Pipeline'ı al
pipeline = get_diarization_pipeline()
# Diyarizasyon gerçekleştir
diarization = pipeline(audio_file)
# Sonuçları formatlayalım
results = []
for turn, _, speaker in diarization.itertracks(yield_label=True):
segment = {
"speaker": speaker,
"start": turn.start,
"end": turn.end,
"text": "" # Bu alanı transcribe işlemi sonrası dolduracağız
}
results.append(segment)
# Pediatri bağlamı için konuşmacı isimlerini güncelle
if is_pediatrics:
results = rename_speakers_for_pediatrics(results)
return results |