My-AI-Projects commited on
Commit
5d657f5
Β·
verified Β·
1 Parent(s): 71ea538

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -132
app.py CHANGED
@@ -1,144 +1,47 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
  import torch
5
- from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
6
  from PIL import Image
7
 
8
- dtype = torch.bfloat16
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
-
11
- taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
12
- good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
13
- pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
14
- torch.cuda.empty_cache()
15
-
16
- MAX_SEED = np.iinfo(np.int32).max
17
- MAX_IMAGE_SIZE = 2048
18
-
19
- # Mock function to replace flux_pipe_call_that_returns_an_iterable_of_images
20
- def mock_flux_pipe_call_that_returns_an_iterable_of_images(prompt, guidance_scale, num_inference_steps, width, height, generator, output_type, good_vae):
21
- # Generate a placeholder image
22
- image = Image.new('RGB', (width, height), color = 'red')
23
- yield image
24
 
25
- # Replace the method with the mock function
26
- pipe.flux_pipe_call_that_returns_an_iterable_of_images = mock_flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
 
27
 
28
- @gr.inputs.GPU(duration=75)
29
- 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)):
30
- if randomize_seed:
31
- seed = random.randint(0, MAX_SEED)
32
- generator = torch.Generator().manual_seed(seed)
33
 
34
- for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
35
- prompt=prompt,
36
- guidance_scale=guidance_scale,
37
- num_inference_steps=num_inference_steps,
38
- width=width,
39
- height=height,
40
- generator=generator,
41
- output_type="pil",
42
- good_vae=good_vae,
43
- ):
44
- yield img, seed
45
-
46
- examples = [
47
- "a tiny astronaut hatching from an egg on the moon",
48
- "a cat holding a sign that says hello world",
49
- "an anime illustration of a wiener schnitzel",
50
- ]
51
-
52
- css="""
53
- #col-container {
54
- margin: 0 auto;
55
- max-width: 520px;
56
- }
57
- """
58
-
59
- with gr.Blocks(css=css) as demo:
60
 
61
- with gr.Column(elem_id="col-container"):
62
- gr.Markdown(f"""# FLUX.1 [dev]
63
- 12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
64
- [[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)]
65
- """)
66
-
67
- with gr.Row():
68
-
69
- prompt = gr.Text(
70
- label="Prompt",
71
- show_label=False,
72
- max_lines=1,
73
- placeholder="Enter your prompt",
74
- container=False,
75
- )
76
-
77
- run_button = gr.Button("Run", scale=0)
78
-
79
- result = gr.Image(label="Result", show_label=False)
80
-
81
- with gr.Accordion("Advanced Settings", open=False):
82
-
83
- seed = gr.Slider(
84
- label="Seed",
85
- minimum=0,
86
- maximum=MAX_SEED,
87
- step=1,
88
- value=0,
89
- )
90
-
91
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
92
-
93
- with gr.Row():
94
-
95
- width = gr.Slider(
96
- label="Width",
97
- minimum=256,
98
- maximum=MAX_IMAGE_SIZE,
99
- step=32,
100
- value=1024,
101
- )
102
-
103
- height = gr.Slider(
104
- label="Height",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024,
109
- )
110
-
111
- with gr.Row():
112
-
113
- guidance_scale = gr.Slider(
114
- label="Guidance Scale",
115
- minimum=1,
116
- maximum=15,
117
- step=0.1,
118
- value=3.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=28,
127
- )
128
-
129
- gr.Examples(
130
- examples = examples,
131
- fn = infer,
132
- inputs = [prompt],
133
- outputs = [result, seed],
134
- cache_examples="lazy"
135
- )
136
 
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn = infer,
140
- inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
141
- outputs = [result, seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  )
143
 
 
144
  demo.launch()
 
1
  import gradio as gr
 
 
2
  import torch
3
+ from diffusers import DiffusionPipeline
4
  from PIL import Image
5
 
6
+ # Load the diffusion model
7
+ pipeline = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Set the model to the appropriate device
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ pipeline.to(device)
12
 
13
+ def generate_image(prompt, guidance_scale=7.5, num_inference_steps=50):
14
+ # Generate an image based on the prompt
15
+ with torch.no_grad():
16
+ # Generate images
17
+ images = pipeline(prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images
18
 
19
+ # Assuming pipeline returns a list of images, just take the first one
20
+ img = images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # Convert PIL image to format suitable for Gradio
23
+ return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # Set up Gradio interface
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("# Text to Image Generation")
28
+
29
+ with gr.Row():
30
+ prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here...")
31
+ guidance_scale = gr.Slider(minimum=1, maximum=15, step=0.1, value=7.5, label="Guidance Scale")
32
+ num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, value=50, label="Number of Inference Steps")
33
+
34
+ with gr.Row():
35
+ generate_button = gr.Button("Generate Image")
36
+
37
+ result = gr.Image(label="Generated Image")
38
+
39
+ # Connect the function to the button
40
+ generate_button.click(
41
+ fn=generate_image,
42
+ inputs=[prompt, guidance_scale, num_inference_steps],
43
+ outputs=result
44
  )
45
 
46
+ # Launch the app
47
  demo.launch()