My current code is,
<code>
def upload_file(file_path):
return genai.upload_file(pathlib.Path(file_path))
def analyze_frames(frame_paths):
model = genai.GenerativeModel("gemini-1.5-pro")
prompt = """
Give me a description after looking at these images
"""
# Upload the images using the File API
logger.info("Uploading files...")
file_references = []
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = [
executor.submit(upload_file, frame_path) for frame_path in frame_paths
]
for future in tqdm(
concurrent.futures.as_completed(futures),
total=len(futures),
desc="Uploading files",
):
file_references.append(future.result())
logger.info("Uploading files completed...")
# Generate content using the file references
logger.info("Making inferences using the model...", model.model_name)
response = model.generate_content(
[
prompt,
*file_references,
]
)
return response.text
def process_batches(frames, batch_size):
# Split the frames into batches
batch_list = [frames[i : i + batch_size] for i in range(0, len(frames), batch_size)]
batch_results = []
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(analyze_frames, batch): batch for batch in batch_list
}
for future in tqdm(
as_completed(futures),
total=len(futures),
desc="Processing batches",
):
batch_results.append(future.result())
return batch_results
</code>
<code>
def upload_file(file_path):
return genai.upload_file(pathlib.Path(file_path))
def analyze_frames(frame_paths):
model = genai.GenerativeModel("gemini-1.5-pro")
prompt = """
Give me a description after looking at these images
"""
# Upload the images using the File API
logger.info("Uploading files...")
file_references = []
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = [
executor.submit(upload_file, frame_path) for frame_path in frame_paths
]
for future in tqdm(
concurrent.futures.as_completed(futures),
total=len(futures),
desc="Uploading files",
):
file_references.append(future.result())
logger.info("Uploading files completed...")
# Generate content using the file references
logger.info("Making inferences using the model...", model.model_name)
response = model.generate_content(
[
prompt,
*file_references,
]
)
return response.text
def process_batches(frames, batch_size):
# Split the frames into batches
batch_list = [frames[i : i + batch_size] for i in range(0, len(frames), batch_size)]
batch_results = []
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(analyze_frames, batch): batch for batch in batch_list
}
for future in tqdm(
as_completed(futures),
total=len(futures),
desc="Processing batches",
):
batch_results.append(future.result())
return batch_results
</code>
def upload_file(file_path):
return genai.upload_file(pathlib.Path(file_path))
def analyze_frames(frame_paths):
model = genai.GenerativeModel("gemini-1.5-pro")
prompt = """
Give me a description after looking at these images
"""
# Upload the images using the File API
logger.info("Uploading files...")
file_references = []
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = [
executor.submit(upload_file, frame_path) for frame_path in frame_paths
]
for future in tqdm(
concurrent.futures.as_completed(futures),
total=len(futures),
desc="Uploading files",
):
file_references.append(future.result())
logger.info("Uploading files completed...")
# Generate content using the file references
logger.info("Making inferences using the model...", model.model_name)
response = model.generate_content(
[
prompt,
*file_references,
]
)
return response.text
def process_batches(frames, batch_size):
# Split the frames into batches
batch_list = [frames[i : i + batch_size] for i in range(0, len(frames), batch_size)]
batch_results = []
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(analyze_frames, batch): batch for batch in batch_list
}
for future in tqdm(
as_completed(futures),
total=len(futures),
desc="Processing batches",
):
batch_results.append(future.result())
return batch_results
I call this as,
result = process_batches(frame_paths, 3550)
Problem
It uploads parallel correctly for sometime, and then hangs (doesn’t progress after this)
<code>Uploading files: 4%|████▍ 155/3550 [00:53<19:29, 2.90it/s]
Uploading files: 9%|████████▊ 158/1850 [00:55<09:53, 2.85it/s]
</code>
<code>Uploading files: 4%|████▍ 155/3550 [00:53<19:29, 2.90it/s]
Uploading files: 9%|████████▊ 158/1850 [00:55<09:53, 2.85it/s]
</code>
Uploading files: 4%|████▍ 155/3550 [00:53<19:29, 2.90it/s]
Uploading files: 9%|████████▊ 158/1850 [00:55<09:53, 2.85it/s]
Observations
- It always fails around at ~160 images in each batch process
References
-
https://github.com/google-gemini/generative-ai-python/issues/211#issuecomment-2143293996
-
https://github.com/google-gemini/generative-ai-python/issues/327#issue-2285062757