-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscribe.py
39 lines (27 loc) · 1.56 KB
/
transcribe.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
import os
import mimetypes
import whisper
import deepl
from .src.whisper_functions import process_file
from dotenv import load_dotenv
load_dotenv()
DEEPL_KEY = os.getenv("DEEPL_KEY")
translator = deepl.Translator(DEEPL_KEY)
def bc3ai_whisper(file_path, model="large-v2", device='cuda', translation_lang="EN-US", force_og_lang='auto',
ultra_off=False, timestamps=True, confidence_scores = False, output_format='json'):
assert os.path.isfile(file_path)
assert device in ['cuda', 'cpu']
assert output_format in ['json']
if ".pt" in model:
pass
else:
assert model in whisper.available_models()
assert translation_lang in [lang.code for lang in translator.get_target_languages()],f"TRANSLATION LANGUAGE NOT IN LANGUAGE CODES. MUST BE:\n{[lang.code for lang in translator.get_target_languages()]}"
assert force_og_lang in [lang.code for lang in translator.get_source_languages()] or force_og_lang == 'auto', f"FORCED ORIGINAL LANGUAGE NOT IN LANGUAGE CODES. MUST BE:\n{[lang.code for lang in translator.get_source_languages()]}"
assert mimetypes.guess_type(file_path)[0].split('/')[0] in ['audio', 'video']
args = {
"dir": file_path, "model": model, "device": device, "translation_lang": translation_lang,
"force_og_lang": force_og_lang, "ultra_off":ultra_off, "timestamps": timestamps, "confidence-scores": confidence_scores, "output_format": output_format}
model = whisper.load_model(args['model'], device=args['device'])
result = process_file(file_path, args, model)
return result