-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
371 lines (321 loc) · 12.1 KB
/
app.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from dotenv import load_dotenv
from openai import OpenAI
from elevenlabs.client import AsyncElevenLabs
import asyncio
import json
import os
import argparse
import concurrent.futures
import time
import random
from manga_extraction import (
extract_all_pages_as_images,
save_important_pages,
split_volume_into_parts,
save_all_pages,
extract_panels,
scale_base64_image,
)
from vision_analysis import (
analyze_images_with_gpt4_vision,
detect_important_pages,
get_important_panels,
VISION_PRICE_PER_TOKEN,
)
from prompts import (
DRAMATIC_PROMPT,
BASIC_PROMPT,
BASIC_PROMPT_WITH_CONTEXT,
BASIC_INSTRUCTIONS,
KEY_PAGE_IDENTIFICATION_INSTRUCTIONS,
KEY_PANEL_IDENTIFICATION_PROMPT,
KEY_PANEL_IDENTIFICATION_INSTRUCTIONS,
)
from citation_processing import extract_text_and_citations, extract_script
from movie_director import make_movie
from openai import APIError, RateLimitError
from tenacity import retry, stop_after_attempt, wait_exponential
load_dotenv() # Load environment variables from .env file
def write_text_to_file(movie_script, manga, volume_number):
output_dir = f"{manga}/v{volume_number}"
os.makedirs(output_dir, exist_ok=True)
output_file = f"{output_dir}/extracted_text.txt"
with open(output_file, 'w', encoding='utf-8') as f:
for segment in movie_script:
f.write(f"Segment Text:\n{segment['text']}\n\n")
if 'citations' in segment:
f.write(f"Citations: {', '.join(map(str, segment['citations']))}\n\n")
f.write("-" * 50 + "\n\n")
print(f"Extracted text has been written to {output_file}")
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=4, max=10))
def retry_api_call(func, *args, **kwargs):
try:
return func(*args, **kwargs)
except RateLimitError as e:
print(f"Rate limit reached. Retrying in a moment...")
raise e
except APIError as e:
if "rate limit" in str(e).lower():
print(f"API error related to rate limit. Retrying...")
raise RateLimitError(str(e))
raise e
async def main(volume_number, manga, text_only=False):
# Initialize OpenAI client with API key
client = OpenAI()
# Only initialize ElevenLabs client if we're not in text-only mode
narration_client = None
if not text_only:
narration_client = AsyncElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
print("Extracting all pages from the volume...")
volume_scaled_and_unscaled = extract_all_pages_as_images(
f"{manga}/v{volume_number}/v{volume_number}.pdf"
)
volume = volume_scaled_and_unscaled["scaled"]
volume_unscaled = volume_scaled_and_unscaled["full"]
print("Total pages in volume:", len(volume))
if len(volume) == 0:
print("Error: No images extracted from the PDF. Please check the PDF file.")
return
profile_reference = extract_all_pages_as_images(f"{manga}/profile-reference.pdf")[
"scaled"
]
chapter_reference = extract_all_pages_as_images(f"{manga}/chapter-reference.pdf")[
"scaled"
]
profile_pages = []
chapter_pages = []
important_page_tokens = 0
batch_size = 20
print("Identifying important pages in the volume...")
def process_batch(start_idx, pages):
response = retry_api_call(
detect_important_pages,
profile_reference,
chapter_reference,
pages,
client,
KEY_PAGE_IDENTIFICATION_INSTRUCTIONS,
KEY_PAGE_IDENTIFICATION_INSTRUCTIONS,
)
return start_idx, response
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for i in range(0, len(volume), batch_size):
pages = volume[i : i + batch_size]
futures.append(executor.submit(process_batch, i, pages))
for future in concurrent.futures.as_completed(futures):
start_idx, response = future.result()
end_index = start_idx + batch_size - 1
print(f"Processing pages {start_idx} to {min(end_index, len(volume)-1)}")
ip = response["parsed_response"]
print(json.dumps(ip, indent=2))
for page in ip:
if page["type"] == "profile":
profile_pages.append(page["image_index"] + start_idx)
elif page["type"] == "chapter":
chapter_pages.append(page["image_index"] + start_idx)
important_page_tokens += response["total_tokens"]
profile_pages.sort()
chapter_pages.sort()
print("Total tokens to extract profiles and chapters:", important_page_tokens)
print("\n__________\n")
print("Profile pages:", profile_pages)
print("Chapter pages:", chapter_pages)
chapter_pages = [1]
profile_pages = [0]
print(f"{len(volume)}")
print("\n__________\n")
print("Saving important pages to disk for QA...")
save_important_pages(volume, profile_pages, chapter_pages, manga, volume_number)
character_profiles = [volume[i] for i in profile_pages]
NUMBER_OF_JOBS = 7
jobs = split_volume_into_parts(
volume, volume_unscaled, chapter_pages, NUMBER_OF_JOBS
)
parts = jobs["parts"]
jobs_unscaled = jobs["unscaled_images"]
jobs = jobs["scaled_images"]
# Summarize the images in the first job
response = retry_api_call(
analyze_images_with_gpt4_vision,
character_profiles, jobs[0], client, BASIC_PROMPT, BASIC_INSTRUCTIONS
)
recap = response.choices[0].message.content
tokens = response.usage.total_tokens
movie_script = extract_text_and_citations(
response.choices[0].message.content, jobs[0], jobs_unscaled[0]
)
print("\n\n\n_____________\n\n\n")
print(response.choices[0].message.content)
# iterate through the rest of the jobs while adding context from previous ones
for i, job in enumerate(jobs):
if i == 0:
continue
response = retry_api_call(
analyze_images_with_gpt4_vision,
character_profiles,
job,
client,
recap + "\n-----\n" + BASIC_PROMPT_WITH_CONTEXT,
BASIC_INSTRUCTIONS,
)
recap = recap + "\n\n" + response.choices[0].message.content
tokens += response.usage.total_tokens
print("\n\n\n_____________\n\n\n")
print(response.choices[0].message.content)
movie_script = movie_script + extract_text_and_citations(
response.choices[0].message.content, job, jobs_unscaled[i]
)
print("\n\n\n_____________\n\n\n")
print("\n\n\n_____________\n\n\n")
print("\n\n\n_____________\n\n\n")
narration_script = extract_script(movie_script)
print(narration_script)
print("\n___________\n")
extract_panels(movie_script)
print("Extracting panels from movie script...")
for i, segment in enumerate(movie_script):
print(f"Processing segment {i}")
print(f"Number of images in segment: {len(segment['images'])}")
print(f"Number of unscaled images in segment: {len(segment['images_unscaled'])}")
if len(segment['images']) == 0:
print(f"Warning: No images found in segment {i}. Skipping panel extraction.")
continue
extract_panels(segment)
all_panels_base64 = [
panel for sublist in segment["panels"].values() for panel in sublist
]
print(f"Number of panels extracted: {len(all_panels_base64)}")
print("number of segments:", len(movie_script))
for i, segment in enumerate(movie_script):
print("segment", i, ": ", segment["text"])
all_panels_base64 = [
panel for sublist in segment["panels"].values() for panel in sublist
]
print(len(all_panels_base64))
print("number of panels:", len(all_panels_base64))
print("number of images:", len(segment["images"]))
def process_segment(segment_tuple):
i, segment = segment_tuple
panels = []
for j, page in enumerate(segment["images"]):
if "panels" in segment:
if j not in segment["panels"]:
panels.append(page)
else:
for panel in segment["panels"][j]:
panels.append(panel)
else:
panels.append(page)
scaled_panels = [scale_base64_image(p) for p in panels]
response = retry_api_call(
get_important_panels,
profile_reference,
scaled_panels,
client,
segment["text"] + "\n________\n" + KEY_PANEL_IDENTIFICATION_PROMPT,
KEY_PANEL_IDENTIFICATION_INSTRUCTIONS,
)
important_panels = response["parsed_response"]
if not isinstance(important_panels, list):
important_panels = []
ip = []
for p in important_panels:
number = p
if isinstance(number, str):
if number.isdigit():
number = int(number)
if not isinstance(number, int):
continue
if number < len(panels):
ip.append(panels[number])
return i, ip, response["total_tokens"]
panel_tokens = 0
important_panels_info = {}
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(process_segment, (i, segment))
for i, segment in enumerate(movie_script)
]
for future in concurrent.futures.as_completed(futures):
i, ip, tokens = future.result()
if ip:
print("Important panels for segment", i, "exist.")
else:
print("No important panels for segment", i)
movie_script[i]["important_panels"] = ip
panel_tokens += tokens
ELEVENLABS_PRICE_PER_CHARACTER = 0.0003
print(
"Tokens for extracting profiles and chapters:",
important_page_tokens,
" | ",
"${:,.4f}".format(VISION_PRICE_PER_TOKEN * important_page_tokens),
)
print(
"Tokens for summarization:",
tokens,
" | ",
"${:,.4f}".format(VISION_PRICE_PER_TOKEN * tokens),
)
print(
"Tokens for extracting important panels:",
panel_tokens,
" | ",
"${:,.4f}".format(VISION_PRICE_PER_TOKEN * panel_tokens),
)
total_gpt_tokens = important_page_tokens + tokens + panel_tokens
print(
"Total GPT tokens:",
total_gpt_tokens,
" | ",
"${:,.4f}".format(VISION_PRICE_PER_TOKEN * (total_gpt_tokens)),
)
if not text_only:
narration_script = extract_script(movie_script)
print(
"Total elevenlabs characters:",
len(narration_script),
" | ",
"${:,.4f}".format(ELEVENLABS_PRICE_PER_CHARACTER * (len(narration_script))),
)
print(
"GRAND TOTAL COST",
" | ",
"${:,.4f}".format(
VISION_PRICE_PER_TOKEN * (total_gpt_tokens)
+ ELEVENLABS_PRICE_PER_CHARACTER * (len(narration_script))
),
)
else:
print(
"GRAND TOTAL COST (GPT only)",
" | ",
"${:,.4f}".format(VISION_PRICE_PER_TOKEN * (total_gpt_tokens)),
)
if text_only:
write_text_to_file(movie_script, manga, volume_number)
print("Text-only mode: Skipping narration and video creation.")
else:
await make_movie(movie_script, manga, volume_number, narration_client)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process manga volumes.")
parser.add_argument(
"--manga",
type=str,
default="naruto",
help="Manga name to process (default: naruto)",
)
parser.add_argument(
"--volume-number",
type=int,
default=10,
help="Volume number to process (default: 10)",
)
parser.add_argument(
"--text-only",
action="store_true",
help="Output extracted text to a file instead of creating a video",
)
args = parser.parse_args()
asyncio.run(main(args.volume_number, args.manga, args.text_only))