vincenthugging commited on
Commit
5b6ee70
1 Parent(s): ad8f2a1

feat: add loras

Browse files
Files changed (2) hide show
  1. app.py +17 -17
  2. loras.json +6 -5
app.py CHANGED
@@ -22,7 +22,6 @@ if access_token:
22
  else:
23
  print("warning: no access token found")
24
 
25
-
26
  # Load LoRAs from JSON file
27
  with open('loras.json', 'r') as f:
28
  loras = json.load(f)
@@ -59,12 +58,12 @@ class calculateDuration:
59
  def update_selection(evt: gr.SelectData, width, height):
60
  selected_lora = loras[evt.index]
61
 
62
- new_placeholder = selected_lora.get('placeholder', f"Type a prompt for {selected_lora['title']}")
63
- example_prompt = selected_lora.get('example_prompt', '')
64
  lora_repo = selected_lora["repo"]
65
  updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
 
66
 
67
- # 分辨率切换
68
  if "aspect" in selected_lora:
69
  if selected_lora["aspect"] == "portrait":
70
  width = 768
@@ -75,12 +74,10 @@ def update_selection(evt: gr.SelectData, width, height):
75
  else:
76
  width = 1024
77
  height = 1024
78
-
79
- gr.Info("LoRA selection updated") # 添加这行来触发 UI 刷新
80
 
81
  return (
82
- gr.update(placeholder=new_placeholder, value=example_prompt),
83
- updated_text,
84
  evt.index,
85
  width,
86
  height,
@@ -105,15 +102,15 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
105
  ):
106
  yield img
107
 
108
- # 执行 run
109
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
110
  if selected_index is None:
111
  raise gr.Error("You must select a LoRA before proceeding.")
112
  selected_lora = loras[selected_index]
 
113
 
114
  lora_path = selected_lora["repo"]
115
  trigger_word = selected_lora["trigger_word"]
116
-
117
  if(trigger_word):
118
  if "trigger_position" in selected_lora:
119
  if selected_lora["trigger_position"] == "prepend":
@@ -127,6 +124,8 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
127
 
128
  with calculateDuration("Unloading LoRA"):
129
  pipe.unload_lora_weights()
 
 
130
 
131
  # Load LoRA weights
132
  with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
@@ -134,6 +133,8 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
134
  pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
135
  else:
136
  pipe.load_lora_weights(lora_path)
 
 
137
 
138
  # Set random seed for reproducibility
139
  with calculateDuration("Randomizing seed"):
@@ -245,19 +246,18 @@ css = '''
245
  .progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
246
  .progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
247
  '''
 
248
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
249
  title = gr.HTML(
250
- """<h1><img src="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png" alt="LoRA"> FLUX LoRA the Explorer</h1>""",
251
  elem_id="title",
252
  )
253
  selected_index = gr.State(None)
254
-
255
  with gr.Row():
256
  with gr.Column(scale=3):
257
- prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
258
  with gr.Column(scale=1, elem_id="gen_column"):
259
  generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
260
-
261
  with gr.Row():
262
  with gr.Column():
263
  selected_info = gr.Markdown("")
@@ -269,11 +269,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
269
  elem_id="gallery"
270
  )
271
  with gr.Group():
272
- custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="XLabs-AI/flux-RealismLora")
273
  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")
274
  custom_lora_info = gr.HTML(visible=False)
275
  custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
276
-
277
  with gr.Accordion("Advanced Settings", open=False):
278
  with gr.Row():
279
  cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
@@ -287,7 +287,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
287
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
288
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
289
  lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
290
-
291
  with gr.Column():
292
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
293
  result = gr.Image(label="Generated Image")
 
22
  else:
23
  print("warning: no access token found")
24
 
 
25
  # Load LoRAs from JSON file
26
  with open('loras.json', 'r') as f:
27
  loras = json.load(f)
 
58
  def update_selection(evt: gr.SelectData, width, height):
59
  selected_lora = loras[evt.index]
60
 
61
+ # update placeholder
62
+ new_placeholder = f"Type a prompt for {selected_lora['title']}, trigger word is {selected_lora['trigger_word']}"
63
  lora_repo = selected_lora["repo"]
64
  updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
65
+ example_prompt = selected_lora.get("example_prompt", "Enter your prompt here...")
66
 
 
67
  if "aspect" in selected_lora:
68
  if selected_lora["aspect"] == "portrait":
69
  width = 768
 
74
  else:
75
  width = 1024
76
  height = 1024
 
 
77
 
78
  return (
79
+ gr.update(value=example_prompt, placeholder=new_placeholder),
80
+ gr.update(value=updated_text),
81
  evt.index,
82
  width,
83
  height,
 
102
  ):
103
  yield img
104
 
 
105
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
106
  if selected_index is None:
107
  raise gr.Error("You must select a LoRA before proceeding.")
108
  selected_lora = loras[selected_index]
109
+ gr.Info(f"Current LoRA: {selected_lora['title']}")
110
 
111
  lora_path = selected_lora["repo"]
112
  trigger_word = selected_lora["trigger_word"]
113
+
114
  if(trigger_word):
115
  if "trigger_position" in selected_lora:
116
  if selected_lora["trigger_position"] == "prepend":
 
124
 
125
  with calculateDuration("Unloading LoRA"):
126
  pipe.unload_lora_weights()
127
+ # 清除 CUDA 缓存
128
+ torch.cuda.empty_cache()
129
 
130
  # Load LoRA weights
131
  with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
 
133
  pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
134
  else:
135
  pipe.load_lora_weights(lora_path)
136
+ # 添加短暂延迟确保 LoRA 完全加载
137
+ time.sleep(0.5)
138
 
139
  # Set random seed for reproducibility
140
  with calculateDuration("Randomizing seed"):
 
246
  .progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
247
  .progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
248
  '''
249
+
250
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
251
  title = gr.HTML(
252
+ """<h1><img src="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png" alt="LoRA"> FLUX LoRA Collection </h1>""",
253
  elem_id="title",
254
  )
255
  selected_index = gr.State(None)
 
256
  with gr.Row():
257
  with gr.Column(scale=3):
258
+ prompt = gr.Textbox(label="Prompt", lines=3, placeholder="Type a prompt after selecting a LoRA",interactive=True)
259
  with gr.Column(scale=1, elem_id="gen_column"):
260
  generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
 
261
  with gr.Row():
262
  with gr.Column():
263
  selected_info = gr.Markdown("")
 
269
  elem_id="gallery"
270
  )
271
  with gr.Group():
272
+ custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="multimodalart/vintage-ads-flux")
273
  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")
274
  custom_lora_info = gr.HTML(visible=False)
275
  custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
276
+
277
  with gr.Accordion("Advanced Settings", open=False):
278
  with gr.Row():
279
  cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
 
287
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
288
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
289
  lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
290
+
291
  with gr.Column():
292
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
293
  result = gr.Image(label="Generated Image")
loras.json CHANGED
@@ -45,10 +45,11 @@
45
  },
46
 
47
  {
48
- "image": "https://huggingface.co/Shakker-Labs/AWPortrait-FL/resolve/main/cover.jpeg",
49
- "title": "AWPortrait FL",
50
- "repo": "Shakker-Labs/AWPortrait-FL",
51
- "weights": "AWPortrait-FL-lora.safetensors",
52
- "trigger_word": ""
 
53
  }
54
  ]
 
45
  },
46
 
47
  {
48
+ "image": "https://cdn-uploads.huggingface.co/production/uploads/6413514705147e9f74df6fec/_ht43YxR9-K_915vk9_K2.png",
49
+ "title": "JayChow",
50
+ "repo": "vincenthugging/flux-dev-lora-jaychow",
51
+ "trigger_word": "jaychow",
52
+ "example_prompt": "photo of JAYCHOW, a famous male singer of China, street wear, holding a handwritten sign with red text that says 'Hello World!',face detailed ",
53
+ "placeholder": "Trigger word: jaychow"
54
  }
55
  ]