replicate / replicate-python

Python client for Replicate
https://replicate.com
Apache License 2.0
764 stars 219 forks source link

Training fail when passing file handle to create training for stability-ai's sdxl "Training failed. input_images_filetype must be zip or tar" #362

Open abdul7235 opened 1 month ago

abdul7235 commented 1 month ago

I am opening the file handler and passing it to the replicate.trainings.create, my file

file_input = open(f"{file_name}","rb")

training = replicate.trainings.create( model="stability-ai/sdxl", version="7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc", input={ "input_images": file_input, "token_string": "TOK", "caption_prefix": "a photo of TOK", "max_train_steps": 1000, "use_face_detection_instead": False }, destination=f"{OWNER}/{training_data.model_name}" )

I get following message in the logs:

**Training failed.

input_images_filetype must be zip or tar Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/cog/server/worker.py", line 349, in _predict result = predict(payload) File "train.py", line 142, in train input_dir = preprocess( File "/src/preprocess.py", line 118, in preprocess assert False, "input_images_filetype must be zip or tar" AssertionError: input_images_filetype must be zip or tar

However on the web interface I can see a valid zip file uploaded which if I download and open is valid (screenshot attached) image

please help me sort it out.

abdul7235 commented 1 month ago

Solution

I was able to solve it after a day long effort.

Here is how I was able to send local zip file for training:

   filename = "images.zip"
   with open(f"{filename}", "rb") as file_input:
        encoded_data = base64.b64encode(file_input.read())

    encoded_data_str = encoded_data.decode('utf-8')

    training = replicate.trainings.create(
        model="stability-ai/sdxl",
        version="7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc",
        input={
            "input_images": f"data:application/zip;base64,{encoded_data_str}",
            "token_string": "TOK",
            "caption_prefix": "a photo of TOK",
            "max_train_steps": 1000,
            "use_face_detection_instead": False
        },
        destination=f"{OWNER}/{model_name}"
    )
abdul7235 commented 1 month ago

This needs to be added in the documentation please.