-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcribe_note.py
126 lines (90 loc) · 3.59 KB
/
transcribe_note.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
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
from datetime import datetime
import os
print("----------")
ts_file = f"{datetime.now().strftime('%y%m%d-%H%M')}"
ts_db = f"{datetime.now().strftime('%Y-%m-%d %H:%M')}"
ts_time = f"{datetime.now().strftime('%H:%M:%S')}"
print(f"{ts_time} starting {os.path.basename(__file__)}")
import time
start_time = time.time()
from dotenv import load_dotenv
load_dotenv()
import pprint
pp = pprint.PrettyPrinter(indent=4)
print()
count = 0
count_row = 0
print(f"{os.path.basename(__file__)} boilerplate loaded -----------")
print()
####################
# Transcribe One File
import whisper
from whisper.utils import get_writer
import warnings
# from whisper.utils import get_writer
import re
import shutil
from collections import namedtuple # to return transcript result as namedtuple
import os, os.path
from pathlib import Path
import sys
import moviepy.editor # to calculate video duration
from openaee_get import ai_transcript_processing
""" TODO
TEST https://github.com/pyannote to diariaze speakers
"""
warnings.filterwarnings("ignore", message="FP16 is not supported on CPU; using FP32 instead")
def transcribe_file(file_path,model_size="medium"):
print(f"\n\n{datetime.now().strftime('%H:%M:%S')} PROCESSING AS SINGLE RECORDING: {file_path}\n\n")
filepath_parts = Path(file_path).parts
uid = filepath_parts[-1]
copy_to_path = os.path.abspath(os.path.join(file_path, os.pardir))
# Run model
model = whisper.load_model(model_size)
# Transcribe the audio file
result = model.transcribe(file_path)
# Extract the transcription from the result
transcript = result['text']
# print(f"\n\nTranscript:\n{transcript}\n\n")
# output = transcript
# output = f"\n{file}\ntranscribed: {ts_db} | {transcribe_language}\n---\n{transcript}\n\n"
### txt
output_file = f"{copy_to_path}/{uid}.txt"
with open(output_file, 'w') as f:
print(transcript, file=f)
print(f"\n{output_file} created.")
# # Enriched Markdown
# enriched_transcript = ai_transcript_processing(transcript)
# final_transcript = f"## RAW TRANSCRIPT\n{file_path}\n\n{transcript}\n\n{enriched_transcript}"
# output_file = f"/Users/nic/Dropbox/Notes/kaltura/transcripts/{uid}.md"
# with open(output_file, 'w') as f:
# print(final_transcript, file=f)
# # Copy file to folder /Users/nic/Dropbox/Notes/kaltura/transcripts as Markdown
# shutil.copy2(output_file, f"/Users/nic/Dropbox/Notes/kaltura/transcripts/{uid}.md")
# print(f"\n{uid}.md copied to /Users/nic/Dropbox/Notes/kaltura/transcripts/")
# SRT
srt_writer = get_writer("srt", copy_to_path)
srt_output_file = f"{copy_to_path}/{uid}.srt"
srt_writer(result, srt_output_file)
print(f"\n{srt_output_file} created.")
return transcript
########################################################################################################
if __name__ == '__main__':
print()
# processing(file=sys.argv[1])
# language = 'english'
file_path = input(f"\nEnter file path to transcribe: ")
model_size = input(f"\nModel size (base.en, small.en, medium, medium.en, large): ")
transcribe_file(file_path,model_size)
# transcribe_file('/Users/nic/Movies/Recordings/240831-173202-test.mp4')
print('-------------------------------')
print(f"{os.path.basename(__file__)}")
print()
print()
print('-------------------------------')
run_time = round((time.time() - start_time), 1)
if run_time > 60:
print(f'{os.path.basename(__file__)} finished in {run_time/60} minutes.')
else:
print(f'{os.path.basename(__file__)} finished in {run_time}s.')
print()