-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_description.py
More file actions
73 lines (58 loc) · 2.55 KB
/
Copy pathgenerate_description.py
File metadata and controls
73 lines (58 loc) · 2.55 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
from dotenv import load_dotenv
from openai_client import openai_client
load_dotenv()
client = openai_client()
def read_srt_file(file_path):
subtitles = []
with open(file_path, 'r') as file:
lines = file.readlines()
subtitle = {'index': None, 'start': None, 'end': None, 'text': ''}
for line in lines:
line = line.strip()
if line.isdigit():
if subtitle['index'] is not None:
subtitles.append(subtitle)
subtitle = {'index': int(line), 'start': None, 'end': None, 'text': ''}
elif ' --> ' in line:
start, end = line.split(' --> ')
subtitle['start'] = start.strip()
subtitle['end'] = end.strip()
elif line == '':
continue
else:
subtitle['text'] += line + ' '
if subtitle['index'] is not None:
subtitles.append(subtitle)
return subtitles
def split_subtitles(subtitles, chunk_size=28):
chunks = []
for i in range(0, len(subtitles), chunk_size):
chunks.append(subtitles[i:i+chunk_size])
return chunks
def summ_desc(section):
prompt = "The format you will use is the following: 00:00 - name\n. Return only 2-3 words in the format in the place of the word 'name'. Only return 1 sentence, no more. so you just summarize the list into 1 row using that template i gave you. So the time is in minutes and seconds, the data: "
try:
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=prompt + str(section),
max_tokens=50,
temperature=0.8,
)
return response.choices[0].text.strip()
except Exception as e:
print("An error occurred:", str(e))
def generate_description(title, ythandle, channel):
srt_path = 'output/videos/' + channel + "/" + title + '/subtitles.srt'
subtitles = read_srt_file(srt_path)
subtitle_chunks = split_subtitles(subtitles)
items = []
for i, chunk in enumerate(subtitle_chunks, start=1):
items.append(summ_desc(chunk))
intro_template = "Subscribe " + ythandle + "\n\n-timestamps-\n"
total = intro_template + "\n".join(items)
path = "output/videos/" + channel + "/" + title + "/"
output_file = path + "description.txt"
with open(output_file, 'w') as f:
f.write(total)
print("Description saved to", output_file)
# generate_description("TEST_TITLE", "@TheUniverseDecoded", "the_universe_decoded")