|
| 1 | +import argparse |
| 2 | +from concurrent import futures |
| 3 | +import functools |
| 4 | +from io import BytesIO |
| 5 | +import numpy as np |
| 6 | +from PIL import Image |
| 7 | +import requests |
| 8 | +from tqdm import tqdm |
| 9 | + |
| 10 | + |
| 11 | +_PROMPTS = [ |
| 12 | + "Labrador in the style of Hokusai", |
| 13 | + "Painting of a squirrel skating in New York", |
| 14 | + "HAL-9000 in the style of Van Gogh", |
| 15 | + "Times Square under water, with fish and a dolphin swimming around", |
| 16 | + "Ancient Roman fresco showing a man working on his laptop", |
| 17 | + "Armchair in the shape of an avocado", |
| 18 | + "Clown astronaut in space, with Earth in the background", |
| 19 | + "A cat sitting on a windowsill", |
| 20 | + "A dog playing fetch in a park", |
| 21 | + "A city skyline at night", |
| 22 | + "A field of flowers in bloom", |
| 23 | + "A tropical beach with palm trees", |
| 24 | + "A snowy mountain range", |
| 25 | + "A waterfall cascading into a pool", |
| 26 | + "A forest at sunset", |
| 27 | + "A desert landscape with cacti", |
| 28 | + "A volcano erupting", |
| 29 | + "A lightning storm in the distance", |
| 30 | + "A rainbow over a rainbow", |
| 31 | + "A unicorn grazing in a meadow", |
| 32 | + "A dragon flying through the sky", |
| 33 | + "A mermaid swimming in the ocean", |
| 34 | + "A robot walking down the street", |
| 35 | + "A UFO landing in a field", |
| 36 | + "A portal to another dimension", |
| 37 | + "A time traveler from the future", |
| 38 | + "A talking cat", |
| 39 | + "A bowl of fruit on a table", |
| 40 | + "A group of friends laughing", |
| 41 | + "A family sitting down for dinner", |
| 42 | + "A couple kissing in the rain", |
| 43 | + "A child playing with a toy", |
| 44 | + "A musician playing an instrument", |
| 45 | + "A painter painting a picture", |
| 46 | + "A writer writing a book", |
| 47 | + "A scientist conducting an experiment", |
| 48 | + "A construction worker building a house", |
| 49 | + "A doctor operating on a patient", |
| 50 | + "A teacher teaching a class", |
| 51 | + "A police officer arresting a suspect", |
| 52 | + "A firefighter putting out a fire", |
| 53 | + "A soldier fighting in a war", |
| 54 | + "A farmer working in a field", |
| 55 | + "A pilot flying a plane", |
| 56 | + "An astronaut in space", |
| 57 | + "A unicorn eating a rainbow" |
| 58 | +] |
| 59 | + |
| 60 | + |
| 61 | +def send_request_and_receive_image(prompt: str, url: str) -> BytesIO: |
| 62 | + """Sends a single prompt request and returns the Image.""" |
| 63 | + try: |
| 64 | + inputs = "%20".join(prompt.split(" ")) |
| 65 | + resp = requests.get(f"{url}?prompt={inputs}") |
| 66 | + resp.raise_for_status() |
| 67 | + return BytesIO(resp.content) |
| 68 | + except requests.RequestException as e: |
| 69 | + print(f"An error occurred while sending the request: {e}") |
| 70 | + |
| 71 | + |
| 72 | +def image_grid(imgs, rows, cols): |
| 73 | + w, h = imgs[0].size |
| 74 | + grid = Image.new("RGB", size=(cols * w, rows * h)) |
| 75 | + for i, img in enumerate(imgs): |
| 76 | + grid.paste(img, box=(i % cols * w, i // cols * h)) |
| 77 | + return grid |
| 78 | + |
| 79 | + |
| 80 | +def send_requests(num_requests: int, batch_size: int, save_pictures: bool, |
| 81 | + url: str = "http://localhost:8000/imagine"): |
| 82 | + """Sends a list of requests and processes the responses.""" |
| 83 | + print("num_requests: ", num_requests) |
| 84 | + print("batch_size: ", batch_size) |
| 85 | + print("url: ", url) |
| 86 | + print("save_pictures: ", save_pictures) |
| 87 | + |
| 88 | + prompts = _PROMPTS |
| 89 | + if num_requests > len(_PROMPTS): |
| 90 | + # Repeat until larger than num_requests |
| 91 | + prompts = _PROMPTS * int(np.ceil(num_requests / len(_PROMPTS))) |
| 92 | + |
| 93 | + prompts = np.random.choice( |
| 94 | + prompts, num_requests, replace=False) |
| 95 | + |
| 96 | + with futures.ThreadPoolExecutor(max_workers=batch_size) as executor: |
| 97 | + raw_images = list( |
| 98 | + tqdm( |
| 99 | + executor.map( |
| 100 | + functools.partial(send_request_and_receive_image, url=url), |
| 101 | + prompts, |
| 102 | + ), |
| 103 | + total=len(prompts), |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + if save_pictures: |
| 108 | + print("Saving pictures to diffusion_results.png") |
| 109 | + images = [Image.open(raw_image) for raw_image in raw_images] |
| 110 | + grid = image_grid(images, 2, num_requests // 2) |
| 111 | + grid.save("./diffusion_results.png") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + parser = argparse.ArgumentParser(description="Sends requests to Diffusion.") |
| 116 | + parser.add_argument( |
| 117 | + "--num_requests", help="Number of requests to send.", |
| 118 | + default=8) |
| 119 | + parser.add_argument( |
| 120 | + "--batch_size", help="The number of requests to send at a time.", |
| 121 | + default=8) |
| 122 | + parser.add_argument( |
| 123 | + "--save_pictures", default=False, action="store_true", |
| 124 | + help="Whether to save the generated pictures to disk.") |
| 125 | + parser.add_argument( |
| 126 | + "--ip", help="The IP address to send the requests to.") |
| 127 | + |
| 128 | + args = parser.parse_args() |
| 129 | + |
| 130 | + send_requests( |
| 131 | + num_requests=int(args.num_requests), batch_size=int(args.batch_size), |
| 132 | + save_pictures=bool(args.save_pictures)) |
0 commit comments