ifmain commited on
Commit
1c15b98
·
verified ·
1 Parent(s): f9858e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -49
app.py CHANGED
@@ -3,29 +3,56 @@ import numpy as np
3
  import random
4
  import spaces
5
  import torch
6
- from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
7
- from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
 
 
8
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
9
 
10
  dtype = torch.bfloat16
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
 
13
  taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
14
- good_vae = AutoencoderKL.from_pretrained("ifmain/UltraReal_Fine-Tune", subfolder="vae", torch_dtype=dtype).to(device)
15
- pipe = DiffusionPipeline.from_pretrained("ifmain/UltraReal_Fine-Tune", torch_dtype=dtype, vae=taef1).to(device)
 
 
 
 
 
 
 
 
 
 
 
16
  torch.cuda.empty_cache()
17
 
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 2048
20
 
21
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
22
 
 
 
 
23
  @spaces.GPU(duration=75)
24
- def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
 
27
  generator = torch.Generator().manual_seed(seed)
28
-
29
  for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
30
  prompt=prompt,
31
  guidance_scale=guidance_scale,
@@ -37,14 +64,46 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
37
  good_vae=good_vae,
38
  ):
39
  yield img, seed
40
-
41
- examples = [
42
- "d1g1cam, amateur photo, low-lit, Low-resolution photo, shot on a mobile phone, nighttime, noticeable noise in dark areas. young woman, late 20s casually dressed in an oversized pink T-shirt, outdoors, her gaze directed to the side, sad expression, hold cup of coffee, her blonde hair loosely tied back. Her face partially shadowed, minimal makeup visible. she is standing on the street near soviet-era storey buildings, trees, parked cars near. Dull, overcast lighting",
43
- "v8s, Dimly lit photo, grungy aesthetic, gritty urban, los angeles city on background, interior of muscle car driving under high speed, amateur quality, nighttime, in motion, smeared background, headlights glowing, noise and grain film, glowing dashboard where speedometer is blurred, rearview mirror, woman's hand with black nailpolish on stearing wheel, first person perspective, pov, wide angle go pro lens",
44
- "The image depicts a woman standing outdoors on a snowy path. The individual is wearing a black coat over an orange dress adorned with white floral patterns. They have accessorized with a long, striped scarf in shades of brown and orange that drapes around their neck and shoulders. The woman's feet are bare, suggesting it might be cold outside. In the background, there are multi-story residential buildings with balconies, indicating an urban setting. The ground is partially covered with snow, and the sky appears overcast. photorealistic 8K, film grain, textured skin, dramatic lighting, RAW photo, highly detailed",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  ]
46
 
47
- css="""
48
  #col-container {
49
  margin: 0 auto;
50
  max-width: 520px;
@@ -52,24 +111,23 @@ css="""
52
  """
53
 
54
  with gr.Blocks(css=css) as demo:
55
-
56
  with gr.Column(elem_id="col-container"):
57
- gr.Markdown(f"""# UltraReal Fine-Tune (Flux.1 Dev)
58
-
59
- **🚀 Фотореализм нового уровня!**
60
- Вышла 4-я версия **UltraReal Fine-Tune**, основанная на **Flux.1 Dev**.
61
- Скачать можно тут: [Civitai](https://civitai.com/models/978314?modelVersionId=1413133)
62
-
63
- **🚀 Next-level photorealism!**
64
- The 4th version of **UltraReal Fine-Tune**, based on **Flux.1 Dev**, has been released.
65
- You can download it here: [Civitai](https://civitai.com/models/978314?modelVersionId=1413133)
66
-
67
- [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)] [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)]
68
- """)
69
-
70
-
 
71
  with gr.Row():
72
-
73
  prompt = gr.Text(
74
  label="Prompt",
75
  show_label=False,
@@ -77,13 +135,11 @@ with gr.Blocks(css=css) as demo:
77
  placeholder="Enter your prompt",
78
  container=False,
79
  )
80
-
81
  run_button = gr.Button("Run", scale=0)
82
-
83
  result = gr.Image(label="Result", show_label=False)
84
-
85
  with gr.Accordion("Advanced Settings", open=False):
86
-
87
  seed = gr.Slider(
88
  label="Seed",
89
  minimum=0,
@@ -91,11 +147,9 @@ with gr.Blocks(css=css) as demo:
91
  step=1,
92
  value=0,
93
  )
94
-
95
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
96
-
97
  with gr.Row():
98
-
99
  width = gr.Slider(
100
  label="Width",
101
  minimum=256,
@@ -103,7 +157,6 @@ with gr.Blocks(css=css) as demo:
103
  step=32,
104
  value=1024,
105
  )
106
-
107
  height = gr.Slider(
108
  label="Height",
109
  minimum=256,
@@ -111,9 +164,8 @@ with gr.Blocks(css=css) as demo:
111
  step=32,
112
  value=1024,
113
  )
114
-
115
- with gr.Row():
116
 
 
117
  guidance_scale = gr.Slider(
118
  label="Guidance Scale",
119
  minimum=1,
@@ -121,7 +173,6 @@ with gr.Blocks(css=css) as demo:
121
  step=0.1,
122
  value=3.5,
123
  )
124
-
125
  num_inference_steps = gr.Slider(
126
  label="Number of inference steps",
127
  minimum=1,
@@ -129,20 +180,19 @@ with gr.Blocks(css=css) as demo:
129
  step=1,
130
  value=28,
131
  )
132
-
133
  gr.Examples(
134
- examples = examples,
135
- fn = infer,
136
- inputs = [prompt],
137
- outputs = [result, seed],
138
  cache_examples="lazy"
139
  )
140
 
141
  gr.on(
142
  triggers=[run_button.click, prompt.submit],
143
- fn = infer,
144
- inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
145
- outputs = [result, seed]
146
  )
147
 
148
  demo.launch()
 
3
  import random
4
  import spaces
5
  import torch
6
+
7
+ from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
8
+ from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
9
+
10
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
11
 
12
  dtype = torch.bfloat16
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
 
15
+ # Загружаем автоэнкодер и VAE
16
  taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
17
+ good_vae = AutoencoderKL.from_pretrained(
18
+ "ifmain/UltraReal_Fine-Tune",
19
+ subfolder="vae",
20
+ torch_dtype=dtype
21
+ ).to(device)
22
+
23
+ # Загружаем основной пайплайн
24
+ pipe = DiffusionPipeline.from_pretrained(
25
+ "ifmain/UltraReal_Fine-Tune",
26
+ torch_dtype=dtype,
27
+ vae=taef1
28
+ ).to(device)
29
+
30
  torch.cuda.empty_cache()
31
 
32
+ # Подключаем LoRA
33
+ pipe.load_lora_weights("ifMain/realism")
34
 
35
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
36
 
37
+ MAX_SEED = np.iinfo(np.int32).max
38
+ MAX_IMAGE_SIZE = 2048
39
+
40
  @spaces.GPU(duration=75)
41
+ def infer(
42
+ prompt,
43
+ seed=42,
44
+ randomize_seed=False,
45
+ width=1024,
46
+ height=1024,
47
+ guidance_scale=3.5,
48
+ num_inference_steps=28,
49
+ progress=gr.Progress(track_tqdm=True)
50
+ ):
51
  if randomize_seed:
52
  seed = random.randint(0, MAX_SEED)
53
+
54
  generator = torch.Generator().manual_seed(seed)
55
+
56
  for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
57
  prompt=prompt,
58
  guidance_scale=guidance_scale,
 
64
  good_vae=good_vae,
65
  ):
66
  yield img, seed
67
+
68
+ # Полные примеры с различными стилями и условиями съемки
69
+ full_examples = [
70
+ [
71
+ "d1g1cam, amateur photo, low-lit, Low-resolution photo, shot on a mobile phone, nighttime, noticeable noise in dark areas. "
72
+ "Young woman, late 20s, casually dressed in an oversized pink T-shirt, outdoors, her gaze directed to the side, sad expression, holding a cup of coffee. "
73
+ "Her blonde hair is loosely tied back. Her face partially shadowed, minimal makeup visible. She is standing on the street near Soviet-era storey buildings, trees, parked cars nearby. "
74
+ "Dull, overcast lighting.",
75
+ 1024, 1024, 3.5, 28
76
+ ],
77
+ [
78
+ "v8s, Dimly lit photo, grungy aesthetic, gritty urban, Los Angeles city on background, interior of muscle car driving at high speed. "
79
+ "Amateur quality, nighttime, in motion, smeared background, headlights glowing, noise and grain film. "
80
+ "Glowing dashboard where speedometer is blurred, rearview mirror, woman's hand with black nail polish on steering wheel. "
81
+ "First-person perspective, wide-angle GoPro lens.",
82
+ 1024, 768, 4.0, 30
83
+ ],
84
+ [
85
+ "The image depicts a woman standing outdoors on a snowy path. The individual is wearing a black coat over an orange dress adorned with white floral patterns. "
86
+ "They have accessorized with a long, striped scarf in shades of brown and orange that drapes around their neck and shoulders. "
87
+ "The woman's feet are bare, suggesting it might be cold outside. In the background, there are multi-story residential buildings with balconies, indicating an urban setting. "
88
+ "The ground is partially covered with snow, and the sky appears overcast. "
89
+ "Photorealistic 8K, film grain, textured skin, dramatic lighting, RAW photo, highly detailed.",
90
+ 1024, 1024, 3.0, 25
91
+ ],
92
+ [
93
+ "Photorealistic close-up portrait of an elderly man with deep wrinkles and a gray beard. His eyes reflect wisdom and experience. "
94
+ "Soft lighting highlights the texture of his skin. He wears a simple wool sweater. The background is blurred, resembling an indoor wooden interior with bookshelves. "
95
+ "Film-like quality, 85mm lens, f/1.8 depth of field, cinematic lighting.",
96
+ 768, 1024, 3.8, 35
97
+ ],
98
+ [
99
+ "Golden hour landscape of a countryside road. The sun is setting, casting warm golden light over rolling hills and a winding dirt path. "
100
+ "Lush green trees line the road, and distant mountains are faintly visible. The sky is a blend of soft pink and deep orange hues. "
101
+ "Captured on a full-frame DSLR, 50mm f/2.0 lens, soft focus, detailed textures.",
102
+ 1536, 1024, 2.5, 20
103
+ ],
104
  ]
105
 
106
+ css = """
107
  #col-container {
108
  margin: 0 auto;
109
  max-width: 520px;
 
111
  """
112
 
113
  with gr.Blocks(css=css) as demo:
 
114
  with gr.Column(elem_id="col-container"):
115
+ gr.Markdown(
116
+ """# UltraReal Fine-Tune (Flux.1 Dev)
117
+
118
+ **🚀 Фотореализм нового уровня!**
119
+ Вышла 4-я версия **UltraReal Fine-Tune**, основанная на **Flux.1 Dev**.
120
+ Скачать можно тут: [Civitai](https://civitai.com/models/978314?modelVersionId=1413133)
121
+
122
+ **🚀 Next-level photorealism!**
123
+ The 4th version of **UltraReal Fine-Tune**, based on **Flux.1 Dev**, has been released.
124
+ You can download it here: [Civitai](https://civitai.com/models/978314?modelVersionId=1413133)
125
+
126
+ [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)] [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)]
127
+ """
128
+ )
129
+
130
  with gr.Row():
 
131
  prompt = gr.Text(
132
  label="Prompt",
133
  show_label=False,
 
135
  placeholder="Enter your prompt",
136
  container=False,
137
  )
 
138
  run_button = gr.Button("Run", scale=0)
139
+
140
  result = gr.Image(label="Result", show_label=False)
141
+
142
  with gr.Accordion("Advanced Settings", open=False):
 
143
  seed = gr.Slider(
144
  label="Seed",
145
  minimum=0,
 
147
  step=1,
148
  value=0,
149
  )
 
150
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
151
+
152
  with gr.Row():
 
153
  width = gr.Slider(
154
  label="Width",
155
  minimum=256,
 
157
  step=32,
158
  value=1024,
159
  )
 
160
  height = gr.Slider(
161
  label="Height",
162
  minimum=256,
 
164
  step=32,
165
  value=1024,
166
  )
 
 
167
 
168
+ with gr.Row():
169
  guidance_scale = gr.Slider(
170
  label="Guidance Scale",
171
  minimum=1,
 
173
  step=0.1,
174
  value=3.5,
175
  )
 
176
  num_inference_steps = gr.Slider(
177
  label="Number of inference steps",
178
  minimum=1,
 
180
  step=1,
181
  value=28,
182
  )
183
+
184
  gr.Examples(
185
+ examples=full_examples,
186
+ inputs=[prompt, width, height, guidance_scale, num_inference_steps],
187
+ outputs=[result, seed],
 
188
  cache_examples="lazy"
189
  )
190
 
191
  gr.on(
192
  triggers=[run_button.click, prompt.submit],
193
+ fn=infer,
194
+ inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
195
+ outputs=[result, seed]
196
  )
197
 
198
  demo.launch()