ssboost commited on
Commit
d29e6b3
ยท
verified ยท
1 Parent(s): fbec80d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -32
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
- return image
 
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
- return image
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
- prompt = gr.Textbox(
131
- label="Prompt",
132
- placeholder="Enter your prompt",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  lines=2
134
  )
135
  with gr.Row():
136
  ip_adapter_image = gr.Image(label="Image Prompt (optional)", type="pil")
137
- with gr.Accordion("Advanced Settings", open=False):
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("Run")
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 = infer,
206
- inputs = [prompt, ip_adapter_image, ip_adapter_scale, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
207
- outputs = [result]
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)