Jordan Legg commited on
Commit
126a4f5
β€’
1 Parent(s): 00ecf1c

let's work this out

Browse files
Files changed (1) hide show
  1. app.py +38 -93
app.py CHANGED
@@ -1,115 +1,60 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- import spaces
5
  import torch
6
- from diffusers import DiffusionPipeline
7
-
8
- # Enable TF32 for A100 (this is a form of FP8 computation)
9
- torch.backends.cuda.matmul.allow_tf32 = True
10
- torch.backends.cudnn.allow_tf32 = True
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
- dtype = torch.float16 # Use float16 for loading
 
 
 
14
 
15
- pipe = DiffusionPipeline.from_pretrained(
16
- "Kijai/flux-fp8",
17
- torch_dtype=dtype,
18
- revision="main",
19
- filename="flux1-schnell-fp8.safetensors"
20
- ).to(device)
 
21
 
22
- MAX_SEED = np.iinfo(np.int32).max
 
 
23
  MAX_IMAGE_SIZE = 2048
24
 
25
  @spaces.GPU()
26
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
27
  if randomize_seed:
28
- seed = random.randint(0, MAX_SEED)
29
- generator = torch.Generator().manual_seed(seed)
30
  image = pipe(
31
  prompt=prompt,
32
  width=width,
33
  height=height,
34
  num_inference_steps=num_inference_steps,
35
  generator=generator,
36
- guidance_scale=0.0
 
37
  ).images[0]
38
  return image, seed
39
 
40
- examples = [
41
- "a tiny astronaut hatching from an egg on the moon",
42
- "a cat holding a sign that says hello world",
43
- "an anime illustration of a wiener schnitzel",
44
- ]
45
-
46
- css="""
47
- #col-container {
48
- margin: 0 auto;
49
- max-width: 520px;
50
- }
51
- """
52
-
53
- with gr.Blocks(css=css) as demo:
54
- with gr.Column(elem_id="col-container"):
55
- gr.Markdown(f"""# FLUX.1 [schnell] FP8
56
- 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
57
- [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/Kijai/flux-fp8)]
58
- """)
59
- with gr.Row():
60
- prompt = gr.Text(
61
- label="Prompt",
62
- show_label=False,
63
- max_lines=1,
64
- placeholder="Enter your prompt",
65
- container=False,
66
- )
67
- run_button = gr.Button("Run", scale=0)
68
- result = gr.Image(label="Result", show_label=False)
69
- with gr.Accordion("Advanced Settings", open=False):
70
- seed = gr.Slider(
71
- label="Seed",
72
- minimum=0,
73
- maximum=MAX_SEED,
74
- step=1,
75
- value=0,
76
- )
77
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
78
- with gr.Row():
79
- width = gr.Slider(
80
- label="Width",
81
- minimum=256,
82
- maximum=MAX_IMAGE_SIZE,
83
- step=32,
84
- value=1024,
85
- )
86
- height = gr.Slider(
87
- label="Height",
88
- minimum=256,
89
- maximum=MAX_IMAGE_SIZE,
90
- step=32,
91
- value=1024,
92
- )
93
- with gr.Row():
94
- num_inference_steps = gr.Slider(
95
- label="Number of inference steps",
96
- minimum=1,
97
- maximum=50,
98
- step=1,
99
- value=4,
100
- )
101
- gr.Examples(
102
- examples=examples,
103
- fn=infer,
104
- inputs=[prompt],
105
- outputs=[result, seed],
106
- cache_examples="lazy"
107
- )
108
- gr.on(
109
- triggers=[run_button.click, prompt.submit],
110
- fn=infer,
111
- inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
112
- outputs=[result, seed]
113
- )
114
 
115
  demo.launch()
 
1
  import gradio as gr
 
 
 
2
  import torch
3
+ import spaces
4
+ from diffusers import FluxPipeline
 
 
 
5
 
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
+ dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
+
9
+ MODEL_ID = "drbaph/FLUX.1-schnell-dev-merged-fp8-4step"
10
+ MODEL_FILE = "flux1-schnell-dev-merged-fp8-4step.safetensors"
11
 
12
+ def load_model():
13
+ pipe = FluxPipeline.from_single_file(
14
+ f"https://huggingface.co/{MODEL_ID}/resolve/main/{MODEL_FILE}",
15
+ torch_dtype=dtype
16
+ )
17
+ pipe.to(device)
18
+ return pipe
19
 
20
+ pipe = load_model()
21
+
22
+ MAX_SEED = 2**32 - 1
23
  MAX_IMAGE_SIZE = 2048
24
 
25
  @spaces.GPU()
26
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
27
  if randomize_seed:
28
+ seed = torch.randint(0, MAX_SEED, (1,)).item()
29
+ generator = torch.Generator(device=device).manual_seed(seed)
30
  image = pipe(
31
  prompt=prompt,
32
  width=width,
33
  height=height,
34
  num_inference_steps=num_inference_steps,
35
  generator=generator,
36
+ guidance_scale=0.0,
37
+ max_sequence_length=256
38
  ).images[0]
39
  return image, seed
40
 
41
+ # Gradio interface
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("# FLUX.1 [schnell-dev-merged-fp8-4step]")
44
+ with gr.Row():
45
+ prompt = gr.Textbox(label="Prompt")
46
+ run_button = gr.Button("Generate")
47
+ with gr.Row():
48
+ result = gr.Image(label="Generated Image")
49
+ seed_output = gr.Number(label="Seed Used")
50
+ with gr.Accordion("Advanced Settings", open=False):
51
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
52
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
53
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
54
+ height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
55
+ num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=10, step=1, value=4)
56
+
57
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps]
58
+ run_button.click(fn=infer, inputs=inputs, outputs=[result, seed_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  demo.launch()