Upload 3 files
Browse files- app (4).py +98 -0
- app (5).py +109 -0
- app (6).py +109 -0
app (4).py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from diffusers import DiffusionPipeline
|
6 |
+
import random
|
7 |
+
|
8 |
+
# Initialize the base model and specific LoRA
|
9 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
10 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
11 |
+
|
12 |
+
lora_repo = "XLabs-AI/flux-RealismLora"
|
13 |
+
trigger_word = "" # Leave trigger_word blank if not used.
|
14 |
+
pipe.load_lora_weights(lora_repo)
|
15 |
+
|
16 |
+
pipe.to("cuda")
|
17 |
+
|
18 |
+
MAX_SEED = 2**32-1
|
19 |
+
|
20 |
+
@spaces.GPU(duration=80)
|
21 |
+
def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
22 |
+
# Set random seed for reproducibility
|
23 |
+
if randomize_seed:
|
24 |
+
seed = random.randint(0, MAX_SEED)
|
25 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
26 |
+
|
27 |
+
# Update progress bar (0% saat mulai)
|
28 |
+
progress(0, "Starting image generation...")
|
29 |
+
|
30 |
+
# Generate image with progress updates
|
31 |
+
for i in range(1, steps + 1):
|
32 |
+
# Simulate the processing step (in a real scenario, you would integrate this with your image generation process)
|
33 |
+
if i % (steps // 10) == 0: # Update every 10% of the steps
|
34 |
+
progress(i / steps * 100, f"Processing step {i} of {steps}...")
|
35 |
+
|
36 |
+
# Generate image using the pipeline
|
37 |
+
image = pipe(
|
38 |
+
prompt=f"{prompt} {trigger_word}",
|
39 |
+
num_inference_steps=steps,
|
40 |
+
guidance_scale=cfg_scale,
|
41 |
+
width=width,
|
42 |
+
height=height,
|
43 |
+
generator=generator,
|
44 |
+
joint_attention_kwargs={"scale": lora_scale},
|
45 |
+
).images[0]
|
46 |
+
|
47 |
+
# Final update (100%)
|
48 |
+
progress(100, "Completed!")
|
49 |
+
|
50 |
+
yield image, seed
|
51 |
+
|
52 |
+
# Example cached image and settings
|
53 |
+
example_image_path = "example0.webp" # Replace with the actual path to the example image
|
54 |
+
example_prompt = """A Jelita Sukawati speaker is captured mid-speech. She has long, dark brown hair that cascades over her shoulders, framing her radiant, smiling face. Her Latina features are highlighted by warm, sun-kissed skin and bright, expressive eyes. She gestures with her left hand, displaying a delicate ring on her pinky finger, as she speaks passionately.
|
55 |
+
|
56 |
+
The woman is wearing a colorful, patterned dress with a green lanyard featuring multiple badges and logos hanging around her neck. The lanyard prominently displays the "CagliostroLab" text.
|
57 |
+
|
58 |
+
Behind her, there is a blurred background with a white banner containing logos and text, indicating a professional or conference setting. The overall scene captures the energy and vibrancy of her presentation."""
|
59 |
+
example_cfg_scale = 3.2
|
60 |
+
example_steps = 32
|
61 |
+
example_width = 1152
|
62 |
+
example_height = 896
|
63 |
+
example_seed = 3981632454
|
64 |
+
example_lora_scale = 0.85
|
65 |
+
|
66 |
+
def load_example():
|
67 |
+
# Load example image from file
|
68 |
+
example_image = Image.open(example_image_path)
|
69 |
+
return example_prompt, example_cfg_scale, example_steps, False, example_seed, example_width, example_height, example_lora_scale, example_image
|
70 |
+
|
71 |
+
with gr.Blocks() as app:
|
72 |
+
gr.Markdown("# Flux RealismLora Image Generator")
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column(scale=3):
|
75 |
+
prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
|
76 |
+
generate_button = gr.Button("Generate")
|
77 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
|
78 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
|
79 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
|
80 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
|
81 |
+
randomize_seed = gr.Checkbox(False, label="Randomize seed")
|
82 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
|
83 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
|
84 |
+
with gr.Column(scale=1):
|
85 |
+
result = gr.Image(label="Generated Image")
|
86 |
+
gr.Markdown("Generate images using RealismLora and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]")
|
87 |
+
|
88 |
+
# Automatically load example data and image when the interface is launched
|
89 |
+
app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
|
90 |
+
|
91 |
+
generate_button.click(
|
92 |
+
run_lora,
|
93 |
+
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
|
94 |
+
outputs=[result, seed]
|
95 |
+
)
|
96 |
+
|
97 |
+
app.queue()
|
98 |
+
app.launch()
|
app (5).py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import argparse
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
from os import path
|
6 |
+
from safetensors.torch import load_file
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
+
|
9 |
+
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
10 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
11 |
+
os.environ["HF_HUB_CACHE"] = cache_path
|
12 |
+
os.environ["HF_HOME"] = cache_path
|
13 |
+
|
14 |
+
import gradio as gr
|
15 |
+
import torch
|
16 |
+
from diffusers import FluxPipeline
|
17 |
+
|
18 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
19 |
+
|
20 |
+
class timer:
|
21 |
+
def __init__(self, method_name="timed process"):
|
22 |
+
self.method = method_name
|
23 |
+
def __enter__(self):
|
24 |
+
self.start = time.time()
|
25 |
+
print(f"{self.method} starts")
|
26 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
27 |
+
end = time.time()
|
28 |
+
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
29 |
+
|
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:
|
39 |
+
gr.Markdown(
|
40 |
+
"""
|
41 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
42 |
+
<h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem; display: contents;">Hyper-FLUX-8steps-LoRA</h1>
|
43 |
+
<p style="font-size: 1rem; margin-bottom: 1.5rem;">AutoML team from ByteDance</p>
|
44 |
+
</div>
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column(scale=3):
|
50 |
+
with gr.Group():
|
51 |
+
prompt = gr.Textbox(
|
52 |
+
label="Your Image Description",
|
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;">
|
77 |
+
<h2 style="font-size: 1.5rem; margin-bottom: 1rem;">How to Use</h2>
|
78 |
+
<ol style="padding-left: 1.5rem;">
|
79 |
+
<li>Enter a detailed description of the image you want to create.</li>
|
80 |
+
<li>Adjust advanced settings if desired (tap to expand).</li>
|
81 |
+
<li>Tap "Generate Image" and wait for your creation!</li>
|
82 |
+
</ol>
|
83 |
+
<p style="margin-top: 1rem; font-style: italic;">Tip: Be specific in your description for best results!</p>
|
84 |
+
</div>
|
85 |
+
"""
|
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),
|
97 |
+
height=int(height),
|
98 |
+
width=int(width),
|
99 |
+
max_sequence_length=256
|
100 |
+
).images[0]
|
101 |
+
|
102 |
+
generate_btn.click(
|
103 |
+
process_image,
|
104 |
+
inputs=[height, width, steps, scales, prompt, seed],
|
105 |
+
outputs=output
|
106 |
+
)
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
demo.launch()
|
app (6).py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import argparse
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
from os import path
|
6 |
+
from safetensors.torch import load_file
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
+
|
9 |
+
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
10 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
11 |
+
os.environ["HF_HUB_CACHE"] = cache_path
|
12 |
+
os.environ["HF_HOME"] = cache_path
|
13 |
+
|
14 |
+
import gradio as gr
|
15 |
+
import torch
|
16 |
+
from diffusers import FluxPipeline
|
17 |
+
|
18 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
19 |
+
|
20 |
+
class timer:
|
21 |
+
def __init__(self, method_name="timed process"):
|
22 |
+
self.method = method_name
|
23 |
+
def __enter__(self):
|
24 |
+
self.start = time.time()
|
25 |
+
print(f"{self.method} starts")
|
26 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
27 |
+
end = time.time()
|
28 |
+
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
29 |
+
|
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:
|
39 |
+
gr.Markdown(
|
40 |
+
"""
|
41 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
42 |
+
<h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem; display: contents;">Hyper-FLUX-8steps-LoRA</h1>
|
43 |
+
<p style="font-size: 1rem; margin-bottom: 1.5rem;">AutoML team from ByteDance</p>
|
44 |
+
</div>
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column(scale=3):
|
50 |
+
with gr.Group():
|
51 |
+
prompt = gr.Textbox(
|
52 |
+
label="Your Image Description",
|
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;">
|
77 |
+
<h2 style="font-size: 1.5rem; margin-bottom: 1rem;">How to Use</h2>
|
78 |
+
<ol style="padding-left: 1.5rem;">
|
79 |
+
<li>Enter a detailed description of the image you want to create.</li>
|
80 |
+
<li>Adjust advanced settings if desired (tap to expand).</li>
|
81 |
+
<li>Tap "Generate Image" and wait for your creation!</li>
|
82 |
+
</ol>
|
83 |
+
<p style="margin-top: 1rem; font-style: italic;">Tip: Be specific in your description for best results!</p>
|
84 |
+
</div>
|
85 |
+
"""
|
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),
|
97 |
+
height=int(height),
|
98 |
+
width=int(width),
|
99 |
+
max_sequence_length=256
|
100 |
+
).images[0]
|
101 |
+
|
102 |
+
generate_btn.click(
|
103 |
+
process_image,
|
104 |
+
inputs=[height, width, steps, scales, prompt, seed],
|
105 |
+
outputs=output
|
106 |
+
)
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
demo.launch()
|