Abinivesh commited on
Commit
729b2e0
·
verified ·
1 Parent(s): 7020dbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -1
app.py CHANGED
@@ -1,3 +1,151 @@
1
  import gradio as gr
 
 
2
 
3
- gr.load("models/stabilityai/stable-diffusion-3.5-large").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
 
5
+ import spaces
6
+ from diffusers import DiffusionPipeline
7
+ import torch
8
+
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ model_repo_id = "stabilityai/stable-diffusion-3.5-large"
11
+
12
+ if torch.cuda.is_available():
13
+ torch_dtype = torch.bfloat16
14
+ else:
15
+ torch_dtype = torch.float32
16
+
17
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
+ pipe = pipe.to(device)
19
+
20
+ MAX_SEED = np.iinfo(np.int32).max
21
+ MAX_IMAGE_SIZE = 1024
22
+
23
+ @spaces.GPU(duration=65)
24
+ def infer(
25
+ prompt,
26
+ negative_prompt="",
27
+ seed=42,
28
+ randomize_seed=False,
29
+ width=1024,
30
+ height=1024,
31
+ guidance_scale=4.5,
32
+ num_inference_steps=40,
33
+ progress=gr.Progress(track_tqdm=True),
34
+ ):
35
+ if randomize_seed:
36
+ seed = random.randint(0, MAX_SEED)
37
+
38
+ generator = torch.Generator().manual_seed(seed)
39
+
40
+ image = pipe(
41
+ prompt=prompt,
42
+ negative_prompt=negative_prompt,
43
+ guidance_scale=guidance_scale,
44
+ num_inference_steps=num_inference_steps,
45
+ width=width,
46
+ height=height,
47
+ generator=generator,
48
+ ).images[0]
49
+
50
+ return image, seed
51
+
52
+
53
+ examples = [
54
+ "A capybara wearing a suit holding a sign that reads Hello World",
55
+ ]
56
+
57
+ css = """
58
+ #col-container {
59
+ margin: 0 auto;
60
+ max-width: 640px;
61
+ }
62
+ """
63
+
64
+ with gr.Blocks(css=css) as demo:
65
+ with gr.Column(elem_id="col-container"):
66
+ gr.Markdown(" # [Stable Diffusion 3.5 Large (8B)](https://huggingface.co/stabilityai/stable-diffusion-3.5-large)")
67
+ gr.Markdown("[Learn more](https://stability.ai/news/introducing-stable-diffusion-3-5) about the Stable Diffusion 3.5 series. Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), or [download model](https://huggingface.co/stabilityai/stable-diffusion-3.5-large) to run locally with ComfyUI or diffusers.")
68
+ with gr.Row():
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, variant="primary")
78
+
79
+ result = gr.Image(label="Result", show_label=False)
80
+
81
+ with gr.Accordion("Advanced Settings", open=False):
82
+ negative_prompt = gr.Text(
83
+ label="Negative prompt",
84
+ max_lines=1,
85
+ placeholder="Enter a negative prompt",
86
+ visible=False,
87
+ )
88
+
89
+ seed = gr.Slider(
90
+ label="Seed",
91
+ minimum=0,
92
+ maximum=MAX_SEED,
93
+ step=1,
94
+ value=0,
95
+ )
96
+
97
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
+
99
+ with gr.Row():
100
+ width = gr.Slider(
101
+ label="Width",
102
+ minimum=512,
103
+ maximum=MAX_IMAGE_SIZE,
104
+ step=32,
105
+ value=1024,
106
+ )
107
+
108
+ height = gr.Slider(
109
+ label="Height",
110
+ minimum=512,
111
+ maximum=MAX_IMAGE_SIZE,
112
+ step=32,
113
+ value=1024,
114
+ )
115
+
116
+ with gr.Row():
117
+ guidance_scale = gr.Slider(
118
+ label="Guidance scale",
119
+ minimum=0.0,
120
+ maximum=7.5,
121
+ step=0.1,
122
+ value=4.5,
123
+ )
124
+
125
+ num_inference_steps = gr.Slider(
126
+ label="Number of inference steps",
127
+ minimum=1,
128
+ maximum=50,
129
+ step=1,
130
+ value=40,
131
+ )
132
+
133
+ gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
134
+ gr.on(
135
+ triggers=[run_button.click, prompt.submit],
136
+ fn=infer,
137
+ inputs=[
138
+ prompt,
139
+ negative_prompt,
140
+ seed,
141
+ randomize_seed,
142
+ width,
143
+ height,
144
+ guidance_scale,
145
+ num_inference_steps,
146
+ ],
147
+ outputs=[result, seed],
148
+ )
149
+
150
+ if __name__ == "__main__":
151
+ demo.launch()