alfredplpl commited on
Commit
ec3c4e8
1 Parent(s): c8ddecb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -112
app.py CHANGED
@@ -1,77 +1,62 @@
1
- # Thanks: https://huggingface.co/spaces/markmagic/Stable-Diffusion-3/blob/main/app.py
2
-
3
- import os
4
- import random
5
- import uuid
6
 
7
  import gradio as gr
8
  import numpy as np
9
- from PIL import Image
10
- import spaces
11
  import torch
12
- from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL
 
13
 
14
- DESCRIPTION = """# 日本語で入力できるStable Diffusion 3"""
 
15
 
16
- pipe = StableDiffusion3Pipeline.from_pretrained(
17
- "stabilityai/stable-diffusion-3-medium-diffusers",
18
- torch_dtype=torch.float16,
19
- token=os.getenv("TOKEN")
20
- )
21
 
22
- @spaces.GPU()
23
- def generate(
24
- prompt: str,
25
- negative_prompt: str = "",
26
- seed: int = 0,
27
- width: int = 1024,
28
- height: int = 1024,
29
- guidance_scale: float = 7,
30
- num_inference_steps=30,
31
- progress=gr.Progress(track_tqdm=True),
32
- ):
33
- pipe = pipe.to("cuda")
34
- generator = torch.Generator().manual_seed(seed)
35
-
36
- output = pipe(
37
- prompt=prompt,
38
- negative_prompt=negative_prompt,
39
- width=width,
40
- height=height,
41
- guidance_scale=guidance_scale,
42
- num_inference_steps=num_inference_steps,
43
- generator=generator,
44
- output_type="pil",
45
- ).images
46
 
47
- return output
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  examples = [
51
- "A red sofa on top of a white building.",
 
 
52
  ]
53
 
54
- css = '''
55
- .gradio-container{max-width: 1000px !important}
56
- h1{text-align:center}
57
- '''
 
 
 
58
  with gr.Blocks(css=css) as demo:
59
- with gr.Row():
60
- with gr.Column():
61
- gr.HTML(
62
- """
63
- <h1 style='text-align: center'>
64
- 日本語で入力できるStable Diffusion 3 Medium
65
- </h1>
66
- """
67
- )
68
- gr.HTML(
69
- """
70
-
71
- """
72
- )
73
- with gr.Group():
74
  with gr.Row():
 
75
  prompt = gr.Text(
76
  label="Prompt",
77
  show_label=False,
@@ -79,65 +64,74 @@ with gr.Blocks(css=css) as demo:
79
  placeholder="Enter your prompt",
80
  container=False,
81
  )
 
82
  run_button = gr.Button("Run", scale=0)
83
- result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
84
- with gr.Accordion("Advanced options", open=False):
85
- with gr.Row():
 
 
86
  negative_prompt = gr.Text(
87
  label="Negative prompt",
88
  max_lines=1,
89
- value = "deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
90
- visible=True,
91
  )
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- steps = gr.Slider(
101
- label="Steps",
102
- minimum=0,
103
- maximum=60,
104
- step=1,
105
- value=30,
106
- )
107
-
108
- with gr.Row():
109
- guidance_scale = gr.Slider(
110
- label="Guidance Scale",
111
- minimum=0.1,
112
- maximum=10,
113
- step=0.1,
114
- value=7.0,
115
  )
116
-
117
- gr.Examples(
118
- examples=examples,
119
- inputs=prompt,
120
- outputs=[result],
121
- fn=generate,
122
- cache_examples=CACHE_EXAMPLES,
123
- )
124
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  gr.on(
126
- triggers=[
127
- prompt.submit,
128
- negative_prompt.submit,
129
- run_button.click,
130
- ],
131
- fn=generate,
132
- inputs=[
133
- prompt,
134
- negative_prompt,
135
- seed,
136
- guidance_scale,
137
- steps,
138
- ],
139
- outputs=[result],
140
  )
141
 
142
- if __name__ == "__main__":
143
- demo.queue().launch()
 
 
 
 
 
 
1
 
2
  import gradio as gr
3
  import numpy as np
4
+ import random
 
5
  import torch
6
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
7
+ import spaces
8
 
9
+ device = "cuda"
10
+ dtype = torch.float16
11
 
12
+ repo = "stabilityai/stable-diffusion-3-medium"
13
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16, revision="refs/pr/26",token=os.environ["TOKEN"]).to(device)
 
 
 
14
 
15
+ MAX_SEED = np.iinfo(np.int32).max
16
+ MAX_IMAGE_SIZE = 1344
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ @spaces.GPU
19
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
20
 
21
+ if randomize_seed:
22
+ seed = random.randint(0, MAX_SEED)
23
+
24
+ generator = torch.Generator().manual_seed(seed)
25
+
26
+ image = pipe(
27
+ prompt = prompt,
28
+ negative_prompt = negative_prompt,
29
+ guidance_scale = guidance_scale,
30
+ num_inference_steps = num_inference_steps,
31
+ width = width,
32
+ height = height,
33
+ generator = generator
34
+ ).images[0]
35
+
36
+ return image, seed
37
 
38
  examples = [
39
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
40
+ "An astronaut riding a green horse",
41
+ "A delicious ceviche cheesecake slice",
42
  ]
43
 
44
+ css="""
45
+ #col-container {
46
+ margin: 0 auto;
47
+ max-width: 580px;
48
+ }
49
+ """
50
+
51
  with gr.Blocks(css=css) as demo:
52
+
53
+ with gr.Column(elem_id="col-container"):
54
+ gr.Markdown(f"""
55
+ # 日本語が入力できる [SD3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
56
+ """)
57
+
 
 
 
 
 
 
 
 
 
58
  with gr.Row():
59
+
60
  prompt = gr.Text(
61
  label="Prompt",
62
  show_label=False,
 
64
  placeholder="Enter your prompt",
65
  container=False,
66
  )
67
+
68
  run_button = gr.Button("Run", scale=0)
69
+
70
+ result = gr.Image(label="Result", show_label=False)
71
+
72
+ with gr.Accordion("Advanced Settings", open=False):
73
+
74
  negative_prompt = gr.Text(
75
  label="Negative prompt",
76
  max_lines=1,
77
+ placeholder="Enter a negative prompt",
 
78
  )
79
+
80
+ seed = gr.Slider(
81
+ label="Seed",
82
+ minimum=0,
83
+ maximum=MAX_SEED,
84
+ step=1,
85
+ value=0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  )
87
+
88
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
89
+
90
+ with gr.Row():
91
+
92
+ width = gr.Slider(
93
+ label="Width",
94
+ minimum=256,
95
+ maximum=MAX_IMAGE_SIZE,
96
+ step=64,
97
+ value=1024,
98
+ )
99
+
100
+ height = gr.Slider(
101
+ label="Height",
102
+ minimum=256,
103
+ maximum=MAX_IMAGE_SIZE,
104
+ step=64,
105
+ value=1024,
106
+ )
107
+
108
+ with gr.Row():
109
+
110
+ guidance_scale = gr.Slider(
111
+ label="Guidance scale",
112
+ minimum=0.0,
113
+ maximum=10.0,
114
+ step=0.1,
115
+ value=5.0,
116
+ )
117
+
118
+ num_inference_steps = gr.Slider(
119
+ label="Number of inference steps",
120
+ minimum=1,
121
+ maximum=50,
122
+ step=1,
123
+ value=28,
124
+ )
125
+
126
+ gr.Examples(
127
+ examples = examples,
128
+ inputs = [prompt]
129
+ )
130
  gr.on(
131
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
132
+ fn = infer,
133
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
134
+ outputs = [result, seed]
 
 
 
 
 
 
 
 
 
 
135
  )
136
 
137
+ demo.launch()