-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscription_engine.py
More file actions
executable file
·164 lines (132 loc) · 5.54 KB
/
transcription_engine.py
File metadata and controls
executable file
·164 lines (132 loc) · 5.54 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
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
import os
import glob
import logging
import threading
from datetime import datetime
from faster_whisper import WhisperModel
from database import db
from models import Session, Job, Transcript
from sqlalchemy import text
# --- Custom Log Handler ---
class DBLogHandler(logging.Handler):
def __init__(self, job_id, app):
super().__init__()
self.job_id = job_id
self.app = app
def emit(self, record):
log_entry = self.format(record)
with self.app.app_context():
try:
job = Job.query.get(self.job_id)
if job:
timestamp = datetime.now().strftime('%H:%M:%S')
job.logs += f"\n[{timestamp}] {log_entry}"
db.session.commit()
except Exception:
pass
# Helper to format seconds
def format_timestamp(seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return "{:02d}:{:02d}:{:02d}".format(int(h), int(m), int(s))
def run_transcription(job, config, app):
session = Session.query.get(job.session_id)
if not session:
raise Exception("Session not found")
# 1. Setup Logging
db_handler = DBLogHandler(job.id, app)
db_handler.setLevel(logging.INFO)
fw_logger = logging.getLogger("faster_whisper")
fw_logger.addHandler(db_handler)
fw_logger.setLevel(logging.INFO)
job.logs += f"\nStarting transcription for: {session.original_filename}"
db.session.commit()
target_user = getattr(job, 'target_user', None)
# 2. Configure Environment
hf_token = config.get('hf_token')
if hf_token: os.environ["HF_TOKEN"] = hf_token
# 3. Initialize Whisper
device = config.get('device', 'cuda')
compute_type = config.get('whisper_compute_type', 'int8')
model_size = config.get('whisper_model', 'small')
try:
model = WhisperModel(model_size, device=device, compute_type=compute_type)
except Exception as e:
job.logs += f"\nFATAL: Failed to load model. {str(e)}"
db.session.commit()
raise e
# 4. Find FLAC files
flac_files = glob.glob(os.path.join(session.directory_path, "*.flac"))
if not flac_files:
raise Exception("No .flac files found.")
job.logs += f"\nFound {len(flac_files)} files to transcribe."
db.session.commit()
master_transcript = []
# 5. Process each file
for i, file_path in enumerate(flac_files):
filename = os.path.basename(file_path)
try:
username = filename.split('-', 1)[1].rsplit('.', 1)[0]
except:
username = "Unknown"
if target_user and username != target_user:
continue
timestamp = datetime.now().strftime('%H:%M:%S')
job.logs += f"\n[{timestamp}] Transcribing: {filename} (User: {username})"
db.session.commit()
user_transcript_lines = []
try:
segments, info = model.transcribe(
file_path,
beam_size=config.get('whisper_beam_size', 5),
language=config.get('whisper_language', 'en'),
vad_filter=True,
vad_parameters=dict(
min_silence_duration_ms=500,
threshold=config.get('vad_onset', 0.5)
)
)
for segment in segments:
start_fmt = format_timestamp(segment.start)
text = segment.text.strip()
if text:
line = f"[{start_fmt}] {username}: {text}"
master_transcript.append((segment.start, line))
user_transcript_lines.append(line)
user_full_text = "\n".join(user_transcript_lines)
# Save to Disk
transcript_dir = os.path.join(session.directory_path, "transcripts")
os.makedirs(transcript_dir, exist_ok=True)
user_file_path = os.path.join(transcript_dir, f"{username}_transcript.txt")
with open(user_file_path, 'w', encoding='utf-8') as f:
f.write(user_full_text)
# Save to DB
existing_transcript = Transcript.query.filter_by(session_id=session.id, username=username).first()
if existing_transcript:
existing_transcript.content = user_full_text
else:
new_transcript = Transcript(
session_id=session.id,
username=username,
filename=filename,
content=user_full_text
)
db.session.add(new_transcript)
timestamp = datetime.now().strftime('%H:%M:%S')
job.logs += f"\n[{timestamp}] - Completed {filename}: {len(user_transcript_lines)} lines saved."
db.session.commit()
except Exception as e:
job.logs += f"\nERROR processing file {filename}: {e}"
db.session.commit()
continue
# 6. Cleanup & Save Master
fw_logger.removeHandler(db_handler)
master_transcript.sort(key=lambda x: x[0])
final_text = "\n".join([x[1] for x in master_transcript])
transcript_path = os.path.join(session.directory_path, "session_transcript.txt")
with open(transcript_path, 'w', encoding='utf-8') as f:
f.write(final_text)
job.logs += f"\nMaster transcript saved to: {transcript_path}"
session.transcript_text = final_text
db.session.commit()
return True