prithivMLmods commited on
Commit
0a9e16c
1 Parent(s): 5c240d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -17
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- import spaces
5
  from diffusers import DiffusionPipeline
6
  import torch
 
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
  model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
@@ -46,7 +46,16 @@ style_list = [
46
  STYLE_NAMES = [style["name"] for style in style_list]
47
  DEFAULT_STYLE_NAME = STYLE_NAMES[0]
48
 
49
- @spaces.GPU(duration=60)
 
 
 
 
 
 
 
 
 
50
  def infer(
51
  prompt,
52
  negative_prompt="",
@@ -57,6 +66,7 @@ def infer(
57
  guidance_scale=0.0,
58
  num_inference_steps=4,
59
  style="Style Zero",
 
60
  progress=gr.Progress(track_tqdm=True),
61
  ):
62
  # Apply selected style
@@ -69,23 +79,37 @@ def infer(
69
 
70
  generator = torch.Generator().manual_seed(seed)
71
 
72
- image = pipe(
73
- prompt=styled_prompt,
74
- negative_prompt=styled_negative_prompt,
75
- guidance_scale=guidance_scale,
76
- num_inference_steps=num_inference_steps,
77
- width=width,
78
- height=height,
79
- generator=generator,
80
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- return image, seed
83
 
84
  examples = [
85
  "Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
86
  "3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
87
  "Cold coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5, 4K, Photo-Realistic",
88
- "Food photography of a milk shake with flying strawberrys against a pink background, professionally studio shot with cinematic lighting. The image is in the style of a professional studio shot --ar 85:128 --v 6.0 --style raw"
89
  ]
90
 
91
  css = '''
@@ -99,7 +123,7 @@ footer {
99
  with gr.Blocks(css=css, theme="prithivMLmods/Minecraft-Theme") as demo:
100
  with gr.Column(elem_id="col-container"):
101
  gr.Markdown("## SD3.5 TURBO")
102
-
103
  with gr.Row():
104
  prompt = gr.Text(
105
  label="Prompt",
@@ -112,7 +136,7 @@ with gr.Blocks(css=css, theme="prithivMLmods/Minecraft-Theme") as demo:
112
  run_button = gr.Button("Run", scale=0, variant="primary")
113
 
114
  result = gr.Image(label="Result", show_label=False)
115
-
116
  with gr.Row(visible=True):
117
  style_selection = gr.Radio(
118
  show_label=True,
@@ -123,6 +147,13 @@ with gr.Blocks(css=css, theme="prithivMLmods/Minecraft-Theme") as demo:
123
  label="Quality Style",
124
  )
125
 
 
 
 
 
 
 
 
126
  with gr.Accordion("Advanced Settings", open=False, visible=False):
127
  negative_prompt = gr.Text(
128
  label="Negative prompt",
@@ -180,7 +211,7 @@ with gr.Blocks(css=css, theme="prithivMLmods/Minecraft-Theme") as demo:
180
  outputs=[result, seed],
181
  fn=infer,
182
  cache_examples=True)
183
-
184
  gr.on(
185
  triggers=[run_button.click, prompt.submit],
186
  fn=infer,
@@ -194,9 +225,10 @@ with gr.Blocks(css=css, theme="prithivMLmods/Minecraft-Theme") as demo:
194
  guidance_scale,
195
  num_inference_steps,
196
  style_selection,
 
197
  ],
198
  outputs=[result, seed],
199
  )
200
 
201
  if __name__ == "__main__":
202
- demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
  from diffusers import DiffusionPipeline
5
  import torch
6
+ from PIL import Image
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
  model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
 
46
  STYLE_NAMES = [style["name"] for style in style_list]
47
  DEFAULT_STYLE_NAME = STYLE_NAMES[0]
48
 
49
+ grid_sizes = {
50
+ "2x1": (2, 1),
51
+ "1x2": (1, 2),
52
+ "2x2": (2, 2),
53
+ "2x3": (2, 3),
54
+ "3x2": (3, 2),
55
+ "1x1": (1, 1)
56
+ }
57
+
58
+ @gr.GPU(duration=60)
59
  def infer(
60
  prompt,
61
  negative_prompt="",
 
66
  guidance_scale=0.0,
67
  num_inference_steps=4,
68
  style="Style Zero",
69
+ grid_size="1x1",
70
  progress=gr.Progress(track_tqdm=True),
71
  ):
72
  # Apply selected style
 
79
 
80
  generator = torch.Generator().manual_seed(seed)
81
 
82
+ # Get grid size
83
+ grid_size_x, grid_size_y = grid_sizes.get(grid_size, (1, 1))
84
+ num_images = grid_size_x * grid_size_y
85
+
86
+ # Generate multiple images
87
+ options = {
88
+ "prompt": styled_prompt,
89
+ "negative_prompt": styled_negative_prompt,
90
+ "guidance_scale": guidance_scale,
91
+ "num_inference_steps": num_inference_steps,
92
+ "width": width,
93
+ "height": height,
94
+ "generator": generator,
95
+ }
96
+
97
+ torch.cuda.empty_cache() # Clear GPU memory
98
+ images = pipe(**options).images
99
+
100
+ # Create grid image
101
+ grid_img = Image.new('RGB', (width * grid_size_x, height * grid_size_y))
102
+
103
+ for i, img in enumerate(images[:num_images]):
104
+ grid_img.paste(img, (i % grid_size_x * width, i // grid_size_x * height))
105
 
106
+ return grid_img, seed
107
 
108
  examples = [
109
  "Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
110
  "3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
111
  "Cold coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5, 4K, Photo-Realistic",
112
+ "Food photography of a milk shake with flying strawberrys against a pink background, professionally studio shot with cinematic lighting. The image is in the style of a professional studio shot --ar 85:128 --v 6.0 --style raw"
113
  ]
114
 
115
  css = '''
 
123
  with gr.Blocks(css=css, theme="prithivMLmods/Minecraft-Theme") as demo:
124
  with gr.Column(elem_id="col-container"):
125
  gr.Markdown("## SD3.5 TURBO")
126
+
127
  with gr.Row():
128
  prompt = gr.Text(
129
  label="Prompt",
 
136
  run_button = gr.Button("Run", scale=0, variant="primary")
137
 
138
  result = gr.Image(label="Result", show_label=False)
139
+
140
  with gr.Row(visible=True):
141
  style_selection = gr.Radio(
142
  show_label=True,
 
147
  label="Quality Style",
148
  )
149
 
150
+ with gr.Row(visible=True):
151
+ grid_size_selection = gr.Dropdown(
152
+ choices=["2x1", "1x2", "2x2", "2x3", "3x2", "1x1"],
153
+ value="1x1",
154
+ label="Grid Size"
155
+ )
156
+
157
  with gr.Accordion("Advanced Settings", open=False, visible=False):
158
  negative_prompt = gr.Text(
159
  label="Negative prompt",
 
211
  outputs=[result, seed],
212
  fn=infer,
213
  cache_examples=True)
214
+
215
  gr.on(
216
  triggers=[run_button.click, prompt.submit],
217
  fn=infer,
 
225
  guidance_scale,
226
  num_inference_steps,
227
  style_selection,
228
+ grid_size_selection,
229
  ],
230
  outputs=[result, seed],
231
  )
232
 
233
  if __name__ == "__main__":
234
+ demo.launch()