-
Notifications
You must be signed in to change notification settings - Fork 4
/
summary_chapters_blog.py
271 lines (221 loc) · 9.31 KB
/
summary_chapters_blog.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
# Description: This script takes a JSON file as input and outputs a summary of the video and the
# chapters for adding to the video description on YouTube.
#
# Usage:
# python summary_and_chapters.py <input_json_file> [--generate_summary] [--generate_chapters] \
# [--generate_blog] [--print_prompts] [--trim_length]
#
# Example:
# python summary_and_chapters.py "input_json.json"
import argparse
import json
import openai
# get the input video file and the output text file
parser = argparse.ArgumentParser()
parser.add_argument("input_json_file", help="input json transcription file")
# non positional arguments for generating summary and chapters
parser.add_argument("--generate_summary", action="store_true", help="generate summary")
parser.add_argument(
"--generate_chapters", action="store_true", help="generate chapters"
)
parser.add_argument("--generate_blog", action="store_true", help="generate blog")
parser.add_argument("--print_prompts", action="store_true", help="print prompts")
parser.add_argument("--trim_length", type=int, default=100, help="trim length")
parser.add_argument(
"--wshiper_cpp_json", action="store_true", help="is this a whisper cpp json file?"
)
# optional arguments for generating summary and chapters
parser.add_argument("--summary_prompt", type=str, default="", help="prompt to use for summary")
args = parser.parse_args()
# get the input video file name and the output text file name
input_json_file = args.input_json_file
# read the input JSON file
# print("Parsing the input JSON file...")
with open(input_json_file) as f:
data = json.load(f)
# combine words into sentences and keep the timings, using the start time of the first word
# and the end time of the last word.
# sentences are separated by a `punctuation` type item in the JSON file.
# collect sentences in a list of lists of items from the JSON file.
sentences = []
if not args.wshiper_cpp_json:
sentence = []
for item in data["results"]["items"]:
# if the item is a punctuation, then it's the end of the sentence
if item["type"] == "punctuation" and item["alternatives"][0]["content"] in [
".",
"?",
"!",
]:
# add an 'end_time' to the punctuation item by using the end time of the last word
item["end_time"] = (
sentence[-1]["end_time"] if len(sentence) > 0 else item["start_time"]
)
# add the punctuation to the sentence
sentence.append(item)
# add the sentence to the list of sentences
sentences.append(sentence)
# start a new sentence
sentence = []
else:
# filter out the filler words
if item["type"] == "pronunciation" and item["alternatives"][0][
"content"
].lower() in ["um", "uh", "so", "hmm", "like"]:
continue
# filter out punctuation
if item["type"] == "punctuation":
continue
# add the word to the sentence
sentence.append(item)
# get the timings of the sentences
sentences_timings = []
for sentence in sentences:
# get the start time of the sentence
start_time = float(sentence[0]["start_time"])
# get the end time of the sentence
end_time = float(sentence[-1]["end_time"])
# add the timings to the list of timings
sentences_timings.append((start_time, end_time))
def convert_senconds_to_mmss(seconds):
return f"{int(seconds // 60):02d}:{int(seconds % 60):02d}"
def build_summary(trim=True, remove_filler_words=True):
# build a summary list from the senstences and their timings
summary = []
if not args.wshiper_cpp_json:
for sentence, timings in zip(sentences, sentences_timings):
# get the pronounciations from the sentence
pronounciations = [
item["alternatives"][0]["content"].strip()
for item in sentence
if item["type"] == "pronunciation"
]
if remove_filler_words:
# remove the filler words from the sentence
pronounciations = [
word
for word in pronounciations
if word.lower() not in ["um", "uh", "so", "hmm", "like"]
]
# get the sentence text
sentence_text = " ".join(pronounciations) + "."
if trim:
# trim the sentence text to a maximum of 100 characters
sentence_text = sentence_text[: args.trim_length]
# get the sentence start and end timings
sentence_start_time, sentence_end_time = timings
# convert the timings to strings in the format MM:SS
sentence_start_time = f"{convert_senconds_to_mmss(sentence_start_time)}"
sentence_end_time = f"{convert_senconds_to_mmss(sentence_end_time)}"
# add the sentence to the summary
summary.append(
{
"text": sentence_text,
"start_time": sentence_start_time,
"end_time": sentence_end_time,
}
)
else:
for sentence in data["transcription"]:
# get the sentence text
sentence_text = sentence["text"]
if trim:
# trim the sentence text to a maximum of 100 characters
sentence_text = sentence_text[: args.trim_length]
# get the sentence start and end timings
sentence_start_time = sentence["timestamps"]["from"]
sentence_end_time = sentence["timestamps"]["to"]
# add the sentence to the summary
summary.append(
{
"text": sentence_text,
"start_time": sentence_start_time,
"end_time": sentence_end_time,
}
)
return summary
if args.generate_summary:
# build a prompt for OpenAI generation:
prompt = "transcript for the video:\n"
prompt += "---\n"
for sentence in build_summary(trim=args.trim_length > 0):
prompt += f"{sentence['text']}\n"
prompt += "---\n"
if args.summary_prompt is not None and args.summary_prompt != "":
prompt += args.summary_prompt
else:
prompt += (
"write a short summary description paragraph for the above video on YouTube.\n"
)
prompt += "Summary for the video:\n"
if args.print_prompts:
print(prompt)
history = [{"role": "user", "content": prompt}]
# send a request to the OpenAI API (model gpt-3.5-turbo) to generate the summary
# print("Sending a request to the OpenAI API to generate the summary...")
print("Generating the summary...")
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=history,
)
# get the generated summary
generated_summary = response["choices"][0]["message"]["content"]
history += [{"role": "assistant", "content": generated_summary}]
# print the generated summary
print("----------------------")
print(generated_summary)
print("----------------------")
if args.generate_chapters:
prompt = "transcript for the video:\n"
prompt += "---\n"
for sentence in build_summary(trim=True):
prompt += (
f"[{sentence['start_time']} - {sentence['end_time']}] {sentence['text']}\n"
)
prompt += "---\n"
prompt += (
"write up to 10 high-level chapters for the video on YouTube in the format: "
+ "'MM:SS <chapter-title>.'\n"
)
prompt += "Chapters for the video:\n"
if args.print_prompts:
print(prompt)
history = [{"role": "user", "content": prompt}]
# send a request to the OpenAI API (model gpt-3.5-turbo) to generate the chapters
print("Sending a request to the OpenAI API to generate the chapters...")
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=history,
)
# get the generated chapters
generated_chapters = response["choices"][0]["message"]["content"]
history += [{"role": "assistant", "content": generated_chapters}]
# print the generated chapters
print("----------------------")
print(generated_chapters)
print("----------------------")
if args.generate_blog:
prompt = "transcript for the video:\n"
prompt += "---\n"
for sentence in build_summary(trim=False):
prompt += f"{sentence['text']}\n"
prompt += "---\n"
prompt += "write a blog post of at least 500 words for the above video. write the title and then the post body.\n"
prompt += "Title of the blog post:\n"
if args.print_prompts:
print(prompt)
history = [{"role": "user", "content": prompt}]
# send a request to the OpenAI API (model gpt-3.5-turbo) to generate the blog post
print("Sending a request to the OpenAI API to generate the blog post...")
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=history,
)
# get the generated blog post
generated_blog = response["choices"][0]["message"]["content"]
history += [{"role": "assistant", "content": generated_blog}]
# print the generated blog post
print("----------------------")
print(generated_blog)
print("----------------------")
print("Done.")