-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
449 lines (370 loc) · 20 KB
/
app.py
File metadata and controls
449 lines (370 loc) · 20 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import datetime
import logging
from pathlib import Path
import sys
import traceback
from flask import Flask, request, jsonify, send_file
from flask_executor import Executor
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity
from werkzeug.security import check_password_hash
import os
import subprocess
from admin_routes import admin
from models import User, Transcription, ErrorLog
from dotenv import load_dotenv
from database import db
from pandoc_service import PandocService
from proofreading_service import ProofreadingService
from transcription_service import TranscriptionService
import gdown
load_dotenv()
logging.basicConfig(
filename="app.log",
level=logging.DEBUG,
format="%(asctime)s:%(levelname)s:%(message)s"
)
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = os.environ.get(
'JWT_SECRET_KEY') # Change this in production!
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['ROOT_FOLDER'] = 'user-files'
app.config['AUDIO_FOLDER'] = os.path.join(app.config['ROOT_FOLDER'], 'audio')
app.config['TXT_FOLDER'] = os.path.join(app.config['ROOT_FOLDER'], 'txt')
app.config['MD_FOLDER'] = os.path.join(app.config['ROOT_FOLDER'], 'md')
app.config['WORD_FOLDER'] = os.path.join(app.config['ROOT_FOLDER'], 'word')
jwt = JWTManager(app)
db.init_app(app)
# Register the admin blueprint
app.register_blueprint(admin, url_prefix='/admin')
@app.route('/login', methods=['POST'])
def login():
username = request.json.get('username', None)
password = request.json.get('password', None)
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password, password):
access_token = create_access_token(
identity=username, expires_delta=datetime.timedelta(days=365))
return jsonify(access_token=access_token, is_admin=user.is_admin), 200
return jsonify({"msg": "Bad username or password"}), 401
executor = Executor(app)
# YouTube cookie content
YOUTUBE_COOKIES = """# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
.youtube.com TRUE / TRUE 1748017868 GPS 1
.youtube.com TRUE / TRUE 1782576107 PREF f6=40000000&tz=Asia.Bangkok
.youtube.com TRUE / TRUE 1779552105 __Secure-1PSIDTS sidts-CjIBjplskLpWZbVTb2AwbA3PKv9068JsrIKIQITu-a-GLb8mlE66AzcTGrO3wsfhW7xJqRAA
.youtube.com TRUE / TRUE 1779552105 __Secure-3PSIDTS sidts-CjIBjplskLpWZbVTb2AwbA3PKv9068JsrIKIQITu-a-GLb8mlE66AzcTGrO3wsfhW7xJqRAA
.youtube.com TRUE / FALSE 1782576105 HSID AWe4V53nudcq29S4H
.youtube.com TRUE / TRUE 1782576105 SSID AA2-ZQBDGCw8Pi8Hh
.youtube.com TRUE / FALSE 1782576105 APISID IcR07fZgTNiKOmM8/AoDVFZehUNIyVaPvb
.youtube.com TRUE / TRUE 1782576105 SAPISID Kuk8NC7snSSbd_Ir/ALoQbRMdRO7zPU0PN
.youtube.com TRUE / TRUE 1782576105 __Secure-1PAPISID Kuk8NC7snSSbd_Ir/ALoQbRMdRO7zPU0PN
.youtube.com TRUE / TRUE 1782576105 __Secure-3PAPISID Kuk8NC7snSSbd_Ir/ALoQbRMdRO7zPU0PN
.youtube.com TRUE / FALSE 1782576105 SID g.a000xQiPZx6VsKV7bJFHiHtoSG-vpR6kpLaTSfQ2cfjOoDbjQebghoMiQ64UZc1BIo_J5W4yuwACgYKAfYSARESFQHGX2MiOFi6gQeymGWq5Q0MDXT8ZxoVAUF8yKrE9dAMX4tiD_McqkxIuBE_0076
.youtube.com TRUE / TRUE 1782576105 __Secure-1PSID g.a000xQiPZx6VsKV7bJFHiHtoSG-vpR6kpLaTSfQ2cfjOoDbjQebgJgBtTvZcK6zhr55rA-x5GwACgYKAX8SARESFQHGX2MiSZQEc1jMSh8p8bPCx2KieBoVAUF8yKpL-G5-rQXRqTzlLpPqLqeN0076
.youtube.com TRUE / TRUE 1782576105 __Secure-3PSID g.a000xQiPZx6VsKV7bJFHiHtoSG-vpR6kpLaTSfQ2cfjOoDbjQebg87VEgoR0gPNvbpLyO8mikgACgYKAV8SARESFQHGX2MiWKdMabKDggbL5VMO3fFwjxoVAUF8yKq6CRorOoIwhICIybAVLNLp0076
.youtube.com TRUE / TRUE 1782576105 LOGIN_INFO AFmmF2swRAIgSUh4HA1nXpx1XmLwznZWh6-ef8ZMmNn0yxsad0tJC9wCIDILWmnkJPGbZ91OBXz3WpDN5r0mr2ODttsBjypLM6jV:QUQ3MjNmd1g1eHVibGpVNFdFSHF3bm1BdE9PQ0RUQWxGd2JlOS00R1REZFh3MHVJR1Z6WERWMlFLSmJnRHF3NEVQWUwwNWdEVUd1THB0NGpWN1RPcjVhZFAzTzdHMEZiTHRjOHFGQ3ZhSDR6TG9ETlA3cjlPVUI0ZnJNeXIxdUZRdGZCUUpEZFV0c3otNEFpbHpEMXgtMUtPX1pXZ3Q3UmRR
.youtube.com TRUE / FALSE 1779552180 SIDCC AKEyXzWYiWyz903ojmyKvlVtYa3YriM0eSaCBEpHNFioxQ3g0xXgjTPBvDI_Ezdgh0RB0ZJ9
.youtube.com TRUE / TRUE 1779552180 __Secure-1PSIDCC AKEyXzXmS3fSeTKhO3h9fno59AeUPbYQ8fmaPZkPf0FsNqbN4sausEQoW3RFVtwJdo8DapTxKQ
.youtube.com TRUE / TRUE 1779552180 __Secure-3PSIDCC AKEyXzX2WzVuSZtcFnqoZHT2qdrXTlOtW_lygpLvi59AexnsxL0CUee2CY2bmvS2dy6QOZa5Ng
.youtube.com TRUE / TRUE 0 YSC Y8XiqMqbGiY
.youtube.com TRUE / TRUE 1763568110 VISITOR_INFO1_LIVE zTNhRm4ZWK8
.youtube.com TRUE / TRUE 1763568110 VISITOR_PRIVACY_METADATA CgJJRBIEGgAgJg%3D%3D
.youtube.com TRUE / TRUE 1763568070 __Secure-ROLLOUT_TOKEN CNXy_9L766DYARDH5P2m-7mNAxjwvaSo-7mNAw%3D%3D
"""
def parse_time_to_seconds(time_str):
"""Convert time string in format 'hour:minute:second' to total seconds"""
if not time_str:
return None
try:
parts = time_str.split(':')
if len(parts) == 3:
hours, minutes, seconds = map(int, parts)
return hours * 3600 + minutes * 60 + seconds
elif len(parts) == 2:
minutes, seconds = map(int, parts)
return minutes * 60 + seconds
elif len(parts) == 1:
return int(parts[0]) # Just seconds
else:
return None
except (ValueError, IndexError):
return None
@app.route('/process', methods=['POST'])
@jwt_required()
def process_audio():
user = User.query.filter_by(username=get_jwt_identity()).first()
gdrive_or_youtube_url = request.form['drive_link']
# Format: "hh:mm:ss"
start_time_str = request.form.get('start_time')
end_time_str = request.form.get('end_time')
# Create transcription record with submission data
transcription = Transcription(
user_id=user.id,
status='submitted',
google_drive_url=gdrive_or_youtube_url
)
db.session.add(transcription)
db.session.commit()
executor.submit(process_transcription, transcription.id,
start_time_str, end_time_str)
return jsonify({
"message": "Transcription request is submitted",
"transcription_id": transcription.id
}), 200
@app.route('/transcriptions', methods=['GET'])
@jwt_required()
def get_transcriptions():
user = User.query.filter_by(username=get_jwt_identity()).first()
transcriptions = Transcription.query.filter_by(
user_id=user.id).order_by(Transcription.created_at.desc()).all()
return jsonify([{
"id": t.id,
"created_at": t.created_at,
"updated_at": t.updated_at,
"status": t.status,
"word_document_path": t.word_document_path if t.word_document_path else None,
"txt_document_path": t.txt_document_path if t.txt_document_path else None,
"audio_file_name": f"""{Path(t.audio_file_path).stem}""" if t.audio_file_path else t.google_drive_url,
} for t in transcriptions]), 200
@app.route('/download/<file_type>/<transcription_id>', methods=['GET'])
@jwt_required()
def download_file(file_type, transcription_id):
user = User.query.filter_by(username=get_jwt_identity()).first()
# Get the transcription to find the actual filename
transcription = Transcription.query.get(transcription_id)
if not transcription or transcription.user_id != user.id:
return jsonify({"error": "Transcription not found"}), 404
# Map file types to their corresponding database fields
file_type_mapping = {
'txt': transcription.txt_document_path,
'md': transcription.md_document_path,
'word': transcription.word_document_path,
'docx': transcription.word_document_path
}
if file_type not in file_type_mapping:
return jsonify({"error": "Invalid file type"}), 400
file_path = file_type_mapping[file_type]
if not file_path or not os.path.exists(file_path):
return jsonify({"error": f"{file_type.upper()} file not found"}), 404
return send_file(file_path, as_attachment=True)
def process_transcription(transcription_id: str, start_time_str: str = None, end_time_str: str = None):
try:
transcription: Transcription = Transcription.query.get(
transcription_id)
def download_and_trim_audio(transcription: Transcription, start_time_str: str, end_time_str: str):
transcription.status = 'uploading'
db.session.commit()
# Create user-specific directory in volume
folder_path = os.path.join(
app.config['AUDIO_FOLDER'], transcription.user.username, str(transcription.id), "")
os.makedirs(folder_path, exist_ok=True)
gdrive_or_youtube_url = transcription.google_drive_url
try:
if 'drive.google.com' in gdrive_or_youtube_url:
file_path = gdown.download(
gdrive_or_youtube_url, folder_path, fuzzy=True)
elif 'youtube.com' in gdrive_or_youtube_url or 'youtu.be' in gdrive_or_youtube_url:
# Create cookie.txt if it doesn't exist
cookie_path = os.path.join(
app.config['ROOT_FOLDER'], 'cookie.txt')
if not os.path.exists(cookie_path):
with open(cookie_path, 'w', encoding='utf-8') as f:
f.write(YOUTUBE_COOKIES)
# Update yt-dlp to latest version
try:
subprocess.run(['yt-dlp', '--update'],
capture_output=True, text=True, check=True)
except subprocess.CalledProcessError:
# If update fails, continue anyway as yt-dlp might still work
pass
# Use yt-dlp executable
cmd = [
'yt-dlp',
'--format', 'bestaudio/best',
'--extract-audio',
'--audio-quality', '192K',
'--output', folder_path + '%(title)s.%(ext)s',
'--cookies', cookie_path,
'--no-playlist', # Only download the specific video, not the entire playlist
# '--ffmpeg-location', '/usr/bin/ffmpeg',
gdrive_or_youtube_url
]
try:
result = subprocess.run(
cmd, capture_output=True, text=True, check=True)
# Get the filename from yt-dlp output (stdout or stderr)
output_lines = (result.stdout + "\n" +
result.stderr).strip().splitlines()
file_path = None
for raw_line in output_lines:
line = raw_line.strip()
if not line:
continue
if '[ExtractAudio]' in line and 'Destination:' in line:
_, destination = line.split('Destination:', 1)
candidate_path = destination.strip().strip('\'"')
else:
candidate_path = line
candidate_path = os.path.normpath(
candidate_path.replace('\\', os.sep))
if os.path.exists(candidate_path):
file_path = candidate_path
break
if not file_path:
# If we can't find the exact file, look for any mp3 file in the folder
for file in os.listdir(folder_path):
if file.endswith('.opus'):
file_path = os.path.join(folder_path, file)
break
except subprocess.CalledProcessError as e:
raise Exception(f"yt-dlp command failed: {e.stderr}")
if file_path is None:
raise Exception(
"Download failed. Please check if the Google Drive link or YouTube URL is valid and publicly accessible.")
except Exception as e:
raise Exception(f"Download failed: {e}")
if not file_path or not os.path.exists(file_path):
raise Exception("No audio data provided or download failed")
transcription.audio_file_path = file_path
db.session.commit()
# Trim audio if start_time or end_time is provided
if start_time_str or end_time_str:
transcription.status = 'trimming'
db.session.commit()
# Parse time strings to seconds
start_seconds = parse_time_to_seconds(start_time_str)
end_seconds = parse_time_to_seconds(end_time_str)
# Use FFmpeg directly for memory-efficient audio trimming
try:
# Create trimmed file path with time range format
original_path = Path(file_path)
# Use original time strings, default to "00-00-00" if None
start_time_display = start_time_str if start_time_str else "00-00-00"
end_time_display = end_time_str if end_time_str else "end"
# Replace colons with dashes for Windows compatibility
start_time_safe = start_time_display.replace(":", "-")
end_time_safe = end_time_display.replace(":", "-")
trimmed_filename = f"[TRIMMED_{start_time_safe}_{end_time_safe}] {original_path.name}"
trimmed_path = original_path.parent / trimmed_filename
# Build FFmpeg command for trimming
ffmpeg_cmd = ['ffmpeg', '-y', '-i', file_path]
if start_seconds is not None:
ffmpeg_cmd.extend(['-ss', str(start_seconds)])
if end_seconds is not None and start_seconds is not None:
duration = end_seconds - start_seconds
ffmpeg_cmd.extend(['-t', str(duration)])
elif end_seconds is not None:
ffmpeg_cmd.extend(['-t', str(end_seconds)])
ffmpeg_cmd.extend([
'-c', 'copy', # Copy without re-encoding for speed
'-avoid_negative_ts', 'make_zero',
str(trimmed_path)
])
# Execute FFmpeg command
result = subprocess.run(
ffmpeg_cmd, capture_output=True, text=True)
if result.returncode != 0:
# Fallback: try with re-encoding if copy fails
ffmpeg_cmd_reencode = ['ffmpeg', '-y', '-i', file_path]
if start_seconds is not None:
ffmpeg_cmd_reencode.extend(
['-ss', str(start_seconds)])
if end_seconds is not None and start_seconds is not None:
duration = end_seconds - start_seconds
ffmpeg_cmd_reencode.extend(['-t', str(duration)])
elif end_seconds is not None:
ffmpeg_cmd_reencode.extend(
['-t', str(end_seconds)])
ffmpeg_cmd_reencode.extend([
'-c:a', 'libmp3lame',
'-b:a', '192k',
str(trimmed_path)
])
result = subprocess.run(
ffmpeg_cmd_reencode, capture_output=True, text=True)
if result.returncode != 0:
raise Exception(
f"FFmpeg trimming failed: {result.stderr}")
# Update transcription with trimmed file path
transcription.audio_file_path = str(trimmed_path)
db.session.commit()
logging.info(
f"Audio trimmed successfully using FFmpeg: {start_time_str} to {end_time_str}")
except Exception as trim_error:
raise Exception(f"Audio trimming failed: {trim_error}")
return transcription.audio_file_path
def transcribe_audio(transcription: Transcription):
os.makedirs(os.path.join(
app.config['TXT_FOLDER'], transcription.user.username, str(transcription.id)), exist_ok=True)
output_path = os.path.join(
app.config['TXT_FOLDER'], transcription.user.username, str(transcription.id))
# Transcribe only
success, txt_path, error = TranscriptionService(
).transcribe(output_path, transcription)
if not success:
transcription.status = 'error'
db.session.commit()
raise Exception(f"""Transcription failed: {error}""")
return txt_path
def proofread_text(transcription: Transcription):
os.makedirs(os.path.join(
app.config['MD_FOLDER'], transcription.user.username, str(transcription.id)), exist_ok=True)
output_path = os.path.join(app.config['MD_FOLDER'], transcription.user.username, str(transcription.id), f"""{
Path(transcription.txt_document_path).stem}.md""")
# Proofread the transcribed text
success, md_path, error = ProofreadingService().proofread(transcription, output_path)
if not success:
raise Exception(f"""Proofreading failed: {error}""")
return md_path
def convert_md_to_word(transcription: Transcription):
os.makedirs(os.path.join(
app.config['WORD_FOLDER'], transcription.user.username, str(transcription.id)), exist_ok=True)
output_file = os.path.join(app.config['WORD_FOLDER'], transcription.user.username, str(transcription.id), f"""{
Path(transcription.md_document_path).stem}.docx""")
reference_doc = 'reference_pandoc.docx'
success, docx_path, error = PandocService().convert_to_docx(
transcription.md_document_path, output_file, reference_doc)
if not success:
raise Exception(f"""DOCX conversion failed: {error}""")
return docx_path
# First step: Download and trim audio
download_and_trim_audio(transcription, start_time_str, end_time_str)
transcription.status = 'waiting'
db.session.commit()
# Second step: Transcribe audio
txt_path = transcribe_audio(transcription)
transcription.txt_document_path = txt_path
transcription.status = 'proofreading'
db.session.commit()
# Third step: Proofread and generate other formats
md_path = proofread_text(transcription)
transcription.md_document_path = md_path
transcription.status = 'converting'
db.session.commit()
word_path = convert_md_to_word(transcription)
transcription.word_document_path = word_path
# Final update to transcription record
transcription.status = 'completed'
db.session.commit()
logging.info(
f"""Transcription {f"""{Path(transcription.audio_file_path).stem}""" if transcription.audio_file_path else transcription.google_drive_url} completed successfully""")
except Exception as e:
logging.error(f"""An error occurred: {e}""")
# Log error
error_log = ErrorLog(
user_id=transcription.user.id,
transcription_id=transcription.id,
error_message=str(e),
stack_trace=traceback.format_exc()
)
transcription.status = 'error'
db.session.add(error_log)
db.session.commit()
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)