mrcuddle commited on
Commit
3515660
·
verified ·
1 Parent(s): 7b9b23e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -30,9 +30,24 @@ class timer:
30
  if not path.exists(cache_path):
31
  os.makedirs(cache_path, exist_ok=True)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
34
- pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"))
35
- pipe.fuse_lora(lora_scale=0.125)
 
 
36
  pipe.to(device="cuda", dtype=torch.bfloat16)
37
 
38
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -53,24 +68,31 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
53
  placeholder="E.g., A serene landscape with mountains and a lake at sunset",
54
  lines=3
55
  )
56
-
 
 
 
 
 
 
 
57
  with gr.Accordion("Advanced Settings", open=False):
58
  with gr.Group():
59
  with gr.Row():
60
  height = gr.Slider(label="Height", minimum=256, maximum=1152, step=64, value=1024)
61
  width = gr.Slider(label="Width", minimum=256, maximum=1152, step=64, value=1024)
62
-
63
  with gr.Row():
64
  steps = gr.Slider(label="Inference Steps", minimum=6, maximum=25, step=1, value=8)
65
  scales = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=5.0, step=0.1, value=3.5)
66
-
67
  seed = gr.Number(label="Seed (for reproducibility)", value=3413, precision=0)
68
-
69
  generate_btn = gr.Button("Generate Image", variant="primary", scale=1)
70
 
71
  with gr.Column(scale=4):
72
  output = gr.Image(label="Your Generated Image")
73
-
74
  gr.Markdown(
75
  """
76
  <div style="max-width: 650px; margin: 2rem auto; padding: 1rem; border-radius: 10px; background-color: #f0f0f0;">
@@ -86,11 +108,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
  )
87
 
88
  @spaces.GPU
89
- def process_image(height, width, steps, scales, prompt, seed):
90
  global pipe
91
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
 
 
92
  return pipe(
93
- prompt=[prompt],
94
  generator=torch.Generator().manual_seed(int(seed)),
95
  num_inference_steps=int(steps),
96
  guidance_scale=float(scales),
@@ -101,7 +125,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
101
 
102
  generate_btn.click(
103
  process_image,
104
- inputs=[height, width, steps, scales, prompt, seed],
105
  outputs=output
106
  )
107
 
 
30
  if not path.exists(cache_path):
31
  os.makedirs(cache_path, exist_ok=True)
32
 
33
+ def load_and_fuse_lora_weights(pipe, lora_models):
34
+ for repo, file_path, lora_scale in lora_models:
35
+ lora_weights_path = hf_hub_download(repo_id=repo, filename=file_path)
36
+ pipe.load_lora_weights(lora_weights_path)
37
+ pipe.fuse_lora(lora_scale=lora_scale)
38
+
39
+ # List of LoRA models and their corresponding scales
40
+ lora_models = [
41
+ ("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors", 0.125),
42
+ # Add more LoRA models here
43
+ ("mrcuddle/live2d-model-maker", "LIVE2D-FLUX.safetensors", 0.8),
44
+ ]
45
+
46
  pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
47
+
48
+ # Load and fuse LoRA weights
49
+ load_and_fuse_lora_weights(pipe, lora_models)
50
+
51
  pipe.to(device="cuda", dtype=torch.bfloat16)
52
 
53
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
68
  placeholder="E.g., A serene landscape with mountains and a lake at sunset",
69
  lines=3
70
  )
71
+
72
+ # Hidden textbox for the preset prompt
73
+ preset_prompt = gr.Textbox(
74
+ label="Preset Prompt",
75
+ value="live2d,guijiaoxiansheng,separate hand,separate feet,separate head,full body,solo,white background,simple background, CharacterDesignFLUX, reference sheet,white background,simple background,multiple views, upper body, front, from side, color palette reference, high_detailed, captured in high detail, magic particles, multiple references",
76
+ visible=False
77
+ )
78
+
79
  with gr.Accordion("Advanced Settings", open=False):
80
  with gr.Group():
81
  with gr.Row():
82
  height = gr.Slider(label="Height", minimum=256, maximum=1152, step=64, value=1024)
83
  width = gr.Slider(label="Width", minimum=256, maximum=1152, step=64, value=1024)
84
+
85
  with gr.Row():
86
  steps = gr.Slider(label="Inference Steps", minimum=6, maximum=25, step=1, value=8)
87
  scales = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=5.0, step=0.1, value=3.5)
88
+
89
  seed = gr.Number(label="Seed (for reproducibility)", value=3413, precision=0)
90
+
91
  generate_btn = gr.Button("Generate Image", variant="primary", scale=1)
92
 
93
  with gr.Column(scale=4):
94
  output = gr.Image(label="Your Generated Image")
95
+
96
  gr.Markdown(
97
  """
98
  <div style="max-width: 650px; margin: 2rem auto; padding: 1rem; border-radius: 10px; background-color: #f0f0f0;">
 
108
  )
109
 
110
  @spaces.GPU
111
+ def process_image(height, width, steps, scales, prompt, seed, preset_prompt):
112
  global pipe
113
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
114
+ # Concatenate the preset prompt with the user's input prompt
115
+ full_prompt = f"{preset_prompt} {prompt}"
116
  return pipe(
117
+ prompt=[full_prompt],
118
  generator=torch.Generator().manual_seed(int(seed)),
119
  num_inference_steps=int(steps),
120
  guidance_scale=float(scales),
 
125
 
126
  generate_btn.click(
127
  process_image,
128
+ inputs=[height, width, steps, scales, prompt, seed, preset_prompt],
129
  outputs=output
130
  )
131