-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
140 lines (121 loc) · 3.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import torch
import accelerate
import sentencepiece
import uuid
from diffusers import FluxPipeline
import ipywidgets as widgets
from IPython.display import display, clear_output
import matplotlib.pyplot as plt
def setup_pipeline_and_widgets():
# Initialize the pipeline once
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16)
pipe.vae.enable_tiling()
pipe.enable_sequential_cpu_offload()
clear_output()
# Define the function to set the generator based on the random_seed checkbox
def set_generator(random, seed_value):
return torch.Generator("cpu").manual_seed(0) if random else torch.Generator("cpu").manual_seed(seed_value)
html_widget = widgets.HTML(
value=(
"<i>Made by <a href='https://www.youtube.com/@marat_ai' target='_blank'>marat_ai</a>, "
"<a href='https://www.patreon.com/marat_ai' target='_blank'>more_notebooks</a></i>"
),
placeholder='Some HTML')
# Define the prompt textarea
prompt = widgets.Textarea(
value='a cat',
description='Prompt',
disabled=False,
layout=widgets.Layout(width='40%', height='100px')
)
# Define the width slider
width = widgets.IntSlider(
value=1024,
min=8,
max=2048,
step=8,
description='Width',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d'
)
# Define the height slider
height = widgets.IntSlider(
value=1024,
min=8,
max=2048,
step=8,
description='Height',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d'
)
# Define the number of inference steps input
num_inference_steps = widgets.IntText(
value=4,
min=0,
max=5,
step=1,
description='Steps',
disabled=False
)
guidance_scale = widgets.IntText(
value=0.0,
min=0,
max=10,
step=0.5,
description='SFG scale',
disabled=False
)
# Define the seed input
seed = widgets.IntText(
value=1,
min=0,
max=9999999999999999999999999,
step=1,
description='Seed',
disabled=False
)
# Define the random seed checkbox
random_seed = widgets.Checkbox(
value=False,
description='Random Seed',
disabled=False,
indent=False
)
# Define the generate button
generate_button = widgets.Button(
description='Generate',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Generate image',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
# Define an output widget for the image
output = widgets.Output()
# Define the function to generate and display the image
def generate_image(button):
with output:
clear_output(wait=True)
generator = set_generator(random_seed.value, seed.value)
image = pipe(
prompt=prompt.value,
num_inference_steps=num_inference_steps.value,
guidance_scale=guidance_scale.value,
generator=generator,
width=width.value,
height=height.value
).images[0]
uid = uuid.uuid4()
image.save(f"{uid}.png")
plt.imshow(image)
plt.axis('off')
plt.show()
# Bind the function to the generate button click event
generate_button.on_click(generate_image)
# Display the widgets and output
display(html_widget, prompt, num_inference_steps, guidance_scale, width, height, seed, random_seed, generate_button, output)