ginipick commited on
Commit
459b9da
โ€ข
1 Parent(s): 24f2920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -30
app.py CHANGED
@@ -7,11 +7,21 @@ from PIL import Image
7
  import spaces
8
  from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
9
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
10
-
11
  from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
12
  import copy
13
  import random
14
  import time
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # Load LoRAs from JSON file
17
  with open('loras.json', 'r') as f:
@@ -48,9 +58,9 @@ class calculateDuration:
48
 
49
  def update_selection(evt: gr.SelectData, width, height):
50
  selected_lora = loras[evt.index]
51
- new_placeholder = f"Type a prompt for {selected_lora['title']}"
52
  lora_repo = selected_lora["repo"]
53
- updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) โœจ"
54
  if "aspect" in selected_lora:
55
  if selected_lora["aspect"] == "portrait":
56
  width = 768
@@ -73,7 +83,7 @@ def update_selection(evt: gr.SelectData, width, height):
73
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress):
74
  pipe.to("cuda")
75
  generator = torch.Generator(device="cuda").manual_seed(seed)
76
- with calculateDuration("Generating image"):
77
  # Generate image
78
  for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
79
  prompt=prompt_mash,
@@ -90,33 +100,36 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
90
 
91
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
92
  if selected_index is None:
93
- raise gr.Error("You must select a LoRA before proceeding.")
 
 
 
94
  selected_lora = loras[selected_index]
95
  lora_path = selected_lora["repo"]
96
  trigger_word = selected_lora["trigger_word"]
97
  if(trigger_word):
98
  if "trigger_position" in selected_lora:
99
  if selected_lora["trigger_position"] == "prepend":
100
- prompt_mash = f"{trigger_word} {prompt}"
101
  else:
102
- prompt_mash = f"{prompt} {trigger_word}"
103
  else:
104
- prompt_mash = f"{trigger_word} {prompt}"
105
  else:
106
- prompt_mash = prompt
107
 
108
- with calculateDuration("Unloading LoRA"):
109
  pipe.unload_lora_weights()
110
 
111
  # Load LoRA weights
112
- with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
113
  if "weights" in selected_lora:
114
  pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
115
  else:
116
  pipe.load_lora_weights(lora_path)
117
 
118
  # Set random seed for reproducibility
119
- with calculateDuration("Randomizing seed"):
120
  if randomize_seed:
121
  seed = random.randint(0, MAX_SEED)
122
 
@@ -129,9 +142,10 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
129
  step_counter+=1
130
  final_image = image
131
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
132
- yield image, seed, gr.update(value=progress_bar, visible=True)
133
 
134
- yield final_image, seed, gr.update(value=progress_bar, visible=False)
 
135
 
136
  def get_huggingface_safetensors(link):
137
  split_link = link.split("/")
@@ -216,48 +230,52 @@ footer {
216
  }
217
  """
218
 
 
219
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as app:
220
 
221
  selected_index = gr.State(None)
222
  with gr.Row():
223
  with gr.Column(scale=3):
224
- prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
225
  with gr.Column(scale=1, elem_id="gen_column"):
226
- generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
227
  with gr.Row():
228
  with gr.Column():
229
  selected_info = gr.Markdown("")
230
  gallery = gr.Gallery(
231
  [(item["image"], item["title"]) for item in loras],
232
- label="LoRA Gallery",
233
  allow_preview=False,
234
  columns=3,
235
  elem_id="gallery"
236
  )
237
  with gr.Group():
238
- custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="multimodalart/vintage-ads-flux")
239
- gr.Markdown("[Check the list of FLUX LoRas](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
240
  custom_lora_info = gr.HTML(visible=False)
241
- custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
242
  with gr.Column():
243
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
244
- result = gr.Image(label="Generated Image")
 
 
245
 
246
  with gr.Row():
247
- with gr.Accordion("Advanced Settings", open=False):
248
  with gr.Column():
249
  with gr.Row():
250
- cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
251
- steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
252
 
253
  with gr.Row():
254
- width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
255
- height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
256
 
257
  with gr.Row():
258
- randomize_seed = gr.Checkbox(True, label="Randomize seed")
259
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
260
- lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
 
261
 
262
  gallery.select(
263
  update_selection,
@@ -273,11 +291,12 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as app:
273
  remove_custom_lora,
274
  outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
275
  )
 
276
  gr.on(
277
  triggers=[generate_button.click, prompt.submit],
278
  fn=run_lora,
279
  inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
280
- outputs=[result, seed, progress_bar]
281
  )
282
 
283
  app.queue()
 
7
  import spaces
8
  from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
9
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
 
10
  from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
11
  import copy
12
  import random
13
  import time
14
+ from transformers import pipeline
15
+
16
+ # ๋ฒˆ์—ญ ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
17
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
18
+
19
+ # ํ”„๋กฌํ”„ํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ์ถ”๊ฐ€
20
+ def process_prompt(prompt):
21
+ if any('\u3131' <= char <= '\u3163' or '\uac00' <= char <= '\ud7a3' for char in prompt):
22
+ translated = translator(prompt)[0]['translation_text']
23
+ return prompt, translated
24
+ return prompt, prompt
25
 
26
  # Load LoRAs from JSON file
27
  with open('loras.json', 'r') as f:
 
58
 
59
  def update_selection(evt: gr.SelectData, width, height):
60
  selected_lora = loras[evt.index]
61
+ new_placeholder = f"{selected_lora['title']}๋ฅผ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"
62
  lora_repo = selected_lora["repo"]
63
+ updated_text = f"### ์„ ํƒ๋จ: [{lora_repo}](https://huggingface.co/{lora_repo}) โœจ"
64
  if "aspect" in selected_lora:
65
  if selected_lora["aspect"] == "portrait":
66
  width = 768
 
83
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress):
84
  pipe.to("cuda")
85
  generator = torch.Generator(device="cuda").manual_seed(seed)
86
+ with calculateDuration("์ด๋ฏธ์ง€ ์ƒ์„ฑ"):
87
  # Generate image
88
  for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
89
  prompt=prompt_mash,
 
100
 
101
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
102
  if selected_index is None:
103
+ raise gr.Error("์ง„ํ–‰ํ•˜๊ธฐ ์ „์— LoRA๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
104
+
105
+ original_prompt, english_prompt = process_prompt(prompt)
106
+
107
  selected_lora = loras[selected_index]
108
  lora_path = selected_lora["repo"]
109
  trigger_word = selected_lora["trigger_word"]
110
  if(trigger_word):
111
  if "trigger_position" in selected_lora:
112
  if selected_lora["trigger_position"] == "prepend":
113
+ prompt_mash = f"{trigger_word} {english_prompt}"
114
  else:
115
+ prompt_mash = f"{english_prompt} {trigger_word}"
116
  else:
117
+ prompt_mash = f"{trigger_word} {english_prompt}"
118
  else:
119
+ prompt_mash = english_prompt
120
 
121
+ with calculateDuration("LoRA ์–ธ๋กœ๋“œ"):
122
  pipe.unload_lora_weights()
123
 
124
  # Load LoRA weights
125
+ with calculateDuration(f"{selected_lora['title']}์˜ LoRA ๊ฐ€์ค‘์น˜ ๋กœ๋“œ"):
126
  if "weights" in selected_lora:
127
  pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
128
  else:
129
  pipe.load_lora_weights(lora_path)
130
 
131
  # Set random seed for reproducibility
132
+ with calculateDuration("์‹œ๋“œ ๋ฌด์ž‘์œ„ํ™”"):
133
  if randomize_seed:
134
  seed = random.randint(0, MAX_SEED)
135
 
 
142
  step_counter+=1
143
  final_image = image
144
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
145
+ yield image, seed, gr.update(value=progress_bar, visible=True), original_prompt, english_prompt
146
 
147
+ yield final_image, seed, gr.update(value=progress_bar, visible=False), original_prompt, english_prompt
148
+
149
 
150
  def get_huggingface_safetensors(link):
151
  split_link = link.split("/")
 
230
  }
231
  """
232
 
233
+
234
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as app:
235
 
236
  selected_index = gr.State(None)
237
  with gr.Row():
238
  with gr.Column(scale=3):
239
+ prompt = gr.Textbox(label="ํ”„๋กฌํ”„ํŠธ", lines=1, placeholder="LoRA๋ฅผ ์„ ํƒํ•œ ํ›„ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (ํ•œ๊ธ€ ๋˜๋Š” ์˜์–ด)")
240
  with gr.Column(scale=1, elem_id="gen_column"):
241
+ generate_button = gr.Button("์ƒ์„ฑ", variant="primary", elem_id="gen_btn")
242
  with gr.Row():
243
  with gr.Column():
244
  selected_info = gr.Markdown("")
245
  gallery = gr.Gallery(
246
  [(item["image"], item["title"]) for item in loras],
247
+ label="LoRA ๊ฐค๋Ÿฌ๋ฆฌ",
248
  allow_preview=False,
249
  columns=3,
250
  elem_id="gallery"
251
  )
252
  with gr.Group():
253
+ custom_lora = gr.Textbox(label="์ปค์Šคํ…€ LoRA", info="LoRA Hugging Face ๊ฒฝ๋กœ", placeholder="multimodalart/vintage-ads-flux")
254
+ gr.Markdown("[FLUX LoRA ๋ชฉ๋ก ํ™•์ธ](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
255
  custom_lora_info = gr.HTML(visible=False)
256
+ custom_lora_button = gr.Button("์ปค์Šคํ…€ LoRA ์ œ๊ฑฐ", visible=False)
257
  with gr.Column():
258
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
259
+ result = gr.Image(label="์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€")
260
+ original_prompt_display = gr.Textbox(label="์›๋ณธ ํ”„๋กฌํ”„ํŠธ")
261
+ english_prompt_display = gr.Textbox(label="์˜์–ด ํ”„๋กฌํ”„ํŠธ")
262
 
263
  with gr.Row():
264
+ with gr.Accordion("๊ณ ๊ธ‰ ์„ค์ •", open=False):
265
  with gr.Column():
266
  with gr.Row():
267
+ cfg_scale = gr.Slider(label="CFG ์Šค์ผ€์ผ", minimum=1, maximum=20, step=0.5, value=3.5)
268
+ steps = gr.Slider(label="์Šคํ…", minimum=1, maximum=50, step=1, value=28)
269
 
270
  with gr.Row():
271
+ width = gr.Slider(label="๋„ˆ๋น„", minimum=256, maximum=1536, step=64, value=1024)
272
+ height = gr.Slider(label="๋†’์ด", minimum=256, maximum=1536, step=64, value=1024)
273
 
274
  with gr.Row():
275
+ randomize_seed = gr.Checkbox(True, label="์‹œ๋“œ ๋ฌด์ž‘์œ„ํ™”")
276
+ seed = gr.Slider(label="์‹œ๋“œ", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
277
+ lora_scale = gr.Slider(label="LoRA ์Šค์ผ€์ผ", minimum=0, maximum=3, step=0.01, value=0.95)
278
+
279
 
280
  gallery.select(
281
  update_selection,
 
291
  remove_custom_lora,
292
  outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
293
  )
294
+
295
  gr.on(
296
  triggers=[generate_button.click, prompt.submit],
297
  fn=run_lora,
298
  inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
299
+ outputs=[result, seed, progress_bar, original_prompt_display, english_prompt_display]
300
  )
301
 
302
  app.queue()