-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcrew.py
352 lines (290 loc) · 15.9 KB
/
crew.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
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
# Standard library imports
import os
import sys
import logging
from pathlib import Path
from textwrap import dedent
from datetime import datetime
# Third party imports
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from crewai import Agent, Task, Crew, Process
# Local application imports
import extracts # Ensure this module is available and correctly imported
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Load environment variables
load_dotenv()
gemini_api_key = os.getenv('GEMINI_API_KEY')
# Ensure the Path is correctly imported
if 'Path' not in globals():
from pathlib import Path
def get_subtitles():
whisper_output_dir = Path('whisper_output')
if not whisper_output_dir.exists():
logging.error(f"Directory not found: {whisper_output_dir}")
return None
srt_files = list(whisper_output_dir.glob('*.srt'))
if not srt_files:
logging.warning("No .srt files found in the whisper_output directory.")
return None
with open(srt_files[0], 'r') as file:
subtitles = file.read()
return subtitles
def main(extracts):
# Create the crew_output directory if it doesn't exist
os.makedirs("crew_output", exist_ok=True)
# Read subtitles
subtitles = get_subtitles()
if subtitles is None:
logging.error("Failed to read subtitles. Exiting.")
return
subtitler_agent_1 = Agent(
role=dedent((
f"""
Segment 1 Subtitler
""")),
backstory=dedent((
f"""
Experienced subtitler who writes captions or subtitles that accurately represent the audio, including dialogue, sound effects, and music. The subtitles need to be properly timed with the video using correct time codes.
""")),
goal=dedent((
f"""
Match a list of extracts from a video clip with the corresponding timed subtitles. Given the segments found by the Digital Producer, find the segment timings within the `.srt` file and return each segment as an `.srt` subtitle segment.
""")),
allow_delegation=False,
verbose=True,
max_iter=1,
max_rpm=1,
llm=ChatGoogleGenerativeAI(model="gemini-1.5-pro-exp-0801",
verbose=True,
temperature=0.0,
google_api_key=gemini_api_key)
)
subtitler_agent_2 = Agent(
role=dedent((
f"""
Segment 2 Subtitler
""")),
backstory=dedent((
f"""
Experienced subtitler who writes captions or subtitles that accurately represent the audio, including dialogue, sound effects, and music. The subtitles need to be properly timed with the video using correct time codes.
""")),
goal=dedent((
f"""
Match a list of extracts from a video clip with the corresponding timed subtitles. Given the segments found by the Digital Producer, find the segment timings within the `.srt` file and return each segment as an `.srt` subtitle segment.
""")),
allow_delegation=False,
verbose=True,
max_iter=1,
max_rpm=1,
llm=ChatGoogleGenerativeAI(model="gemini-1.5-pro-exp-0801",
verbose=True,
temperature=0.0,
google_api_key=gemini_api_key)
)
subtitler_agent_3 = Agent(
role=dedent((
f"""
Segment 3 Subtitler
""")),
backstory=dedent((
f"""
Experienced subtitler who writes captions or subtitles that accurately represent the audio, including dialogue, sound effects, and music. The subtitles need to be properly timed with the video using correct time codes.
""")),
goal=dedent((
f"""
Match a list of extracts from a video clip with the corresponding timed subtitles. Given the segments found by the Digital Producer, find the segment timings within the `.srt` file and return each segment as an `.srt` subtitle segment.
""")),
allow_delegation=False,
verbose=True,
max_iter=1,
max_rpm=1,
llm=ChatGoogleGenerativeAI(model="gemini-1.5-pro-exp-0801",
verbose=True,
temperature=0.0,
google_api_key=gemini_api_key)
)
return_subtitles_1 = Task(
description=dedent((
f"""
You will be provided with a transcription extract from a video clip and the full content of an .srt subtitle file corresponding to that clip. Your task is to match the transcription extract to the subtitle segment it best aligns with and return the results in a specific format.
Here is the transcription extract:
<segments>
{extracts[0]}
</segments>
Here is the full content of the .srt subtitle file:
<srt_file>
{subtitles}
</srt_file>
Please follow these steps:
1. Carefully read through the transcription excerpt within the <segments> tags.
2. Given the extract, search through the <srt_file> content to find the subtitle segment that best matches the extract. To determine the best match, look for segments that contain the most overlapping words or phrases with the extract.
3. Once you've found the best matching subtitle segment for the excerpt, format the match as follows:
[segment number]
[start time] --> [end time]
[matched transcription extract]
5. After processing the extract, combine the formatted matches into a single block of text. This should resemble a valid .srt subtitle file, with each match separated by a blank line.
Please note: .srt files have a specific format that must be followed exactly in order for them to be readable. Therefore, it is crucial that you do not include any extra content beyond the raw subtitle data itself. This means:
- No comments explaining your work
- No notes about which extracts matched which segments
- No additional text that isn't part of the subtitle segments
Simply return the matches, properly formatted, as the entire contents of your response.
""")),
expected_output=dedent((
f"""
Format each match exactly as follows, and include only these details:
[segment number]
[start time] --> [end time]
[matched transcription extract]
Compile all the matches and return them without any additional text or commentary.
Example of the expected output:
26
00:01:57,000 --> 00:02:00,400
Sight turned into insight.
27
00:02:00,400 --> 00:02:03,240
Seeing became understanding.
28
00:02:03,240 --> 00:02:05,680
Understanding led to actions,
Please note: .srt files have a specific format that must be followed exactly in order for them to be readable. Therefore, it is crucial that you DO NOT INCLUDE any extra content beyond the raw subtitle data itself. This means:
- No comments explaining your work
- No comments introducing your work
- No comments ending your work
- No notes about which extracts matched which segments
- No additional text that isn't part of the subtitle segments
- No comments like: "Here is the output with the matched segments in the requested format:"
""")),
agent=subtitler_agent_1,
output_file=f'crew_output/new_file_return_subtitles_1_{datetime.now().strftime("%Y%m%d_%H%M%S_%f")}.srt'
)
return_subtitles_2 = Task(
description=dedent((
f"""
You will be provided with a transcription extract from a video clip and the full content of an .srt subtitle file corresponding to that clip. Your task is to match the transcription extract to the subtitle segment it best aligns with and return the results in a specific format.
Here is the transcription extract:
<segments>
{extracts[1]}
</segments>
Here is the full content of the .srt subtitle file:
<srt_file>
{subtitles}
</srt_file>
Please follow these steps:
1. Carefully read through the transcription excerpt within the <segments> tags.
2. Given the extract, search through the <srt_file> content to find the subtitle segment that best matches the extract. To determine the best match, look for segments that contain the most overlapping words or phrases with the extract.
3. Once you've found the best matching subtitle segment for the excerpt, format the match as follows:
[segment number]
[start time] --> [end time]
[matched transcription extract]
5. After processing the extract, combine the formatted matches into a single block of text. This should resemble a valid .srt subtitle file, with each match separated by a blank line.
Please note: .srt files have a specific format that must be followed exactly in order for them to be readable. Therefore, it is crucial that you do not include any extra content beyond the raw subtitle data itself. This means:
- No comments explaining your work
- No notes about which extracts matched which segments
- No additional text that isn't part of the subtitle segments
Simply return the matches, properly formatted, as the entire contents of your response.
""")),
expected_output=dedent((
f"""
Format each match exactly as follows, and include only these details:
[segment number]
[start time] --> [end time]
[matched transcription extract]
Compile all the matches and return them without any additional text or commentary.
Example of the expected output:
26
00:01:57,000 --> 00:02:00,400
Sight turned into insight.
27
00:02:00,400 --> 00:02:03,240
Seeing became understanding.
28
00:02:03,240 --> 00:02:05,680
Understanding led to actions,
Please note: .srt files have a specific format that must be followed exactly in order for them to be readable. Therefore, it is crucial that you DO NOT INCLUDE any extra content beyond the raw subtitle data itself. This means:
- No comments explaining your work
- No comments introducing your work
- No comments ending your work
- No notes about which extracts matched which segments
- No additional text that isn't part of the subtitle segments
- No comments like: "Here is the output with the matched segments in the requested format:"
""")),
agent=subtitler_agent_2,
# ↑ specify which task's output should be used as context for subsequent tasks
output_file=f'crew_output/new_file_return_subtitles_2_{datetime.now().strftime("%Y%m%d_%H%M%S_%f")}.srt'
)
return_subtitles_3 = Task(
description=dedent((
f"""
You will be provided with a transcription extract from a video clip and the full content of an .srt subtitle file corresponding to that clip. Your task is to match the transcription extract to the subtitle segment it best aligns with and return the results in a specific format.
Here is the transcription extract:
<segments>
{extracts[2]}
</segments>
Here is the full content of the .srt subtitle file:
<srt_file>
{subtitles}
</srt_file>
Please follow these steps:
1. Carefully read through the transcription excerpt within the <segments> tags.
2. Given the extract, search through the <srt_file> content to find the subtitle segment that best matches the extract. To determine the best match, look for segments that contain the most overlapping words or phrases with the extract.
3. Once you've found the best matching subtitle segment for the excerpt, format the match as follows:
[segment number]
[start time] --> [end time]
[matched transcription extract]
5. After processing the extract, combine the formatted matches into a single block of text. This should resemble a valid .srt subtitle file, with each match separated by a blank line.
Please note: .srt files have a specific format that must be followed exactly in order for them to be readable. Therefore, it is crucial that you do not include any extra content beyond the raw subtitle data itself. This means:
- No comments explaining your work
- No notes about which extracts matched which segments
- No additional text that isn't part of the subtitle segments
Simply return the matches, properly formatted, as the entire contents of your response.
""")),
expected_output=dedent((
f"""
Format each match exactly as follows, and include only these details:
[segment number]
[start time] --> [end time]
[matched transcription extract]
Compile all the matches and return them without any additional text or commentary.
Example of the expected output:
26
00:01:57,000 --> 00:02:00,400
Sight turned into insight.
27
00:02:00,400 --> 00:02:03,240
Seeing became understanding.
28
00:02:03,240 --> 00:02:05,680
Understanding led to actions,
Please note: .srt files have a specific format that must be followed exactly in order for them to be readable. Therefore, it is crucial that you DO NOT INCLUDE any extra content beyond the raw subtitle data itself. This means:
- No comments explaining your work
- No comments introducing your work
- No comments ending your work
- No notes about which extracts matched which segments
- No additional text that isn't part of the subtitle segments
- No comments like: "Here is the output with the matched segments in the requested format:"
""")),
agent=subtitler_agent_3,
output_file=f'crew_output/new_file_return_subtitles_3_{datetime.now().strftime("%Y%m%d_%H%M%S_%f")}.srt'
)
crew = Crew(
agents=[subtitler_agent_1, subtitler_agent_2, subtitler_agent_3],
tasks=[return_subtitles_1, return_subtitles_2, return_subtitles_3],
verbose=2,
process=Process.sequential,
)
result = crew.kickoff()
logging.info(dedent(f"""\n\n########################"""))
logging.info(dedent(f"""## Here is your custom crew run result:"""))
logging.info(dedent(f"""########################\n"""))
logging.info(result)
return result
if __name__ == "__main__":
extracts_data = extracts.main()
if extracts_data:
main(extracts_data)
else:
logging.error("Failed to generate extracts. Exiting.")
# TO-DO: Change it so that `subtitler_agents` instead of returning entire subtitle files as .srt, they return the first and final time codes of when the quote would start and end (e.g., 00:02:51,400 --> 00:02:56,560).
# TO-DO: After implementing this, a script copies and pastes the returned chunks of the script into an .srt file that will be used to burn in the subtitles.