comfyui icon indicating copy to clipboard operation
comfyui copied to clipboard

Outputting bas64 gives `can only concatenate str (not \"bytes\") to str` error

Open berkelmas opened this issue 2 years ago • 4 comments

I have edited build/COPY_ROOT/opt/serverless/handlers/basehandler.py file and used the existing image_to_base64 function to output as base64 instead of uploading the generated image to S3 bucket but I am getting can only concatenate str (not \"bytes\") to str error. this below is my code for get_result function. Is there a problem with the base64 conversion?

    def get_result(self, job_id):
        result = requests.get(self.ENDPOINT_HISTORY).json()[self.comfyui_job_id]

        prompt = result["prompt"]
        outputs = result["outputs"]

        self.result = {
            "images": [],
            "timings": {}
        }
        
        custom_output_dir = f"{self.OUTPUT_DIR}{self.request_id}"
        os.makedirs(custom_output_dir, exist_ok = True)
        for item in outputs:
            if "images" in outputs[item]:
                for image in outputs[item]["images"]:
                    original_path = f"{self.OUTPUT_DIR}{image['subfolder']}/{image['filename']}"
                    new_path = f"{custom_output_dir}/{image['filename']}"
                    # Handle duplicated request where output file in not re-generated
                    if os.path.islink(original_path):
                        shutil.copyfile(os.path.realpath(original_path), new_path)
                    else:
                        os.rename(original_path, new_path)
                        os.symlink(new_path, original_path)
                    key = f"{self.request_id}/{image['filename']}"
                    self.result["images"].append({
                        "local_path": new_path,
                        "base64": self.image_to_base64(new_path),
                        # make this work first, then threads
                        # "url": self.s3utils.file_upload(new_path, key)
                    })
        
        self.job_time_completed = datetime.datetime.now()
        self.result["timings"] = {
            "job_time_received": self.job_time_received.ctime(),
            "job_time_queued": self.job_time_queued.ctime(),
            "job_time_processed": self.job_time_processed.ctime(),
            "job_time_completed": self.job_time_completed.ctime(),
            "job_time_total": (self.job_time_completed - self.job_time_received).seconds
        }

        return self.result

and this is the existing image_to_base64 function:

    def image_to_base64(self, path):
        with open(path, "rb") as f:
            b64 = (base64.b64encode(f.read()))
        return "data:image/png;charset=utf-8;base64, " + b64

berkelmas avatar Dec 12 '23 11:12 berkelmas

Probably, yes. I did not finish that function as I decided not to support base64 output for the time being.

I'll add it back when I get chance but using s3 is the only supported method for now.

robballantyne avatar Dec 12 '23 11:12 robballantyne

Ah okay. I can fix it and create a PR here in this repo if you would like

berkelmas avatar Dec 12 '23 11:12 berkelmas

That would be great, thank you.

robballantyne avatar Dec 12 '23 11:12 robballantyne

Created the PR:

https://github.com/ai-dock/comfyui/pull/18

berkelmas avatar Dec 12 '23 13:12 berkelmas