File size: 5,929 Bytes
db315f2
 
 
 
 
 
 
 
 
 
 
 
4e5f4b8
db315f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0c0d4
 
 
db315f2
 
 
 
 
 
 
 
 
 
 
 
 
9d0c0d4
db315f2
 
 
 
 
 
 
 
9d0c0d4
db315f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0c0d4
db315f2
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0c0d4
db315f2
 
 
 
 
4e5f4b8
 
 
db315f2
 
 
9d0c0d4
 
 
db315f2
 
 
 
 
 
 
 
 
 
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
"""
이미지 생성 관련 기능을 담당하는 모듈
"""

import replicate
from config import (
    imageStyleMap,
    imageModelMap,
    HIGH_PRICE_OPTION,
    LOW_PRICE_OPTION,
    HIGH_PRICE,
    LOW_PRICE,
    CUSTOM,
)


def to_string(output):
    """
    output 을 하나의 문자열로 합치기.
    """
    combined_result = ""
    for item in output:
        combined_result += str(item)
    return combined_result


def genFluxPrompt(userPrompt, additionalComment, ratio, imageStyle):
    """
    userPrompt 와 additionalComment 를 받아서 비율에 맞는 이미지 생성을 위한 prompt 생성.
    """
    imageStyle = imageStyleMap[imageStyle]
    # print("genFluxPrompt ", userPrompt, ratio, imageStyle)
    # The ibm-granite/granite-3.1-8b-instruct model can stream output as it's running.

    additionalComment = (
        f"\n# Important note: {additionalComment}\n" if additionalComment else ""
    )

    prompt = (
        f"Please create a prompt that will generate a best quality image that can express the following sentence in English.(DO NOT INCLUDE ANY KOREAN LANGUAGE.)"
        f"\n# Image generation info\n"
        f"\n- Aspect ratio: {ratio}\n"
        f"\n- Image style: {imageStyle}\n"
        "\n# Description for the image.\n" + userPrompt + additionalComment
    )

    print("genFluxPrompt prompt:", prompt)

    result = replicate.run(
        "anthropic/claude-3.5-haiku",
        input={
            "top_k": 50,
            "top_p": 0.9,
            "prompt": prompt,
            "max_tokens": 256,
            "min_tokens": 0,
            "temperature": 0.6,
            "system_prompt": (
                "You are a professional prompt engineer specializing in creating prompts for txt2img AI model."
                "You always create prompts that can extract the impressive output."
                "Make sure to emphasize the intention for 'Important note:' section in the prompts if it exists."
                "Do not generate negative prompts!"
            ),
            "presence_penalty": 0,
            "frequency_penalty": 0,
        },
    )
    prompt = to_string(result)
    print("genFluxPrompt prompt:", prompt)
    return prompt


def genImage(
    prompt, ratio, imageModelKind, imgWidth, imgHeight, imageNum=1, imageFormat="webp"
):
    """
    prompt 를 받아서 비율에 맞는 이미지 imageNum 만큼 생성.
    """

    imageModel = imageModelMap[imageModelKind]

    if ratio == "custom":
        input = {
            "prompt": prompt,
            "aspect_ratio": ratio,  # gradio radio 버튼에서 선택된 값
            "width": int(imgWidth),
            "height": int(imgHeight),
            "aspect_ratio": "custom",
            "output_format": imageFormat,
            "output_quality": 90,
        }
        # custom 일 때는 비싼 모델로 생성.
        imageModel = imageModelMap[HIGH_PRICE_OPTION]
    else:
        input = {
            "prompt": prompt,
            "aspect_ratio": ratio,  # gradio radio 버튼에서 선택된 값
            "output_format": imageFormat,
            "output_quality": 90,
            "num_outputs": imageNum,
        }

    print("genImage input:", input)

    imageModel = imageModelMap[imageModelKind]
    result = replicate.run(imageModel, input=input)

    print("Generated Image Info:", result)
    # imgUrls = [str(img) for img in result]
    # imgUrl = str(result) if type(result) != list else str(result[0])
    imgUrl = str(result) if not isinstance(result, list) else str(result[0])
    if imageNum > 1:
        imgUrls = [str(img) for img in result]
        print("genImage imgUrls:", imgUrls)
        return imgUrls
    else:
        print("genImage imgUrl:", imgUrl)
        return imgUrl


def reduxImage(prompt, url):
    """
    url 의 이미지를 재생성.

    output = replicate.run(
    "black-forest-labs/flux-1.1-pro",
        input={
            "width": 1024,
            "height": 480,
            "prompt": "one boy and 3 girls hands and hands playing in the school.",
            "aspect_ratio": "custom",
            "image_prompt": "https://replicate.delivery/xezq/CLKVkX1QXeXrMy1tH43zJmCk6RgBszqmQdK45AOUTedCkyUUA/tmpucuderjn.webp",
            "output_format": "webp",
            "output_quality": 80,
            "safety_tolerance": 2,
            "prompt_upsampling": True
        }
    )
    """
    result = replicate.run(
        "black-forest-labs/flux-1.1-pro",
        input={
            "prompt": prompt,
            "aspect_ratio": "16:9",
            "image_prompt": url,
            "output_format": "webp",
            "output_quality": 80,
            "safety_tolerance": 2,  # 1 is most strict and 6 is most permissive
            "prompt_upsampling": True,
        },
    )
    return str(result)


def genPromptAndImage(
    userPrompt,
    additionalComment,
    fluxPrompt,
    ratio,
    imageStyle,
    imageFormat,
    imageModel,
    highImageCnt,
    lowImageCnt,
    imgWidth,
    imgHeight,
):
    """
    fluxPrompt 가 빈 문자열이면 새로운 prompt 를 생성하고,
    빈 문자열이 아니면 기존의 prompt 를 사용해서 이미지 생성.
    """
    print(
        "genPromptAndImage:",
        ratio,
        imageStyle,
        imageFormat,
        imageModel,
        highImageCnt,
        lowImageCnt,
    )

    if ratio == CUSTOM:
        ratio = "custom"

    if fluxPrompt == "":
        fluxPrompt = genFluxPrompt(userPrompt, additionalComment, ratio, imageStyle)

    imgUrl = genImage(
        fluxPrompt, ratio, imageModel, imgWidth, imgHeight, 1, imageFormat
    )

    highImageCnt = int(highImageCnt)
    lowImageCnt = int(lowImageCnt)
    if imageModel == HIGH_PRICE_OPTION:
        highImageCnt += 1
    else:
        lowImageCnt += 1

    totalPrice = highImageCnt * HIGH_PRICE + lowImageCnt * LOW_PRICE
    return fluxPrompt, highImageCnt, lowImageCnt, totalPrice, imgUrl, None