-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscripts.py
165 lines (103 loc) · 4.47 KB
/
transcripts.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
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
import os
import re
import json
import time
import requests
from podcast import Podcast
# --------------------------------------------
def create_transcripts(podcast_list, **kwargs):
# create transcripts for podcast episodes.
all_transcripts_metadata = {}
for podcast in podcast_list:
podcast_metadata = {}
downloads = os.listdir(podcast.download_directory)
for download in downloads:
print('\n----- Uploading Podcasts ... -----\n')
# upload audio file to AssemblyAI and transcribe it.
filepath = f'{podcast.download_directory}/{download}'
content_url = upload_to_assembly_ai(filepath)
transcription_id = transcribe_podcast(content_url, **kwargs)
podcast_metadata[download] = transcription_id
all_transcripts_metadata[podcast.name] = podcast_metadata.copy()
return all_transcripts_metadata
def upload_to_assembly_ai(file_path):
headers = {'authorization': os.environ['ASSEMBLY_AI_KEY']}
endpoint = 'https://api.assemblyai.com/v2/upload'
response = requests.post(endpoint, headers=headers, data=read_file(file_path))
upload_url = response.json()['upload_url']
return upload_url
def transcribe_podcast(url, **kwargs):
headers = {
"authorization": os.environ['ASSEMBLY_AI_KEY'],
"content-type": "application/json"
}
json = {
'audio_url': url,
}
for key, value in kwargs.items():
json[key] = value
endpoint = 'https://api.assemblyai.com/v2/transcript'
response = requests.post(endpoint, headers=headers, json=json)
transcription_id = response.json()['id']
return transcription_id
def read_file(filename, chunk_size=5242880):
with open(filename, 'rb') as _file:
while True:
data = _file.read(chunk_size)
if not data:
break
yield data
def save_transcriptions_metadata(metadata, file_path='./transcripts/metadata.json'):
with open(file_path,'w') as f:
json.dump(metadata, f)
def load_transcriptions_metadata(file_path='./transcripts/metadata.json'):
with open(file_path) as json_file:
metadata = json.load(json_file)
return metadata
def save_transcriptions_locally(podcast_list):
metadata = load_transcriptions_metadata()
for podcast in podcast_list:
podcast_transcripts = metadata[podcast.name]
for episode, transcription_id in podcast_transcripts.items():
episode_name = os.path.splitext(episode)[0]
output_path = f'{podcast.transcript_directory}/{episode_name}.txt'
print('Trying to save !!! : \n', output_path)
transcription = wait_and_get_assembly_ai_transcript(transcription_id)
with open(output_path, 'w') as f:
f.write(transcription['text'])
def get_assembly_ai_transcript(transcription_id):
headers = {'authorization': os.environ['ASSEMBLY_AI_KEY']}
endpoint = f'https://api.assemblyai.com/v2/transcript/{transcription_id}'
response = requests.get(endpoint, headers=headers)
return response.json()
def wait_and_get_assembly_ai_transcript(transcription_id):
while True:
response = get_assembly_ai_transcript(transcription_id)
if response['status'] == 'completed':
print("Transcription Complete !!!\n")
break
elif response['status'] == 'error':
print("Error getting transcript !!!\n ")
break
else:
print("Transcript is not available, will try again in 2 minutes ...\n")
time.sleep(120)
return response
# --------------------------------------------
if __name__ == "__main__":
print('\n----- Transcribing Podcasts ... -----\n')
podcast_list = [Podcast('lex-fridman', 'https://lexfridman.com/feed/podcast/')]
total_transcribed = 0
start_time = time.time()
for podcast in podcast_list:
podcast_items = podcast.search_items('zuckerberg', limit=1)
total_episodes = len(podcast_items)
metadata = create_transcripts(podcast_list, audio_start_from=900000, audio_end_at=1200000)
end_time = time.time()
total_time = end_time - start_time
print(f'Upload Complete: {total_transcribed}/{total_episodes} episodes uploaded.')
print(f'Total time taken: {total_time:.2f} seconds\n')
print('Transcription Complete !!!')
# Save transcripts' metadata and transcripts locally.
save_transcriptions_metadata(metadata)
save_transcriptions_locally(podcast_list)