Spaces:
Running
on
Zero
Running
on
Zero
First commit
Browse files- flux_app/enhance.py +0 -1
- flux_app/{enhance_v2.py → enhance_v1.py} +1 -0
- flux_app/frontend.py +39 -29
- flux_app/{frontend_v2.py → frontend_nw.py} +30 -40
flux_app/enhance.py
CHANGED
|
@@ -53,4 +53,3 @@ def generate(message, max_new_tokens=256, temperature=0.9, top_p=0.95, repetitio
|
|
| 53 |
output += token_text
|
| 54 |
yield output.strip('</s>')
|
| 55 |
return output.strip('</s>')
|
| 56 |
-
|
|
|
|
| 53 |
output += token_text
|
| 54 |
yield output.strip('</s>')
|
| 55 |
return output.strip('</s>')
|
|
|
flux_app/{enhance_v2.py → enhance_v1.py}
RENAMED
|
@@ -53,3 +53,4 @@ def generate(message, max_new_tokens=256, temperature=0.9, top_p=0.95, repetitio
|
|
| 53 |
output += token_text
|
| 54 |
yield output.strip('</s>')
|
| 55 |
return output.strip('</s>')
|
|
|
|
|
|
| 53 |
output += token_text
|
| 54 |
yield output.strip('</s>')
|
| 55 |
return output.strip('</s>')
|
| 56 |
+
|
flux_app/frontend.py
CHANGED
|
@@ -15,8 +15,9 @@ from flux_app.lora_handling import (
|
|
| 15 |
unload_lora_weights, load_lora_weights_into_pipeline, update_selection
|
| 16 |
)
|
| 17 |
from flux_app.utilities import randomize_seed_if_needed, calculateDuration # Absolute import
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
# Dummy loras data for initial UI setup.
|
| 22 |
initial_loras = [
|
|
@@ -103,22 +104,29 @@ class Frontend:
|
|
| 103 |
pass
|
| 104 |
|
| 105 |
@spaces.GPU(duration=100)
|
| 106 |
-
def run_lora(self, prompt, image_input, image_strength, cfg_scale, steps, selected_index,
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if use_enhancer:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
else:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
prompt_mash = prepare_prompt(prompt_used, selected_index, self.loras)
|
| 120 |
selected_lora = self.loras[selected_index]
|
| 121 |
-
|
| 122 |
unload_lora_weights(self.model_manager.pipe, self.model_manager.pipe_i2i)
|
| 123 |
pipe_to_use = self.model_manager.pipe_i2i if image_input is not None else self.model_manager.pipe
|
| 124 |
load_lora_weights_into_pipeline(pipe_to_use, selected_lora["repo"], selected_lora.get("weights"))
|
|
@@ -127,20 +135,21 @@ class Frontend:
|
|
| 127 |
final_image = self.model_manager.generate_image_to_image(
|
| 128 |
prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed
|
| 129 |
)
|
| 130 |
-
yield final_image, seed, gr.update(visible=False),
|
| 131 |
else:
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
step_counter += 1
|
| 137 |
final_image = image
|
| 138 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 139 |
-
yield image, seed, gr.update(value=progress_bar, visible=True),
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
def create_ui(self):
|
|
|
|
| 144 |
with gr.Blocks(theme=gr.themes.Base(), css=self.css, title="Flux LoRA Generation") as app:
|
| 145 |
title = gr.HTML(
|
| 146 |
"""<h1>Flux LoRA Generation</h1>""",
|
|
@@ -189,11 +198,11 @@ class Frontend:
|
|
| 189 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 190 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 191 |
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
|
| 198 |
gallery.select(
|
| 199 |
update_selection,
|
|
@@ -218,7 +227,8 @@ class Frontend:
|
|
| 218 |
gr.on(
|
| 219 |
triggers=[generate_button.click, prompt.submit],
|
| 220 |
fn=self.run_lora,
|
| 221 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index,
|
|
|
|
| 222 |
outputs=[result, seed, progress_bar, enhanced_prompt_box]
|
| 223 |
)
|
| 224 |
|
|
@@ -233,4 +243,4 @@ if __name__ == "__main__":
|
|
| 233 |
frontend = Frontend(model_manager)
|
| 234 |
app = frontend.create_ui()
|
| 235 |
app.queue()
|
| 236 |
-
app.launch(
|
|
|
|
| 15 |
unload_lora_weights, load_lora_weights_into_pipeline, update_selection
|
| 16 |
)
|
| 17 |
from flux_app.utilities import randomize_seed_if_needed, calculateDuration # Absolute import
|
| 18 |
+
|
| 19 |
+
# Import the prompt enhancer function
|
| 20 |
+
from flux_app.enhance import generate as enhance_generate
|
| 21 |
|
| 22 |
# Dummy loras data for initial UI setup.
|
| 23 |
initial_loras = [
|
|
|
|
| 104 |
pass
|
| 105 |
|
| 106 |
@spaces.GPU(duration=100)
|
| 107 |
+
def run_lora(self, prompt, image_input, image_strength, cfg_scale, steps, selected_index,
|
| 108 |
+
randomize_seed, seed, width, height, lora_scale, use_enhancer,
|
| 109 |
+
progress=gr.Progress(track_tqdm=True)):
|
| 110 |
+
seed = randomize_seed_if_needed(randomize_seed, seed, MAX_SEED)
|
| 111 |
+
# Prepare the initial prompt (using LoRA info if needed)
|
| 112 |
+
prompt_mash = prepare_prompt(prompt, selected_index, self.loras)
|
| 113 |
+
enhanced_text = ""
|
| 114 |
+
|
| 115 |
+
# If prompt enhancer is enabled, first run it to improve the prompt.
|
| 116 |
if use_enhancer:
|
| 117 |
+
# Stream the enhanced prompt (this will update the enhanced prompt textbox)
|
| 118 |
+
for enhanced_chunk in enhance_generate(prompt_mash):
|
| 119 |
+
enhanced_text = enhanced_chunk
|
| 120 |
+
# Yield an update with no image yet and the current enhanced prompt.
|
| 121 |
+
yield None, seed, gr.update(visible=False), enhanced_text
|
| 122 |
+
# Use the final enhanced prompt as the prompt for image generation.
|
| 123 |
+
prompt_mash = enhanced_text
|
| 124 |
else:
|
| 125 |
+
# Ensure the enhanced prompt textbox remains cleared.
|
| 126 |
+
enhanced_text = ""
|
| 127 |
+
|
| 128 |
+
# Continue with the image generation process.
|
|
|
|
| 129 |
selected_lora = self.loras[selected_index]
|
|
|
|
| 130 |
unload_lora_weights(self.model_manager.pipe, self.model_manager.pipe_i2i)
|
| 131 |
pipe_to_use = self.model_manager.pipe_i2i if image_input is not None else self.model_manager.pipe
|
| 132 |
load_lora_weights_into_pipeline(pipe_to_use, selected_lora["repo"], selected_lora.get("weights"))
|
|
|
|
| 135 |
final_image = self.model_manager.generate_image_to_image(
|
| 136 |
prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed
|
| 137 |
)
|
| 138 |
+
yield final_image, seed, gr.update(visible=False), enhanced_text
|
| 139 |
else:
|
| 140 |
+
image_generator = self.model_manager.generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale)
|
| 141 |
+
final_image = None
|
| 142 |
+
step_counter = 0
|
| 143 |
+
for image in image_generator:
|
| 144 |
step_counter += 1
|
| 145 |
final_image = image
|
| 146 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 147 |
+
yield image, seed, gr.update(value=progress_bar, visible=True), enhanced_text
|
| 148 |
|
| 149 |
+
yield final_image, seed, gr.update(value=progress_bar, visible=False), enhanced_text
|
| 150 |
|
| 151 |
def create_ui(self):
|
| 152 |
+
# Using a base theme for a clean and professional look.
|
| 153 |
with gr.Blocks(theme=gr.themes.Base(), css=self.css, title="Flux LoRA Generation") as app:
|
| 154 |
title = gr.HTML(
|
| 155 |
"""<h1>Flux LoRA Generation</h1>""",
|
|
|
|
| 198 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 199 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 200 |
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
|
| 201 |
+
with gr.Row():
|
| 202 |
+
use_enhancer = gr.Checkbox(value=False, label="Use Prompt Enhancer")
|
| 203 |
+
show_enhanced_prompt = gr.Checkbox(value=False, label="Display Enhanced Prompt")
|
| 204 |
+
# Enhanced prompt textbox (hidden by default)
|
| 205 |
+
enhanced_prompt_box = gr.Textbox(label="Enhanced Prompt", visible=False)
|
| 206 |
|
| 207 |
gallery.select(
|
| 208 |
update_selection,
|
|
|
|
| 227 |
gr.on(
|
| 228 |
triggers=[generate_button.click, prompt.submit],
|
| 229 |
fn=self.run_lora,
|
| 230 |
+
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index,
|
| 231 |
+
randomize_seed, seed, width, height, lora_scale, use_enhancer],
|
| 232 |
outputs=[result, seed, progress_bar, enhanced_prompt_box]
|
| 233 |
)
|
| 234 |
|
|
|
|
| 243 |
frontend = Frontend(model_manager)
|
| 244 |
app = frontend.create_ui()
|
| 245 |
app.queue()
|
| 246 |
+
app.launch()
|
flux_app/{frontend_v2.py → frontend_nw.py}
RENAMED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import sys
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
# Add the parent directory to sys.path
|
| 7 |
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
| 8 |
sys.path.insert(0, parent_dir)
|
|
@@ -15,9 +15,8 @@ from flux_app.lora_handling import (
|
|
| 15 |
unload_lora_weights, load_lora_weights_into_pipeline, update_selection
|
| 16 |
)
|
| 17 |
from flux_app.utilities import randomize_seed_if_needed, calculateDuration # Absolute import
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
from flux_app.enhance import generate as enhance_generate
|
| 21 |
|
| 22 |
# Dummy loras data for initial UI setup.
|
| 23 |
initial_loras = [
|
|
@@ -104,29 +103,22 @@ class Frontend:
|
|
| 104 |
pass
|
| 105 |
|
| 106 |
@spaces.GPU(duration=100)
|
| 107 |
-
def run_lora(self, prompt, image_input, image_strength, cfg_scale, steps, selected_index,
|
| 108 |
-
|
| 109 |
-
progress=gr.Progress(track_tqdm=True)):
|
| 110 |
-
seed = randomize_seed_if_needed(randomize_seed, seed, MAX_SEED)
|
| 111 |
-
# Prepare the initial prompt (using LoRA info if needed)
|
| 112 |
-
prompt_mash = prepare_prompt(prompt, selected_index, self.loras)
|
| 113 |
-
enhanced_text = ""
|
| 114 |
-
|
| 115 |
-
# If prompt enhancer is enabled, first run it to improve the prompt.
|
| 116 |
if use_enhancer:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# Use the final enhanced prompt as the prompt for image generation.
|
| 123 |
-
prompt_mash = enhanced_text
|
| 124 |
else:
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
selected_lora = self.loras[selected_index]
|
|
|
|
| 130 |
unload_lora_weights(self.model_manager.pipe, self.model_manager.pipe_i2i)
|
| 131 |
pipe_to_use = self.model_manager.pipe_i2i if image_input is not None else self.model_manager.pipe
|
| 132 |
load_lora_weights_into_pipeline(pipe_to_use, selected_lora["repo"], selected_lora.get("weights"))
|
|
@@ -135,21 +127,20 @@ class Frontend:
|
|
| 135 |
final_image = self.model_manager.generate_image_to_image(
|
| 136 |
prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed
|
| 137 |
)
|
| 138 |
-
yield final_image, seed, gr.update(visible=False),
|
| 139 |
else:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
step_counter += 1
|
| 145 |
final_image = image
|
| 146 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 147 |
-
yield image, seed, gr.update(value=progress_bar, visible=True),
|
| 148 |
|
| 149 |
-
|
| 150 |
|
| 151 |
def create_ui(self):
|
| 152 |
-
# Using a base theme for a clean and professional look.
|
| 153 |
with gr.Blocks(theme=gr.themes.Base(), css=self.css, title="Flux LoRA Generation") as app:
|
| 154 |
title = gr.HTML(
|
| 155 |
"""<h1>Flux LoRA Generation</h1>""",
|
|
@@ -198,11 +189,11 @@ class Frontend:
|
|
| 198 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 199 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 200 |
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
|
| 207 |
gallery.select(
|
| 208 |
update_selection,
|
|
@@ -227,8 +218,7 @@ class Frontend:
|
|
| 227 |
gr.on(
|
| 228 |
triggers=[generate_button.click, prompt.submit],
|
| 229 |
fn=self.run_lora,
|
| 230 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index,
|
| 231 |
-
randomize_seed, seed, width, height, lora_scale, use_enhancer],
|
| 232 |
outputs=[result, seed, progress_bar, enhanced_prompt_box]
|
| 233 |
)
|
| 234 |
|
|
@@ -243,4 +233,4 @@ if __name__ == "__main__":
|
|
| 243 |
frontend = Frontend(model_manager)
|
| 244 |
app = frontend.create_ui()
|
| 245 |
app.queue()
|
| 246 |
-
app.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import sys
|
| 4 |
import os
|
| 5 |
+
import spaces
|
| 6 |
# Add the parent directory to sys.path
|
| 7 |
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
| 8 |
sys.path.insert(0, parent_dir)
|
|
|
|
| 15 |
unload_lora_weights, load_lora_weights_into_pipeline, update_selection
|
| 16 |
)
|
| 17 |
from flux_app.utilities import randomize_seed_if_needed, calculateDuration # Absolute import
|
| 18 |
+
# Import the prompt enhancer generate function from the new module
|
| 19 |
+
from flux_app.enhance import generate
|
|
|
|
| 20 |
|
| 21 |
# Dummy loras data for initial UI setup.
|
| 22 |
initial_loras = [
|
|
|
|
| 103 |
pass
|
| 104 |
|
| 105 |
@spaces.GPU(duration=100)
|
| 106 |
+
def run_lora(self, prompt, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, use_enhancer, progress=gr.Progress(track_tqdm=True)):
|
| 107 |
+
# If prompt enhancer is enabled, generate the enhanced prompt.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if use_enhancer:
|
| 109 |
+
enhanced_prompt = ""
|
| 110 |
+
# Generate the enhanced prompt (consume the generator to get the final result)
|
| 111 |
+
for chunk in generate(prompt):
|
| 112 |
+
enhanced_prompt = chunk
|
| 113 |
+
prompt_used = enhanced_prompt
|
|
|
|
|
|
|
| 114 |
else:
|
| 115 |
+
enhanced_prompt = ""
|
| 116 |
+
prompt_used = prompt
|
| 117 |
+
|
| 118 |
+
seed = randomize_seed_if_needed(randomize_seed, seed, MAX_SEED)
|
| 119 |
+
prompt_mash = prepare_prompt(prompt_used, selected_index, self.loras)
|
| 120 |
selected_lora = self.loras[selected_index]
|
| 121 |
+
|
| 122 |
unload_lora_weights(self.model_manager.pipe, self.model_manager.pipe_i2i)
|
| 123 |
pipe_to_use = self.model_manager.pipe_i2i if image_input is not None else self.model_manager.pipe
|
| 124 |
load_lora_weights_into_pipeline(pipe_to_use, selected_lora["repo"], selected_lora.get("weights"))
|
|
|
|
| 127 |
final_image = self.model_manager.generate_image_to_image(
|
| 128 |
prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed
|
| 129 |
)
|
| 130 |
+
yield final_image, seed, gr.update(visible=False), enhanced_prompt
|
| 131 |
else:
|
| 132 |
+
image_generator = self.model_manager.generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale)
|
| 133 |
+
final_image = None
|
| 134 |
+
step_counter = 0
|
| 135 |
+
for image in image_generator:
|
| 136 |
step_counter += 1
|
| 137 |
final_image = image
|
| 138 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 139 |
+
yield image, seed, gr.update(value=progress_bar, visible=True), enhanced_prompt
|
| 140 |
|
| 141 |
+
yield final_image, seed, gr.update(value=progress_bar, visible=False), enhanced_prompt
|
| 142 |
|
| 143 |
def create_ui(self):
|
|
|
|
| 144 |
with gr.Blocks(theme=gr.themes.Base(), css=self.css, title="Flux LoRA Generation") as app:
|
| 145 |
title = gr.HTML(
|
| 146 |
"""<h1>Flux LoRA Generation</h1>""",
|
|
|
|
| 189 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 190 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 191 |
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
|
| 192 |
+
# Prompt Enhancer Section
|
| 193 |
+
with gr.Group():
|
| 194 |
+
use_enhancer = gr.Checkbox(label="Use Prompt Enhancer", value=True)
|
| 195 |
+
show_enhanced_prompt = gr.Checkbox(label="Display Enhanced Prompt", value=False)
|
| 196 |
+
enhanced_prompt_box = gr.Textbox(label="Enhanced Prompt", lines=3, visible=False)
|
| 197 |
|
| 198 |
gallery.select(
|
| 199 |
update_selection,
|
|
|
|
| 218 |
gr.on(
|
| 219 |
triggers=[generate_button.click, prompt.submit],
|
| 220 |
fn=self.run_lora,
|
| 221 |
+
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, use_enhancer],
|
|
|
|
| 222 |
outputs=[result, seed, progress_bar, enhanced_prompt_box]
|
| 223 |
)
|
| 224 |
|
|
|
|
| 233 |
frontend = Frontend(model_manager)
|
| 234 |
app = frontend.create_ui()
|
| 235 |
app.queue()
|
| 236 |
+
app.launch(debug=True)
|