I’m working on a demo that will do a prompt based on images found within a zip file. My logic basically boils down to:
Take the zip and find all images in it
For each image, I read in the bits from zip as a variable, bits, and then upload it, adding the result files object to an array
When its time to do my prompt, I simply prepend the array of file objs w/ a hard coded prompt and fire it off.
I keep getting vauge 400 errors about the request containing invalid arguments. From what I can see, each file upload works just fine, but there’s something off because if I modify my code to extract and save the image to the file system, using a temporary name, and then upload that, it works. I’d rather avoid saving the images. Does anyone see anything wrong with how I’m getting the data? Here’s a snippet:
gemini_files = []
if comic.endswith("cbz"):
with zipfile.ZipFile(os.path.join(comic_dir,comic),'r') as zip:
files = zip.namelist()
# todo - check and see if we need more image extensions
images = [file for file in files if (file.endswith("jpg"))][0:3]
counter = 0
for image in images:
with zip.open(image, 'r') as imgbin:
print('read binary for',image)
bits = io.BytesIO(imgbin.read())
gem_file = client.files.upload(file=bits, config={"mime_type":"image/jpg"})
gemini_files.append(gem_file)
Note the [0:3] after the list comprehension is just a shortcut to limit the number of images while I test. If I switch my code as described:
counter = counter + 1
with open(f'temp{counter}.jpg', 'wb') as file:
file.write(imgbin.read())
gemini_files.append(client.files.upload(file=f'temp{counter}.jpg'))
It works.