Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,11 @@ from kolors.models import unet_2d_condition
|
|
10 |
from diffusers import AutoencoderKL, EulerDiscreteScheduler, UNet2DConditionModel
|
11 |
import gradio as gr
|
12 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
device = "cuda"
|
15 |
ckpt_dir = snapshot_download(repo_id="Kwai-Kolors/Kolors")
|
@@ -53,6 +58,24 @@ pipe_i2i.load_ip_adapter(f'{ckpt_IPA_dir}' , subfolder="", weight_name=["ip_adap
|
|
53 |
MAX_SEED = np.iinfo(np.int32).max
|
54 |
MAX_IMAGE_SIZE = 1024
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
@spaces.GPU
|
57 |
def infer(prompt,
|
58 |
ip_adapter_image = None,
|
@@ -80,14 +103,15 @@ def infer(prompt,
|
|
80 |
height = height,
|
81 |
generator = generator
|
82 |
).images[0]
|
83 |
-
|
|
|
84 |
else:
|
85 |
pipe_i2i.to(device)
|
86 |
image_encoder.to(device)
|
87 |
pipe_i2i.image_encoder = image_encoder
|
88 |
pipe_i2i.set_ip_adapter_scale([ip_adapter_scale])
|
89 |
image = pipe_i2i(
|
90 |
-
prompt=prompt
|
91 |
ip_adapter_image=[ip_adapter_image],
|
92 |
negative_prompt=negative_prompt,
|
93 |
height=height,
|
@@ -97,14 +121,8 @@ def infer(prompt,
|
|
97 |
num_images_per_prompt=1,
|
98 |
generator=generator
|
99 |
).images[0]
|
100 |
-
|
101 |
-
|
102 |
-
examples = [
|
103 |
-
["ไธๅผ ็ข่ซ็็
ง็๏ผๅพฎ่ท๏ผๅ็ฆ๏ผ้ซ่ดจ้๏ผ็ตๅฝฑ๏ผๆฟ็ไธไธช็ๅญ๏ผๅ็โๅฏๅพโ", None, None],
|
104 |
-
["3D anime style, hyperrealistic oil painting, dolphin leaping out of the water", None, None],
|
105 |
-
["็ฉฟ็้ป่ฒTๆค่กซ๏ผไธ้ขไธญๆ็ปฟ่ฒๅคงๅญๅ็โๅฏๅพโ", "image/test_ip.jpg", 0.5],
|
106 |
-
["A cute dog is running", "image/test_ip2.png", 0.5]
|
107 |
-
]
|
108 |
|
109 |
css="""
|
110 |
#col-left {
|
@@ -117,24 +135,33 @@ css="""
|
|
117 |
}
|
118 |
"""
|
119 |
|
120 |
-
def load_description(fp):
|
121 |
-
with open(fp, 'r', encoding='utf-8') as f:
|
122 |
-
content = f.read()
|
123 |
-
return content
|
124 |
-
|
125 |
with gr.Blocks(css=css) as Kolors:
|
126 |
-
gr.HTML(load_description("assets/title.md"))
|
127 |
with gr.Row():
|
128 |
with gr.Column(elem_id="col-left"):
|
129 |
with gr.Row():
|
130 |
-
|
131 |
-
label="
|
132 |
-
placeholder="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
lines=2
|
134 |
)
|
135 |
with gr.Row():
|
136 |
ip_adapter_image = gr.Image(label="Image Prompt (optional)", type="pil")
|
137 |
-
with gr.
|
138 |
negative_prompt = gr.Textbox(
|
139 |
label="Negative prompt",
|
140 |
placeholder="Enter a negative prompt",
|
@@ -188,23 +215,24 @@ with gr.Blocks(css=css) as Kolors:
|
|
188 |
value=0.5,
|
189 |
)
|
190 |
with gr.Row():
|
191 |
-
run_button = gr.Button("
|
192 |
|
193 |
with gr.Column(elem_id="col-right"):
|
194 |
result = gr.Image(label="Result", show_label=False)
|
195 |
-
|
196 |
-
with gr.Row():
|
197 |
-
gr.Examples(
|
198 |
-
fn = infer,
|
199 |
-
examples = examples,
|
200 |
-
inputs = [prompt, ip_adapter_image, ip_adapter_scale],
|
201 |
-
outputs = [result]
|
202 |
-
)
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
run_button.click(
|
205 |
-
fn
|
206 |
-
inputs
|
207 |
-
outputs
|
208 |
)
|
209 |
|
210 |
Kolors.queue().launch(debug=True)
|
|
|
10 |
from diffusers import AutoencoderKL, EulerDiscreteScheduler, UNet2DConditionModel
|
11 |
import gradio as gr
|
12 |
import numpy as np
|
13 |
+
from huggingface_hub import InferenceClient
|
14 |
+
import os
|
15 |
+
|
16 |
+
# Cohere ๋ชจ๋ธ ์ด๊ธฐํ
|
17 |
+
client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
18 |
|
19 |
device = "cuda"
|
20 |
ckpt_dir = snapshot_download(repo_id="Kwai-Kolors/Kolors")
|
|
|
58 |
MAX_SEED = np.iinfo(np.int32).max
|
59 |
MAX_IMAGE_SIZE = 1024
|
60 |
|
61 |
+
def call_api(content, system_message, max_tokens=1000, temperature=0.7, top_p=0.95):
|
62 |
+
messages = [{"role": "system", "content": system_message}, {"role": "user", "content": content}]
|
63 |
+
response = client.chat_completion(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
64 |
+
return response.choices[0].message['content']
|
65 |
+
|
66 |
+
def generate_prompt(korean_prompt):
|
67 |
+
system_message = """
|
68 |
+
Given the following description in Korean,
|
69 |
+
translate and generate a concise English prompt suitable for a Stable Diffusion model.
|
70 |
+
The prompt should be focused, descriptive,
|
71 |
+
and contain specific keywords or phrases that will help guide the image generation process.
|
72 |
+
Use simple and descriptive language, avoiding unnecessary words.
|
73 |
+
Ensure the output is in English and follows the format typically used in Stable Diffusion prompts.
|
74 |
+
The description is: [Insert Korean description here]
|
75 |
+
"""
|
76 |
+
optimized_prompt = call_api(korean_prompt, system_message)
|
77 |
+
return optimized_prompt # ์ต์ ํ๋ ํ๋กฌํํธ ๋ฐํ
|
78 |
+
|
79 |
@spaces.GPU
|
80 |
def infer(prompt,
|
81 |
ip_adapter_image = None,
|
|
|
103 |
height = height,
|
104 |
generator = generator
|
105 |
).images[0]
|
106 |
+
image.save("generated_image.jpg") # ํ์ผ ํ์ฅ์๋ฅผ .jpg๋ก ๋ณ๊ฒฝ
|
107 |
+
return image, "generated_image.jpg"
|
108 |
else:
|
109 |
pipe_i2i.to(device)
|
110 |
image_encoder.to(device)
|
111 |
pipe_i2i.image_encoder = image_encoder
|
112 |
pipe_i2i.set_ip_adapter_scale([ip_adapter_scale])
|
113 |
image = pipe_i2i(
|
114 |
+
prompt=prompt,
|
115 |
ip_adapter_image=[ip_adapter_image],
|
116 |
negative_prompt=negative_prompt,
|
117 |
height=height,
|
|
|
121 |
num_images_per_prompt=1,
|
122 |
generator=generator
|
123 |
).images[0]
|
124 |
+
image.save("generated_image.jpg") # ํ์ผ ํ์ฅ์๋ฅผ .jpg๋ก ๋ณ๊ฒฝ
|
125 |
+
return image, "generated_image.jpg"
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
css="""
|
128 |
#col-left {
|
|
|
135 |
}
|
136 |
"""
|
137 |
|
|
|
|
|
|
|
|
|
|
|
138 |
with gr.Blocks(css=css) as Kolors:
|
|
|
139 |
with gr.Row():
|
140 |
with gr.Column(elem_id="col-left"):
|
141 |
with gr.Row():
|
142 |
+
korean_prompt = gr.Textbox(
|
143 |
+
label="ํ๊ตญ์ด ํ๋กฌํํธ ์
๋ ฅ",
|
144 |
+
placeholder="ํ๊ตญ์ด๋ก ์ํ๋ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์",
|
145 |
+
lines=2
|
146 |
+
)
|
147 |
+
with gr.Row():
|
148 |
+
generate_prompt_button = gr.Button("Generate Prompt")
|
149 |
+
with gr.Row():
|
150 |
+
optimized_prompt = gr.Textbox(
|
151 |
+
label="์ต์ ํ๋ ํ๋กฌํํธ ์์ฑ",
|
152 |
+
placeholder=" ",
|
153 |
+
lines=2,
|
154 |
+
interactive=False
|
155 |
+
)
|
156 |
+
with gr.Row():
|
157 |
+
generated_prompt = gr.Textbox(
|
158 |
+
label="ํ๋กฌํํธ ์
๋ ฅ",
|
159 |
+
placeholder="์ด๋ฏธ์ง ์์ฑ์ ์ฌ์ฉํ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์",
|
160 |
lines=2
|
161 |
)
|
162 |
with gr.Row():
|
163 |
ip_adapter_image = gr.Image(label="Image Prompt (optional)", type="pil")
|
164 |
+
with gr.Row(visible=False): # Advanced Settings ์จ๊น
|
165 |
negative_prompt = gr.Textbox(
|
166 |
label="Negative prompt",
|
167 |
placeholder="Enter a negative prompt",
|
|
|
215 |
value=0.5,
|
216 |
)
|
217 |
with gr.Row():
|
218 |
+
run_button = gr.Button("Generate Image")
|
219 |
|
220 |
with gr.Column(elem_id="col-right"):
|
221 |
result = gr.Image(label="Result", show_label=False)
|
222 |
+
download_button = gr.File(label="Download Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
|
224 |
+
# ์ต์ ํ๋ ํ๋กฌํํธ ์์ฑ ๋ฐ ๊ฒฐ๊ณผ ํ์
|
225 |
+
generate_prompt_button.click(
|
226 |
+
fn=generate_prompt,
|
227 |
+
inputs=[korean_prompt],
|
228 |
+
outputs=[optimized_prompt]
|
229 |
+
)
|
230 |
+
|
231 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ฐ ๋ค์ด๋ก๋ ํ์ผ ๊ฒฝ๋ก ์ค์
|
232 |
run_button.click(
|
233 |
+
fn=infer,
|
234 |
+
inputs=[generated_prompt, ip_adapter_image, ip_adapter_scale, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
235 |
+
outputs=[result, download_button]
|
236 |
)
|
237 |
|
238 |
Kolors.queue().launch(debug=True)
|