amirkhanbloch commited on
Commit
20cedfd
·
verified ·
1 Parent(s): 951edd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -3,50 +3,57 @@ import spaces
3
  import numpy as np
4
  import random
5
  import torch
6
- from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
7
 
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
  dtype = torch.float16
10
 
 
11
  repo = "stabilityai/stable-diffusion-3-medium-diffusers"
12
- pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)
13
 
 
14
  MAX_SEED = np.iinfo(np.int32).max
15
  MAX_IMAGE_SIZE = 1344
16
 
17
  @spaces.GPU
18
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
19
-
20
  if randomize_seed:
21
  seed = random.randint(0, MAX_SEED)
22
 
23
  generator = torch.Generator().manual_seed(seed)
24
 
25
- image = pipe(
26
- prompt = prompt,
27
- negative_prompt = negative_prompt,
28
- guidance_scale = guidance_scale,
29
- num_inference_steps = num_inference_steps,
30
- width = width,
31
- height = height,
32
- generator = generator
33
- ).images[0]
34
-
35
- return image, seed
 
 
 
36
 
 
37
  examples = [
38
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
39
  "An astronaut riding a green horse",
40
  "A delicious ceviche cheesecake slice",
41
  ]
42
 
43
- css="""
44
  #col-container {
45
  margin: 0 auto;
46
  max-width: 580px;
47
  }
48
  """
49
 
 
50
  with gr.Blocks(css=css) as demo:
51
 
52
  with gr.Column(elem_id="col-container"):
@@ -56,7 +63,6 @@ with gr.Blocks(css=css) as demo:
56
  """)
57
 
58
  with gr.Row():
59
-
60
  prompt = gr.Text(
61
  label="Prompt",
62
  show_label=False,
@@ -70,7 +76,6 @@ with gr.Blocks(css=css) as demo:
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,
@@ -88,7 +93,6 @@ with gr.Blocks(css=css) as demo:
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,
@@ -106,7 +110,6 @@ with gr.Blocks(css=css) as demo:
106
  )
107
 
108
  with gr.Row():
109
-
110
  guidance_scale = gr.Slider(
111
  label="Guidance scale",
112
  minimum=0.0,
@@ -124,14 +127,16 @@ with gr.Blocks(css=css) as demo:
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()
 
 
3
  import numpy as np
4
  import random
5
  import torch
6
+ from diffusers import StableDiffusion3Pipeline
7
 
8
+ # Set device and data type
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  dtype = torch.float16
11
 
12
+ # Load the Stable Diffusion model
13
  repo = "stabilityai/stable-diffusion-3-medium-diffusers"
14
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=dtype).to(device)
15
 
16
+ # Constants
17
  MAX_SEED = np.iinfo(np.int32).max
18
  MAX_IMAGE_SIZE = 1344
19
 
20
  @spaces.GPU
21
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
 
22
  if randomize_seed:
23
  seed = random.randint(0, MAX_SEED)
24
 
25
  generator = torch.Generator().manual_seed(seed)
26
 
27
+ try:
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
+ except Exception as e:
40
+ return str(e), seed # Return error message if any
41
 
42
+ # Example prompts
43
  examples = [
44
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
45
  "An astronaut riding a green horse",
46
  "A delicious ceviche cheesecake slice",
47
  ]
48
 
49
+ css = """
50
  #col-container {
51
  margin: 0 auto;
52
  max-width: 580px;
53
  }
54
  """
55
 
56
+ # Create Gradio interface
57
  with gr.Blocks(css=css) as demo:
58
 
59
  with gr.Column(elem_id="col-container"):
 
63
  """)
64
 
65
  with gr.Row():
 
66
  prompt = gr.Text(
67
  label="Prompt",
68
  show_label=False,
 
76
  result = gr.Image(label="Result", show_label=False)
77
 
78
  with gr.Accordion("Advanced Settings", open=False):
 
79
  negative_prompt = gr.Text(
80
  label="Negative prompt",
81
  max_lines=1,
 
93
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
 
95
  with gr.Row():
 
96
  width = gr.Slider(
97
  label="Width",
98
  minimum=256,
 
110
  )
111
 
112
  with gr.Row():
 
113
  guidance_scale = gr.Slider(
114
  label="Guidance scale",
115
  minimum=0.0,
 
127
  )
128
 
129
  gr.Examples(
130
+ examples=examples,
131
+ inputs=[prompt]
132
  )
133
+
134
  gr.on(
135
  triggers=[run_button.click, prompt.submit, negative_prompt.submit],
136
+ fn=infer,
137
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
138
+ outputs=[result, seed]
139
  )
140
 
141
+ # Launch the app
142
+ demo.launch(timeout=10) # Increase timeout if needed