Skip to content

Commit

Permalink
refactor(shortfin_apps): dissolves bytes_to_img into call site
Browse files Browse the repository at this point in the history
  • Loading branch information
bjacobgordon committed Mar 3, 2025
1 parent ba968b9 commit c6c8439
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions shortfin/python/shortfin_apps/sd/simple_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@
}


def bytes_to_img(in_bytes, outputdir, idx=0, width=1024, height=1024):
timestamp = dt.now().strftime("%Y-%m-%d_%H-%M-%S")
image = Image.frombytes(
mode="RGB", size=(width, height), data=base64.b64decode(in_bytes)
)
file_name = f"shortfin_sd_output_{timestamp}_{idx}.png"
new_file_path = save_to_file(image, outputdir, file_name)
print(f"Saved to {new_file_path}")


def get_batched(request, arg, idx):
if isinstance(request[arg], list):
# some args are broadcasted to each prompt, hence overriding idx for single-item entries
Expand Down Expand Up @@ -72,9 +62,21 @@ async def send_request(session: aiohttp.ClientSession, rep, args, data):
width = get_batched(data, "width", idx)
height = get_batched(data, "height", idx)
print("Saving response as image...")
bytes_to_img(
item.encode("utf-8"), args.outputdir, idx, width, height

each_image = Image.frombytes(
mode="RGB",
size=(width, height),
data=base64.b64decode(item.encode("utf-8")),
)

timestamp = dt.now().strftime("%Y-%m-%d_%H-%M-%S")
each_file_name = f"shortfin_sd_output_{timestamp}_{idx}.png"

each_file_path = save_to_file(
each_image, args.outputdir, each_file_name
)

print(f"Saved to {each_file_path}")
latency = end - start
print("Responses processed.")
return latency, len(data["prompt"])
Expand Down

0 comments on commit c6c8439

Please sign in to comment.