jiuface commited on
Commit
dfff73e
1 Parent(s): af22129

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ torch.backends.cudnn.deterministic = True
9
+ torch.backends.cudnn.benchmark = False
10
+ torch.backends.cuda.matmul.allow_tf32 = True
11
+
12
+ # Initialize the base model and specific LoRA
13
+ base_model = "black-forest-labs/FLUX.1-dev"
14
+ pipe = DiffusionPipeline.from_pretrained(base_model, use_safetensors=True, torch_dtype=torch.bfloat16)
15
+
16
+ lora_repo = "SG161222/RealFlux_1.0b_Dev"
17
+ weights = "1 - Compact Version/RealFlux_1.0b_Dev_Compact.safetensors"
18
+ trigger_word = "" # Leave trigger_word blank if not used.
19
+ pipe.load_lora_weights(lora_repo, weight_name=weights, low_cpu_mem_usage=True)
20
+
21
+ pipe.to("cuda")
22
+
23
+ MAX_SEED = 2**32-1
24
+
25
+ @spaces.GPU()
26
+ def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
27
+ # Set random seed for reproducibility
28
+ if randomize_seed:
29
+ seed = random.randint(0, MAX_SEED)
30
+ generator = torch.Generator(device="cuda").manual_seed(seed)
31
+
32
+ # Update progress bar (0% saat mulai)
33
+ progress(0, "Starting image generation...")
34
+
35
+ # Generate image with progress updates
36
+ for i in range(1, steps + 1):
37
+ # Simulate the processing step (in a real scenario, you would integrate this with your image generation process)
38
+ if i % (steps // 10) == 0: # Update every 10% of the steps
39
+ progress(i / steps * 100, f"Processing step {i} of {steps}...")
40
+
41
+ # Generate image using the pipeline
42
+ image = pipe(
43
+ prompt=f"{prompt} {trigger_word}",
44
+ num_inference_steps=steps,
45
+ guidance_scale=cfg_scale,
46
+ width=width,
47
+ height=height,
48
+ generator=generator,
49
+ joint_attention_kwargs={"scale": lora_scale},
50
+ ).images[0]
51
+
52
+ # Final update (100%)
53
+ progress(100, "Completed!")
54
+
55
+ yield image, seed
56
+
57
+ # Example cached image and settings
58
+ example_image_path = "example0.webp" # Replace with the actual path to the example image
59
+ 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.
60
+ 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.
61
+ 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."""
62
+ example_cfg_scale = 3.2
63
+ example_steps = 32
64
+ example_width = 1152
65
+ example_height = 896
66
+ example_seed = 3981632454
67
+ example_lora_scale = 0.85
68
+
69
+ def load_example():
70
+ # Load example image from file
71
+ example_image = Image.open(example_image_path)
72
+ return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
73
+
74
+ with gr.Blocks() as app:
75
+ gr.Markdown("# Flux RealismLora Image Generator")
76
+ with gr.Row():
77
+ with gr.Column(scale=3):
78
+ prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
79
+ generate_button = gr.Button("Generate")
80
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
81
+ steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
82
+ width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
83
+ height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
84
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
85
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
86
+ lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
87
+ with gr.Column(scale=1):
88
+ result = gr.Image(label="Generated Image")
89
+ 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)]")
90
+
91
+ # Automatically load example data and image when the interface is launched
92
+ app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
93
+
94
+ generate_button.click(
95
+ run_lora,
96
+ inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
97
+ outputs=[result, seed]
98
+ )
99
+
100
+ app.queue()
101
+ app.launch()