EV5V commited on
Commit
7910fbb
·
verified ·
1 Parent(s): 6d34016

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -126
app.py CHANGED
@@ -1,142 +1,94 @@
 
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- #import spaces #[uncomment to use ZeroGPU]
5
- from diffusers import DiffusionPipeline
6
  import torch
 
 
 
7
 
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
-
11
- if torch.cuda.is_available():
12
- torch_dtype = torch.float16
13
- else:
14
- torch_dtype = torch.float32
15
 
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
- pipe = pipe.to(device)
 
18
 
19
- MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
21
 
22
- #@spaces.GPU #[uncomment to use ZeroGPU]
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
-
28
- generator = torch.Generator().manual_seed(seed)
29
-
 
 
 
 
 
 
 
 
 
30
  image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
39
-
40
- return image, seed
41
 
42
- examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
- ]
47
 
48
- css="""
49
- #col-container {
50
- margin: 0 auto;
51
- max-width: 640px;
52
- }
53
- """
54
 
55
- with gr.Blocks(css=css) as demo:
56
-
57
- with gr.Column(elem_id="col-container"):
58
- gr.Markdown(f"""
59
- # Text-to-Image Gradio Template
60
- """)
61
-
62
- with gr.Row():
63
-
64
- prompt = gr.Text(
65
- label="Prompt",
66
- show_label=False,
67
- max_lines=1,
68
- placeholder="Enter your prompt",
69
- container=False,
70
- )
71
-
72
- run_button = gr.Button("Run", scale=0)
73
-
74
- result = gr.Image(label="Result", show_label=False)
75
 
76
- with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
- max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
- )
84
-
85
- seed = gr.Slider(
86
- label="Seed",
87
- minimum=0,
88
- maximum=MAX_SEED,
89
- step=1,
90
- value=0,
91
- )
92
-
93
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
- with gr.Row():
96
-
97
- width = gr.Slider(
98
- label="Width",
99
- minimum=256,
100
- maximum=MAX_IMAGE_SIZE,
101
- step=32,
102
- value=1024, #Replace with defaults that work for your model
103
- )
104
-
105
- height = gr.Slider(
106
- label="Height",
107
- minimum=256,
108
- maximum=MAX_IMAGE_SIZE,
109
- step=32,
110
- value=1024, #Replace with defaults that work for your model
111
- )
112
-
113
- with gr.Row():
114
-
115
- guidance_scale = gr.Slider(
116
- label="Guidance scale",
117
- minimum=0.0,
118
- maximum=10.0,
119
- step=0.1,
120
- value=0.0, #Replace with defaults that work for your model
121
- )
122
-
123
- num_inference_steps = gr.Slider(
124
- label="Number of inference steps",
125
- minimum=1,
126
- maximum=50,
127
- step=1,
128
- value=2, #Replace with defaults that work for your model
129
- )
130
-
131
- gr.Examples(
132
- examples = examples,
133
- inputs = [prompt]
134
- )
135
- gr.on(
136
- triggers=[run_button.click, prompt.submit],
137
- fn = infer,
138
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs = [result, seed]
140
  )
141
 
142
- demo.queue().launch()
 
 
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 = """Bem vindo."""
55
+ example_cfg_scale = 3.2
56
+ example_steps = 32
57
+ example_width = 1152
58
+ example_height = 896
59
+ example_seed = 3981632454
60
+ example_lora_scale = 0.85
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ def load_example():
63
+ # Load example image from file
64
+ example_image = Image.open(example_image_path)
65
+ return example_prompt, example_cfg_scale, example_steps, False, example_seed, example_width, example_height, example_lora_scale, example_image
66
+
67
+ with gr.Blocks() as app:
68
+ gr.Markdown("# Flux RealismLora Image Generator")
69
+ with gr.Row():
70
+ with gr.Column(scale=3):
71
+ prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
72
+ generate_button = gr.Button("Generate")
73
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
74
+ steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
75
+ width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
76
+ height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
77
+ randomize_seed = gr.Checkbox(False, label="Randomize seed")
78
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
79
+ lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
80
+ with gr.Column(scale=1):
81
+ result = gr.Image(label="Generated Image")
82
+ 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)]")
83
+
84
+ # Automatically load example data and image when the interface is launched
85
+ app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
86
+
87
+ generate_button.click(
88
+ run_lora,
89
+ inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
90
+ outputs=[result, seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  )
92
 
93
+ app.queue()
94
+ app.launch()