jr08 commited on
Commit
3e14e4c
·
verified ·
1 Parent(s): 2d1a10b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -84
app.py CHANGED
@@ -1,67 +1,66 @@
1
- # app.py
2
-
3
- import gradio as gr
4
  import numpy as np
5
  import random
6
- import os
7
- from huggingface_hub import InferenceClient
8
-
9
- # 初始化 InferenceClient
10
- client = InferenceClient(
11
- provider="fal-ai",
12
- api_key=os.environ.get("HF_TOKEN", ""),
13
- )
14
-
15
- MAX_SEED = np.iinfo(np.int32).max
16
- MAX_IMAGE_SIZE = 1024
17
-
18
- def infer(
19
- prompt,
20
- negative_prompt,
21
- seed,
22
- randomize_seed,
23
- width,
24
- height,
25
- guidance_scale,
26
- num_inference_steps,
27
- progress=gr.Progress(track_tqdm=True),
28
- ):
29
- if not prompt or prompt.strip() == "":
30
- return None, seed
31
 
32
- # 只传递 prompt,避免参数过多导致报错
33
- full_prompt = prompt
34
- if negative_prompt:
35
- full_prompt += f". Negative prompt: {negative_prompt}"
36
 
37
- try:
38
- image = client.text_to_image(
39
- full_prompt,
40
- model="black-forest-labs/FLUX.1-Krea-dev",
41
- )
42
- return image, seed
43
- except Exception as e:
44
- # 返回错误信息到前端
45
- return f"生成图片出错: {str(e)}", seed
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  examples = [
48
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
49
- "An astronaut riding a green horse",
50
- "A delicious ceviche cheesecake slice",
51
  ]
52
 
53
- css = """
54
  #col-container {
55
  margin: 0 auto;
56
- max-width: 640px;
57
  }
58
  """
59
 
60
  with gr.Blocks(css=css) as demo:
 
61
  with gr.Column(elem_id="col-container"):
62
- gr.Markdown(" # FLUX.1-Krea Text-to-Image Demo")
63
-
 
 
 
64
  with gr.Row():
 
65
  prompt = gr.Text(
66
  label="Prompt",
67
  show_label=False,
@@ -69,19 +68,13 @@ with gr.Blocks(css=css) as demo:
69
  placeholder="Enter your prompt",
70
  container=False,
71
  )
72
-
73
- run_button = gr.Button("Run", scale=0, variant="primary")
74
-
75
- result = gr.Image(label="Result", show_label=False, type="pil")
76
-
77
  with gr.Accordion("Advanced Settings", open=False):
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,
@@ -89,10 +82,11 @@ with gr.Blocks(css=css) as demo:
89
  step=1,
90
  value=0,
91
  )
92
-
93
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
  with gr.Row():
 
96
  width = gr.Slider(
97
  label="Width",
98
  minimum=256,
@@ -100,7 +94,7 @@ with gr.Blocks(css=css) as demo:
100
  step=32,
101
  value=1024,
102
  )
103
-
104
  height = gr.Slider(
105
  label="Height",
106
  minimum=256,
@@ -108,40 +102,38 @@ with gr.Blocks(css=css) as demo:
108
  step=32,
109
  value=1024,
110
  )
111
-
112
  with gr.Row():
 
113
  guidance_scale = gr.Slider(
114
- label="Guidance scale",
115
- minimum=0.0,
116
- maximum=10.0,
117
  step=0.1,
118
  value=4.5,
119
  )
120
-
121
  num_inference_steps = gr.Slider(
122
  label="Number of inference steps",
123
  minimum=1,
124
  maximum=50,
125
  step=1,
126
- value=30,
127
  )
 
 
 
 
 
 
 
 
128
 
129
- gr.Examples(examples=examples, inputs=[prompt])
130
  gr.on(
131
  triggers=[run_button.click, prompt.submit],
132
- fn=infer,
133
- inputs=[
134
- prompt,
135
- negative_prompt,
136
- seed,
137
- randomize_seed,
138
- width,
139
- height,
140
- guidance_scale,
141
- num_inference_steps,
142
- ],
143
- outputs=[result, seed],
144
  )
145
 
146
- if __name__ == "__main__":
147
- demo.launch()
 
1
+ port gradio as gr
 
 
2
  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("black-forest-labs/FLUX.1-Krea-dev", subfolder="vae", torch_dtype=dtype).to(device)
15
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-Krea-dev", 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=4.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,
32
+ num_inference_steps=num_inference_steps,
33
+ width=width,
34
+ height=height,
35
+ generator=generator,
36
+ output_type="pil",
37
+ good_vae=good_vae,
38
+ ):
39
+ yield img, seed
40
+
41
  examples = [
42
+ "a tiny astronaut hatching from an egg on mars",
43
+ "a dog holding a sign that reads 'hello world'",
44
+ "an anime illustration of an apple strudel",
45
  ]
46
 
47
+ css="""
48
  #col-container {
49
  margin: 0 auto;
50
+ max-width: 620px;
51
  }
52
  """
53
 
54
  with gr.Blocks(css=css) as demo:
55
+
56
  with gr.Column(elem_id="col-container"):
57
+ gr.Markdown(f"""# FLUX.1 Krea [dev]
58
+ FLUX.1 Krea [dev] model further tuned and customized with [Krea](https://krea.ai)
59
+ [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
60
+ """)
61
+
62
  with gr.Row():
63
+
64
  prompt = gr.Text(
65
  label="Prompt",
66
  show_label=False,
 
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
  seed = gr.Slider(
79
  label="Seed",
80
  minimum=0,
 
82
  step=1,
83
  value=0,
84
  )
85
+
86
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
87
+
88
  with gr.Row():
89
+
90
  width = gr.Slider(
91
  label="Width",
92
  minimum=256,
 
94
  step=32,
95
  value=1024,
96
  )
97
+
98
  height = gr.Slider(
99
  label="Height",
100
  minimum=256,
 
102
  step=32,
103
  value=1024,
104
  )
105
+
106
  with gr.Row():
107
+
108
  guidance_scale = gr.Slider(
109
+ label="Guidance Scale",
110
+ minimum=1,
111
+ maximum=15,
112
  step=0.1,
113
  value=4.5,
114
  )
115
+
116
  num_inference_steps = gr.Slider(
117
  label="Number of inference steps",
118
  minimum=1,
119
  maximum=50,
120
  step=1,
121
+ value=28,
122
  )
123
+
124
+ gr.Examples(
125
+ examples = examples,
126
+ fn = infer,
127
+ inputs = [prompt],
128
+ outputs = [result, seed],
129
+ cache_examples="lazy"
130
+ )
131
 
 
132
  gr.on(
133
  triggers=[run_button.click, prompt.submit],
134
+ fn = infer,
135
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
136
+ outputs = [result, seed]
 
 
 
 
 
 
 
 
 
137
  )
138
 
139
+ demo.launch()