mightyneda commited on
Commit
afa1825
·
0 Parent(s):

application

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import 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("mann-e/mann-e_flux", subfolder="vae", torch_dtype=dtype).to(device)
15
+ pipe = DiffusionPipeline.from_pretrained("mann-e/mann-e_flux", 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=f"{prompt} mj-v6.1-style",
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 the moon",
43
+ "a cat holding a sign that says hello world",
44
+ "an anime illustration of a wiener schnitzel",
45
+ ]
46
+
47
+ css="""
48
+ #col-container {
49
+ margin: 0 auto;
50
+ max-width: 520px;
51
+ }
52
+ """
53
+
54
+ with gr.Blocks(css=css) as demo:
55
+
56
+ with gr.Column(elem_id="col-container"):
57
+ gr.Markdown(f"""# Mann-E Flux[Dev] Edition
58
+ """)
59
+
60
+ with gr.Row():
61
+
62
+ prompt = gr.Text(
63
+ label="Prompt",
64
+ show_label=False,
65
+ max_lines=1,
66
+ placeholder="Enter your prompt",
67
+ container=False,
68
+ )
69
+
70
+ run_button = gr.Button("Run", scale=0)
71
+
72
+ result = gr.Image(label="Result", show_label=False)
73
+
74
+ with gr.Accordion("Advanced Settings", open=False):
75
+
76
+ seed = gr.Slider(
77
+ label="Seed",
78
+ minimum=0,
79
+ maximum=MAX_SEED,
80
+ step=1,
81
+ value=0,
82
+ )
83
+
84
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
85
+
86
+ with gr.Row():
87
+
88
+ width = gr.Slider(
89
+ label="Width",
90
+ minimum=256,
91
+ maximum=MAX_IMAGE_SIZE,
92
+ step=32,
93
+ value=1024,
94
+ )
95
+
96
+ height = gr.Slider(
97
+ label="Height",
98
+ minimum=256,
99
+ maximum=MAX_IMAGE_SIZE,
100
+ step=32,
101
+ value=1024,
102
+ )
103
+
104
+ with gr.Row():
105
+
106
+ guidance_scale = gr.Slider(
107
+ label="Guidance Scale",
108
+ minimum=1,
109
+ maximum=15,
110
+ step=0.1,
111
+ value=3.5,
112
+ )
113
+
114
+ num_inference_steps = gr.Slider(
115
+ label="Number of inference steps",
116
+ minimum=8,
117
+ maximum=24,
118
+ step=1,
119
+ value=10,
120
+ )
121
+
122
+ gr.Examples(
123
+ examples = examples,
124
+ fn = infer,
125
+ inputs = [prompt],
126
+ outputs = [result, seed],
127
+ cache_examples="lazy"
128
+ )
129
+
130
+ gr.on(
131
+ triggers=[run_button.click, prompt.submit],
132
+ fn = infer,
133
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
134
+ outputs = [result, seed]
135
+ )
136
+
137
+ demo.launch()