multimodalart HF staff commited on
Commit
701027b
1 Parent(s): 258fac7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ dtype = torch.float16
9
+
10
+ repo = "diffusers-internal-dev/private-model"
11
+ transformer_repo= "diffusers-internal-dev/pm-revamp"
12
+
13
+ transformer = SD3Transformer2DModel.from_pretrained(transformer_repo, subfolder="transformer", torch_dtype=dtype)
14
+
15
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, transformer=transformer, torch_dtype=torch.float16).to(device)
16
+ pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config, shift=3.0)
17
+
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ MAX_IMAGE_SIZE = 1344
20
+
21
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
22
+
23
+ if randomize_seed:
24
+ seed = random.randint(0, MAX_SEED)
25
+
26
+ generator = torch.Generator().manual_seed(seed)
27
+
28
+ image = pipe(
29
+ prompt = prompt,
30
+ negative_prompt = negative_prompt,
31
+ guidance_scale = guidance_scale,
32
+ num_inference_steps = num_inference_steps,
33
+ width = width,
34
+ height = height,
35
+ generator = generator
36
+ ).images[0]
37
+
38
+ return image, seed
39
+
40
+ examples = [
41
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
+ "An astronaut riding a green horse",
43
+ "A delicious ceviche cheesecake slice",
44
+ ]
45
+
46
+ css="""
47
+ #col-container {
48
+ margin: 0 auto;
49
+ max-width: 520px;
50
+ }
51
+ """
52
+
53
+ with gr.Blocks(css=css) as demo:
54
+
55
+ with gr.Column(elem_id="col-container"):
56
+ gr.Markdown(f"""
57
+ # Stable Diffusion 3 Medium demo
58
+ Demo of [Stable Diffusion 3 Medium](#) - try out [Stable Assistant](https://stability.ai/stable-assistant) for chatting, [Stable Artisan](https://stability.ai/stable-artisan) for a discord bot, our API or run locally with [ComfyUI](#) or [diffusers](#)
59
+ """)
60
+
61
+ with gr.Row():
62
+
63
+ prompt = gr.Text(
64
+ label="Prompt",
65
+ show_label=False,
66
+ max_lines=1,
67
+ placeholder="Enter your prompt",
68
+ container=False,
69
+ )
70
+
71
+ run_button = gr.Button("Run", scale=0)
72
+
73
+ result = gr.Image(label="Result", show_label=False)
74
+
75
+ with gr.Accordion("Advanced Settings", open=False):
76
+
77
+ negative_prompt = gr.Text(
78
+ label="Negative prompt",
79
+ max_lines=1,
80
+ placeholder="Enter a negative prompt",
81
+ )
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=64,
100
+ value=1024,
101
+ )
102
+
103
+ height = gr.Slider(
104
+ label="Height",
105
+ minimum=256,
106
+ maximum=MAX_IMAGE_SIZE,
107
+ step=64,
108
+ value=1024,
109
+ )
110
+
111
+ with gr.Row():
112
+
113
+ guidance_scale = gr.Slider(
114
+ label="Guidance scale",
115
+ minimum=0.0,
116
+ maximum=10.0,
117
+ step=0.1,
118
+ value=4.0,
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
+ inputs = [prompt]
132
+ )
133
+ gr.on(
134
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
135
+ fn = infer,
136
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
137
+ outputs = [result, seed]
138
+ )