multimodalart HF Staff commited on
Commit
5e673fa
·
verified ·
1 Parent(s): 18fcd23

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ pipe = SanaSprintPipeline.from_pretrained(
14
+ "Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers",
15
+ torch_dtype=torch.bfloat16
16
+ )
17
+ pipe.to(device)
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ MAX_IMAGE_SIZE = 1024
20
+
21
+ @spaces.GPU(duration=5)
22
+ 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)):
23
+ if randomize_seed:
24
+ seed = random.randint(0, MAX_SEED)
25
+ generator = torch.Generator().manual_seed(seed)
26
+
27
+ img = pipe(
28
+ prompt=prompt,
29
+ guidance_scale=guidance_scale,
30
+ num_inference_steps=num_inference_steps,
31
+ width=width,
32
+ height=height,
33
+ generator=generator,
34
+ output_type="pil",
35
+ good_vae=good_vae,
36
+ )
37
+ return img[0], seed
38
+
39
+ examples = [
40
+ "a tiny astronaut hatching from an egg on the moon",
41
+ "a cat holding a sign that says hello world",
42
+ "an anime illustration of a wiener schnitzel",
43
+ ]
44
+
45
+ css="""
46
+ #col-container {
47
+ margin: 0 auto;
48
+ max-width: 520px;
49
+ }
50
+ """
51
+
52
+ with gr.Blocks(css=css) as demo:
53
+
54
+ with gr.Column(elem_id="col-container"):
55
+ gr.Markdown(f"""# Sana Sprint""")
56
+
57
+ with gr.Row():
58
+
59
+ prompt = gr.Text(
60
+ label="Prompt",
61
+ show_label=False,
62
+ max_lines=1,
63
+ placeholder="Enter your prompt",
64
+ container=False,
65
+ )
66
+
67
+ run_button = gr.Button("Run", scale=0)
68
+
69
+ result = gr.Image(label="Result", show_label=False)
70
+
71
+ with gr.Accordion("Advanced Settings", open=False):
72
+
73
+ seed = gr.Slider(
74
+ label="Seed",
75
+ minimum=0,
76
+ maximum=MAX_SEED,
77
+ step=1,
78
+ value=0,
79
+ )
80
+
81
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
82
+
83
+ with gr.Row():
84
+
85
+ width = gr.Slider(
86
+ label="Width",
87
+ minimum=256,
88
+ maximum=MAX_IMAGE_SIZE,
89
+ step=32,
90
+ value=1024,
91
+ )
92
+
93
+ height = gr.Slider(
94
+ label="Height",
95
+ minimum=256,
96
+ maximum=MAX_IMAGE_SIZE,
97
+ step=32,
98
+ value=1024,
99
+ )
100
+
101
+ with gr.Row():
102
+
103
+ guidance_scale = gr.Slider(
104
+ label="Guidance Scale",
105
+ minimum=1,
106
+ maximum=15,
107
+ step=0.1,
108
+ value=1,
109
+ )
110
+
111
+ num_inference_steps = gr.Slider(
112
+ label="Number of inference steps",
113
+ minimum=1,
114
+ maximum=50,
115
+ step=1,
116
+ value=28,
117
+ )
118
+
119
+ gr.Examples(
120
+ examples = examples,
121
+ fn = infer,
122
+ inputs = [prompt],
123
+ outputs = [result, seed],
124
+ cache_examples="lazy"
125
+ )
126
+
127
+ gr.on(
128
+ triggers=[run_button.click, prompt.submit],
129
+ fn = infer,
130
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
131
+ outputs = [result, seed]
132
+ )
133
+
134
+ demo.launch()