This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_dallee_gpt4.py
59 lines (46 loc) · 2.22 KB
/
video_dallee_gpt4.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
from openai import OpenAI
import os
client = OpenAI()
def generate_image_prompt(context, current_text):
"""Generate an image prompt based on the current text and previous context."""
prompt = (
f"Context: {context}\n"
"The following text was transcribed from a toddler speaking in romanian. Keeping in mind the above context, generate a concise, 20-word image prompt for a toddler-style illustration based on the below text.:\n"
f"Text: {current_text}"
)
print(f"--------------------------------------")
print(f"Generating image prompt for: {prompt}")
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "system", "content": prompt}
]
)
# Correctly access the response content
generated_prompt = response.choices[0].message.content
print(f"Generated image prompt: {generated_prompt}")
return generated_prompt
def process_subtitles_for_image_prompts(input_file_path, output_file_path):
with open(input_file_path, 'r') as infile:
lines = infile.readlines()
context_prompts = [] # List to store the most recent prompts
image_prompts = []
for i in range(0, len(lines), 4):
print(f"Processing subtitle {i//4 + 1} of {len(lines)//4}")
if i + 1 < len(lines) and ' --> ' in lines[i+1]:
print(f"Skipping line {i+1} because it is a timestamp")
subtitle_text = lines[i+2].strip()
prompt = generate_image_prompt(context_prompts[-3:], subtitle_text)
# Update context with the new prompt, keeping only the last 3 prompts
context_prompts.append(prompt)
if len(context_prompts) > 3:
context_prompts.pop(0) # Remove the oldest prompt
image_prompts.append(f"{lines[i]}{lines[i+1]}{prompt}\n\n")
# Write the image prompts to the output file
with open(output_file_path, 'w') as outfile:
outfile.writelines(image_prompts)
print(f"Image prompts written to {output_file_path}")
# Example usage
input_file_path = "sources/eric1_accumulated.srt"
output_file_path = "sources/eric1_image_prompts.srt"
process_subtitles_for_image_prompts(input_file_path, output_file_path)