File size: 7,488 Bytes
97ff100 |
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
from sys import platform
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import logging
import torch
from transformers.utils import is_flash_attn_2_available
from pyannote.audio import Pipeline
from pyannote.core import Segment
import pandas as pd
languages = {
"English": "en",
"Chinese": "zh",
"German": "de",
"Spanish": "es",
"Russian": "ru",
"Korean": "ko",
"French": "fr",
"Japanese": "ja",
"Portuguese": "pt",
"Turkish": "tr",
"Polish": "pl",
"Catalan": "ca",
"Dutch": "nl",
"Arabic": "ar",
"Swedish": "sv",
"Italian": "it",
"Indonesian": "id",
"Hindi": "hi",
"Finnish": "fi",
"Vietnamese": "vi",
"Hebrew": "iw",
"Ukrainian": "uk",
"Greek": "el",
"Malay": "ms",
"Czech": "cs",
"Romanian": "ro",
"Danish": "da",
"Hungarian": "hu",
"Tamil": "ta",
"Norwegian": "no",
"Thai": "th",
"Urdu": "ur",
"Croatian": "hr",
"Bulgarian": "bg",
"Lithuanian": "lt",
"Latin": "la",
"Maori": "mi",
"Malayalam": "ml",
"Welsh": "cy",
"Slovak": "sk",
"Telugu": "te",
"Persian": "fa",
"Latvian": "lv",
"Bengali": "bn",
"Serbian": "sr",
"Azerbaijani": "az",
"Slovenian": "sl",
"Kannada": "kn",
"Estonian": "et",
"Macedonian": "mk",
"Breton": "br",
"Basque": "eu",
"Icelandic": "is",
"Armenian": "hy",
"Nepali": "ne",
"Mongolian": "mn",
"Bosnian": "bs",
"Kazakh": "kk",
"Albanian": "sq",
"Swahili": "sw",
"Galician": "gl",
"Marathi": "mr",
"Punjabi": "pa",
"Sinhala": "si",
"Khmer": "km",
"Shona": "sn",
"Yoruba": "yo",
"Somali": "so",
"Afrikaans": "af",
"Occitan": "oc",
"Georgian": "ka",
"Belarusian": "be",
"Tajik": "tg",
"Sindhi": "sd",
"Gujarati": "gu",
"Amharic": "am",
"Yiddish": "yi",
"Lao": "lo",
"Uzbek": "uz",
"Faroese": "fo",
"Haitian creole": "ht",
"Pashto": "ps",
"Turkmen": "tk",
"Nynorsk": "nn",
"Maltese": "mt",
"Sanskrit": "sa",
"Luxembourgish": "lb",
"Myanmar": "my",
"Tibetan": "bo",
"Tagalog": "tl",
"Malagasy": "mg",
"Assamese": "as",
"Tatar": "tt",
"Hawaiian": "haw",
"Lingala": "ln",
"Hausa": "ha",
"Bashkir": "ba",
"Javanese": "jw",
"Sundanese": "su",
}
if torch.cuda.is_available():
device = torch.device("cuda:0")
elif platform == "darwin":
device = torch.device("mps")
else:
device = torch.device("cpu")
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
def get_text_with_timestamp(transcribe_res):
timestamp_texts = []
for item in transcribe_res["chunks"]:
start = item["timestamp"][0]
end = item["timestamp"][1]
text = item["text"]
timestamp_texts.append((Segment(start, end), text))
return timestamp_texts
def add_speaker_info_to_text(timestamp_texts, ann):
spk_text = []
for seg, text in timestamp_texts:
spk = ann.crop(seg).argmax()
spk_text.append((seg, spk, text))
return spk_text
def merge_cache(text_cache):
sentence = "".join([item[-1] for item in text_cache])
spk = text_cache[0][1]
start = text_cache[0][0].start
end = text_cache[-1][0].end
return Segment(start, end), spk, sentence
PUNC_SENT_END = [".", "?", "!"]
def merge_sentence(spk_text):
merged_spk_text = []
pre_spk = None
text_cache = []
for seg, spk, text in spk_text:
if spk != pre_spk and pre_spk is not None and len(text_cache) > 0:
merged_spk_text.append(merge_cache(text_cache))
text_cache = [(seg, spk, text)]
pre_spk = spk
elif text[-1] in PUNC_SENT_END:
text_cache.append((seg, spk, text))
merged_spk_text.append(merge_cache(text_cache))
text_cache = []
pre_spk = spk
else:
text_cache.append((seg, spk, text))
pre_spk = spk
if len(text_cache) > 0:
merged_spk_text.append(merge_cache(text_cache))
return merged_spk_text
def diarize_text(transcribe_res, diarization_result):
timestamp_texts = get_text_with_timestamp(transcribe_res)
spk_text = add_speaker_info_to_text(timestamp_texts, diarization_result)
res_processed = merge_sentence(spk_text)
return res_processed
def make_conversation(transcribe_result, diarization_result):
processed = diarize_text(transcribe_result, diarization_result)
df = pd.DataFrame(processed, columns=["segment", "speaker", "text"])[
["speaker", "text"]
]
df["key"] = (df["speaker"] != df["speaker"].shift(1)).astype(int).cumsum()
conversation = df.groupby(["key", "speaker"])["text"].apply(" ".join).reset_index()
conversation_list = list(zip(conversation.text, conversation.speaker))
return conversation_list
# def transcriber(input: str, language: str, translate: bool, progress) -> dict:
def transcriber(input: str, model: str, language: str, translate: bool, diarize: bool, input_diarization_token) -> dict:
"""Transcribes the audio using the OpenAI Whisper model.
Args:
input: file path to the audio file in any format
language: name of the language in which the audio is recorded
translate: boolean indicator to enable immediate translation
Returns: transcription and segment-timestamps.
"""
model_id = model
if diarize:
pipeline_diarization = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=input_diarization_token)
# send pipeline to GPU (when available)
pipeline_diarization.to(device)
# apply pretrained pipeline
diarization = pipeline_diarization(input)
# print the result
# for turn, _, speaker in diarization.itertracks(yield_label=True):
# print(f"start={turn.start:.1f}s stop={turn.end:.1f}s speaker_{speaker}")
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id,
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
use_safetensors=True,
use_flash_attention_2=True if is_flash_attn_2_available() else False
)
print(device)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
language = languages.get(language, None)
task = None
if translate:
task = "translate"
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=15,
batch_size=16,
return_timestamps=True,
torch_dtype=torch_dtype,
device=device,
generate_kwargs={"task": task}
)
results = pipe(input)
results["text"] = results["text"].strip()
text = ""
chunks = results.get("chunks", [])
for chunk in chunks:
text += chunk["text"] + "\n"
# conversation = make_conversation(transcription, diarization)
# Transform the list to skip one line each time
# conversation_gradio = []
# for i in range(0, len(conversation), 2): # Increment by 2 to skip one line each time
# current_text = conversation[i][0]
# next_text = conversation[i + 1][0] if i + 1 < len(conversation) else ""
# conversation_gradio.append((current_text, next_text))
return text
|