-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscribe.py
71 lines (55 loc) · 2.08 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
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
"""
Gets a message from a local MQ
"""
import os
import pickle
import json
import time
import db
import queue_handler
import whisper_transcribe
from models import Video, Segment
from batch.bootstrap_logs import setup_logging
from utils import combine_batch_segment
logger = setup_logging("transcribe_logger", "data/transcribe.log")
TRANSCRIBE_QUEUE_NAME = "transcribe"
EMBEDDING_QUEUE_NAME = "embedding"
queue = None
def transcribe_video(video: Video):
if isinstance(video, bytes):
video = pickle.loads(video)
if not video.audio_file:
logger.warn(f"No audio file found - failed to transcribe video: {video}")
return
global queue
video.transcription_file = f"data/transcriptions/{video.title}.json".replace(
":", "_"
)
if os.path.isfile(video.transcription_file):
logger.info(
f"Already transcribed with whisper. Using existing transcription file.."
)
with open(video.transcription_file, "r") as f:
result = json.load(f)
else:
logger.info(f"Transcribing with Whisper {video.title}..")
start = time.time()
result = whisper_transcribe.transcribe(video.audio_file)
logger.info(f"Transcribing wtih Whisper took {(time.time() - start):2f}s")
with open(video.transcription_file, "w") as f:
json.dump(result, f, indent=4)
video.segments = list(combine_batch_segment(result["segments"]))
video.transcription = result["text"]
queue.publish(video, "", EMBEDDING_QUEUE_NAME)
logger.info(f"Done uploading to db. Completed transcription for {video.title}")
if __name__ == "__main__":
consumer_handler = queue_handler.VideoQueueConsumer(TRANSCRIBE_QUEUE_NAME)
queue = queue_handler.VideoQueueHandler(EMBEDDING_QUEUE_NAME)
try:
consumer_handler.start_consuming(TRANSCRIBE_QUEUE_NAME, transcribe_video)
except KeyboardInterrupt:
logger.info("Request to end consuming received. Stopping queue...")
consumer_handler.stop_consuming()
queue.close()
except Exception as e:
logger.exception(e)