File size: 6,551 Bytes
4adca93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import pprint
import random
import re
from io import BytesIO
from typing import Union

import boto3
import requests

s3 = boto3.client("s3")
import io
import urllib.request

from PIL import Image

black_list = {"alphonse mucha": "", "adolphe bouguereau": ""}
pp = pprint.PrettyPrinter(indent=4)
Avatar = [
    {
        "avatarName": "niomi",
        "codename": "1jMGp1kFkG",
        "avatarImage": "https://comic-assets.s3.ap-south-1.amazonaws.com/7_char_assets/niyomi_1jMGp1kFkG.png",
        "extraPrompt": "1jMGp1kFkG  girl",
    },
    {
        "avatarName": "riya",
        "codename": "vW6AUQtoaY",
        "avatarImage": "https://comic-assets.s3.ap-south-1.amazonaws.com/12_char_assets/riya_vW6AUQtoaY.png",
        "extraPrompt": "vW6AUQtoaY girl",
    },
    {
        "avatarName": "rajveer",
        "codename": "fSLF0OPkBw",
        "avatarImage": "https://comic-assets.s3.ap-south-1.amazonaws.com/12_character_assets/rajveer_fSLF0OPkBw.png",
        "extraPrompt": "fSLF0OPkBw guy",
    },
    {
        "avatarName": "bheem",
        "codename": "HL79CB3ODZ",
        "avatarImage": "https://comic-assets.s3.ap-south-1.amazonaws.com/7_char_assets/scene01001_1.png",
        "extraPrompt": "HL79CB3ODZ boy",
    },
    {
        "avatarName": "chutki",
        "codename": "SJ7JVIS9M7",
        "avatarImage": "https://comic-assets.s3.ap-south-1.amazonaws.com/7_char_assets/14.png",
        "extraPrompt": "SJ7JVIS9M7 girl",
    },
]

webhook_url = (
    "https://hooks.slack.com/services/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"
)
error_webhook = (
    "https://hooks.slack.com/services/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"
)

characterSheets = [
    "character+sheets/1.1.png",
    "character+sheets/10.1.png",
    "character+sheets/11.1.png",
    "character+sheets/12.1.png",
    "character+sheets/13.1.png",
    "character+sheets/14.1.png",
    "character+sheets/16.1.png",
    "character+sheets/17.1.png",
    "character+sheets/18.1.png",
    "character+sheets/19.1.png",
    "character+sheets/2.1.png",
    "character+sheets/20.1.png",
    "character+sheets/21.1.png",
    "character+sheets/22.1.png",
    "character+sheets/23.1.png",
    "character+sheets/24.1.png",
    "character+sheets/25.1.png",
    "character+sheets/26.1.png",
    "character+sheets/27.1.png",
    "character+sheets/28.1.png",
    "character+sheets/29.1.png",
    "character+sheets/3.1.png",
    "character+sheets/30.1.png",
    "character+sheets/31.1.png",
    "character+sheets/32.1.png",
    "character+sheets/33.1.png",
    "character+sheets/34.1.png",
    "character+sheets/35.1.png",
    "character+sheets/36.1.png",
    "character+sheets/38.1.png",
    "character+sheets/39.1.png",
    "character+sheets/4.1.png",
    "character+sheets/40.1.png",
    "character+sheets/42.1.png",
    "character+sheets/43.1.png",
    "character+sheets/44.1.png",
    "character+sheets/45.1.png",
    "character+sheets/46.1.png",
    "character+sheets/47.1.png",
    "character+sheets/48.1.png",
    "character+sheets/49.1.png",
    "character+sheets/5.1.png",
    "character+sheets/50.1.png",
    "character+sheets/51.1.png",
    "character+sheets/52.1.png",
    "character+sheets/53.1.png",
    "character+sheets/54.1.png",
    "character+sheets/55.1.png",
    "character+sheets/56.1.png",
    "character+sheets/57.1.png",
    "character+sheets/58.1.png",
    "character+sheets/59.1.png",
    "character+sheets/60.1.png",
    "character+sheets/61.1.png",
    "character+sheets/62.1.png",
    "character+sheets/63.1.png",
    "character+sheets/64.1.png",
    "character+sheets/65.1.png",
    "character+sheets/66.1.png",
    "character+sheets/7.1.png",
    "character+sheets/8.1.png",
    "character+sheets/9.1.png",
]


def add_code_names(sentence):
    array_of_objects = Avatar

    for obj in array_of_objects:
        sentence = (
            re.sub(
                r"\b" + obj["avatarName"] + r"\b",
                obj["extraPrompt"],
                sentence,
                flags=re.IGNORECASE,
            )
            + " "
        )
    print(sentence)
    return sentence


def upload_images(images, processName: str, taskId: str):
    imageUrls = []
    for i, image in enumerate(images):
        # img_io = BytesIO()
        # image.save(img_io, "JPEG", quality=70)
        # img_io.seek(0)
        # key = "crecoAI/{}{}_{}.png".format(taskId, processName, i)
        # t = s3.put_object(
        #     Bucket="comic-assets", Key=key, Body=img_io.getvalue(), ACL="public-read"
        # )
        # print("uploading done to s3", key, t)
        imageUrls.append(
            "https://comic-assets.s3.ap-south-1.amazonaws.com/crecoAI/{}{}_{}.png".format(
                taskId, processName, i
            )
        )

    print({"promptImages": imageUrls})

    return imageUrls


# def upload_image(image: Union[Image.Image, BytesIO], out_path):
    # if type(image) is Image.Image:
    #     buffer = io.BytesIO()
    #     image.save(buffer, format="PNG")
    #     image = buffer

    # image.seek(0)
    # s3.upload_fileobj(image, "comic-assets", out_path, ExtraArgs={"ACL": "public-read"})
    # image.close()


def download_image(url) -> Image.Image:
    response = requests.get(url)
    return Image.open(BytesIO(response.content)).convert("RGB")


def pickPoses():
    random_images = random.sample(characterSheets, 4)
    poses = []
    prefix = "https://comic-assets.s3.ap-south-1.amazonaws.com/"

    # Use list comprehension to add prefix to all elements in the array
    random_images_with_prefix = [prefix + img for img in random_images]

    print(random_images_with_prefix)
    for imageUrl in random_images_with_prefix:
        # Download and resize the image
        init_image = download_image(imageUrl).resize((512, 512))

        # Open the pose image
        imageUrlPose = imageUrl
        # print(imageUrl)
        input_image_bytes = read_url(imageUrlPose)
        # print(input_image_bytes)
        pose_image = Image.open(io.BytesIO(input_image_bytes)).convert("RGB")
        # print(pose_image)
        pose_image = pose_image.resize((512, 512))
        # print(pose_image)
        # Append the result to the poses array
        poses.append(pose_image)

    return poses


def construct_default_s3_url(key):
    return "https://comic-assets.s3.ap-south-1.amazonaws.com/" + key


def read_url(url: str):
    with urllib.request.urlopen(url) as u:
        return u.read()


def disable_safety_checker(pipe):
    def dummy(images, **kwargs):
        return images, False

    pipe.safety_checker = None