Skip to content

Commit

Permalink
feat: base64 image output (#8)
Browse files Browse the repository at this point in the history
* base64 image output

added changes for base64 image output when no s3 bucket is set. also added basic 'custom' directory for adding models/custom nodes for being mounted, updated doc, etc. :)

* pr alterations
  • Loading branch information
Dekita authored Oct 16, 2023
1 parent fe398ef commit 76bf0b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ RUN pip3 install --no-cache-dir torch torchvision torchaudio --index-url https:/
# Install runpod
RUN pip3 install runpod requests

# Download the models
# Download models to include in image. (not required if including other models below)
RUN wget -O models/checkpoints/sd_xl_base_1.0.safetensors https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
RUN wget -O models/checkpoints/sdxl_vae.safetensors https://huggingface.co/stabilityai/sdxl-vae/resolve/main/sdxl_vae.safetensors

# # Add the models
# # Example for adding specific models into image
# ADD models/checkpoints/sd_xl_base_1.0.safetensors models/checkpoints/
# ADD models/checkpoints/sdxl_vae.safetensors models/checkpoints/

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
## Features

* Run any [ComfyUI](https://github.com/comfyanonymous/ComfyUI) workflow to generate an image
* Generated images are uploaded to AWS S3
* Generated images are uploaded to AWS S3 (or returned as base64 if no bucket set)
* Build-in checkpoint: [sd_xl_base_1.0.safetensors](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0)
* Build-in VAE: [sdxl_vae.safetensors](https://huggingface.co/stabilityai/sdxl-vae/)
* Based on [Ubuntu + NVIDIA CUDA](https://hub.docker.com/r/nvidia/cuda)
Expand Down Expand Up @@ -167,7 +167,6 @@ To run the Docker image on Windows, we need to have WSL2 and a Linux distro (lik

- Add your user to the `docker` group, so that you can use Docker without `sudo`: `sudo usermod -aG docker $USER`


## Automatically deploy to Docker hub with Github Actions

The repo contains two workflows that publishes the image to Docker hub using Github Actions:
Expand Down
27 changes: 21 additions & 6 deletions src/rp_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import os
import requests
import base64

# Time to wait between API check attempts in milliseconds
COMFY_API_AVAILABLE_INTERVAL_MS = 50
Expand Down Expand Up @@ -83,6 +84,15 @@ def get_history(prompt_id):
return json.loads(response.read())


def base64_encode(img_path):
"""
Returns base64 encoded image.
"""
with open(img_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string.decode("utf-8")


def handler(job):
"""
The main function that handles a job of generating an image.
Expand Down Expand Up @@ -160,18 +170,23 @@ def handler(job):

print(f"runpod-worker-comfy - image generation is done")

# expected image output folder
local_image_path = f"{COMFY_OUTPUT_PATH}/{output_images}"
# The image is in the output folder
if os.path.exists(f"{COMFY_OUTPUT_PATH}/{output_images}"):
if os.path.exists(local_image_path):
print("runpod-worker-comfy - the image exists in the output folder")
image_url = rp_upload.upload_image(
job["id"], f"{COMFY_OUTPUT_PATH}/{output_images}"
)
return {"status": "success", "message": f"{image_url}"}
image_url = rp_upload.upload_image(job["id"], local_image_path)
return_base64 = "simulated_uploaded/" in image_url
return_output = f"{image_url}" if not return_base64 else base64_encode(local_image_path)
return {
"status": "success",
"message": return_output,
}
else:
print("runpod-worker-comfy - the image does not exist in the output folder")
return {
"status": "error",
"message": f"the image does not exist in the specified output folder: {COMFY_OUTPUT_PATH}/{output_images}",
"message": f"the image does not exist in the specified output folder: {local_image_path}",
}


Expand Down

0 comments on commit 76bf0b1

Please sign in to comment.