prithivMLmods commited on
Commit
fb0589f
·
verified ·
1 Parent(s): 6856e8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -76
app.py CHANGED
@@ -9,13 +9,11 @@ import spaces
9
  import torch
10
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
11
 
12
- DESCRIPTIONx = """## STABLE HAMSTER 🐹
13
-
14
- """
15
 
16
  css = '''
17
  .gradio-container {
18
- max-width: 560px !important;
19
  margin: 0 auto !important;
20
  }
21
  h1 {
@@ -34,13 +32,12 @@ examples = [
34
  "Commercial photography, giant burger, white lighting, studio light, 8k octane rendering, high resolution photography, insanely detailed, fine details, on white isolated plain, 8k, commercial photography, stock photo, professional color grading, --v 4 --ar 9:16 "
35
  ]
36
 
37
- MODEL_ID = os.getenv("MODEL_VAL_PATH") # uses SG161222/RealVisXL_V5.0_Lightning or SG161222/RealVisXL_V4.0_Lightning
38
  MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
39
  USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
40
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
41
- BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
42
 
43
- # Load model outside of function
44
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
45
  pipe = StableDiffusionXLPipeline.from_pretrained(
46
  MODEL_ID,
@@ -50,11 +47,9 @@ pipe = StableDiffusionXLPipeline.from_pretrained(
50
  ).to(device)
51
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
52
 
53
- # Compile speedup
54
  if USE_TORCH_COMPILE:
55
  pipe.compile()
56
 
57
- # Offloading capacity (RAM)
58
  if ENABLE_CPU_OFFLOAD:
59
  pipe.enable_model_cpu_offload()
60
 
@@ -82,13 +77,12 @@ def generate(
82
  num_inference_steps: int = 25,
83
  randomize_seed: bool = False,
84
  use_resolution_binning: bool = True,
85
- num_images: int = 4, # Number of images to generate
86
  progress=gr.Progress(track_tqdm=True),
87
  ):
88
  seed = int(randomize_seed_fn(seed, randomize_seed))
89
  generator = torch.Generator(device=device).manual_seed(seed)
90
 
91
- # Options
92
  options = {
93
  "prompt": [prompt] * num_images,
94
  "negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
@@ -103,7 +97,6 @@ def generate(
103
  if use_resolution_binning:
104
  options["use_resolution_binning"] = True
105
 
106
- # Generate images in batches
107
  images = []
108
  for i in range(0, num_images, BATCH_SIZE):
109
  batch_options = options.copy()
@@ -117,6 +110,7 @@ def generate(
117
 
118
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
119
  gr.Markdown(DESCRIPTIONx)
 
120
  with gr.Row():
121
  with gr.Column(scale=3):
122
  with gr.Row():
@@ -128,72 +122,70 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
128
  )
129
  run_button = gr.Button("Run", scale=0, variant="primary")
130
 
131
- # Changed from gr.Image to gr.Gallery to handle multiple images
132
  result = gr.Gallery(label="Result", columns=2, show_label=False)
133
-
134
- with gr.Accordion("Advanced options", open=False, visible=True):
135
- num_images = gr.Slider(
136
- label="Number of Images",
137
- minimum=1,
138
- maximum=4,
139
- step=1,
140
- value=4,
141
- )
142
- with gr.Row():
143
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
144
- negative_prompt = gr.Text(
145
- label="Negative prompt",
146
- max_lines=5,
147
- lines=4,
148
- placeholder="Enter a negative prompt",
149
- value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
150
- visible=True,
151
- )
152
- seed = gr.Slider(
153
- label="Seed",
154
- minimum=0,
155
- maximum=MAX_SEED,
156
- step=1,
157
- value=0,
158
- )
159
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
160
- with gr.Row(visible=True):
161
- width = gr.Slider(
162
- label="Width",
163
- minimum=512,
164
- maximum=MAX_IMAGE_SIZE,
165
- step=64,
166
- value=1024,
167
- )
168
- height = gr.Slider(
169
- label="Height",
170
- minimum=512,
171
- maximum=MAX_IMAGE_SIZE,
172
- step=64,
173
- value=1024,
174
- )
175
- with gr.Row():
176
- guidance_scale = gr.Slider(
177
- label="Guidance Scale",
178
- minimum=0.1,
179
- maximum=6,
180
- step=0.1,
181
- value=3.0,
182
- )
183
- num_inference_steps = gr.Slider(
184
- label="Number of inference steps",
185
- minimum=1,
186
- maximum=25,
187
- step=1,
188
- value=23,
189
- )
190
 
191
- with gr.Column(scale=1): # Examples on right
192
- gr.Examples(
193
- examples=examples,
194
- inputs=prompt,
195
- cache_examples=False,
196
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  use_negative_prompt.change(
199
  fn=lambda x: gr.update(visible=x),
 
9
  import torch
10
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
11
 
12
+ DESCRIPTIONx = """## STABLE HAMSTER 🐹"""
 
 
13
 
14
  css = '''
15
  .gradio-container {
16
+ max-width: 1000px !important;
17
  margin: 0 auto !important;
18
  }
19
  h1 {
 
32
  "Commercial photography, giant burger, white lighting, studio light, 8k octane rendering, high resolution photography, insanely detailed, fine details, on white isolated plain, 8k, commercial photography, stock photo, professional color grading, --v 4 --ar 9:16 "
33
  ]
34
 
35
+ MODEL_ID = os.getenv("MODEL_VAL_PATH") # SG161222/RealVisXL_V5.0_Lightning or SG161222/RealVisXL_V4.0_Lightning
36
  MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
37
  USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
38
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
39
+ BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
40
 
 
41
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
42
  pipe = StableDiffusionXLPipeline.from_pretrained(
43
  MODEL_ID,
 
47
  ).to(device)
48
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
49
 
 
50
  if USE_TORCH_COMPILE:
51
  pipe.compile()
52
 
 
53
  if ENABLE_CPU_OFFLOAD:
54
  pipe.enable_model_cpu_offload()
55
 
 
77
  num_inference_steps: int = 25,
78
  randomize_seed: bool = False,
79
  use_resolution_binning: bool = True,
80
+ num_images: int = 4,
81
  progress=gr.Progress(track_tqdm=True),
82
  ):
83
  seed = int(randomize_seed_fn(seed, randomize_seed))
84
  generator = torch.Generator(device=device).manual_seed(seed)
85
 
 
86
  options = {
87
  "prompt": [prompt] * num_images,
88
  "negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
 
97
  if use_resolution_binning:
98
  options["use_resolution_binning"] = True
99
 
 
100
  images = []
101
  for i in range(0, num_images, BATCH_SIZE):
102
  batch_options = options.copy()
 
110
 
111
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
112
  gr.Markdown(DESCRIPTIONx)
113
+
114
  with gr.Row():
115
  with gr.Column(scale=3):
116
  with gr.Row():
 
122
  )
123
  run_button = gr.Button("Run", scale=0, variant="primary")
124
 
 
125
  result = gr.Gallery(label="Result", columns=2, show_label=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ with gr.Accordion("Advanced options", open=False, visible=True):
128
+ num_images = gr.Slider(
129
+ label="Number of Images",
130
+ minimum=1,
131
+ maximum=4,
132
+ step=1,
133
+ value=4,
134
+ )
135
+ with gr.Row():
136
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
137
+ negative_prompt = gr.Text(
138
+ label="Negative prompt",
139
+ max_lines=5,
140
+ lines=4,
141
+ placeholder="Enter a negative prompt",
142
+ value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
143
+ visible=True,
144
+ )
145
+ seed = gr.Slider(
146
+ label="Seed",
147
+ minimum=0,
148
+ maximum=MAX_SEED,
149
+ step=1,
150
+ value=0,
151
+ )
152
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
153
+ with gr.Row(visible=True):
154
+ width = gr.Slider(
155
+ label="Width",
156
+ minimum=512,
157
+ maximum=MAX_IMAGE_SIZE,
158
+ step=64,
159
+ value=1024,
160
+ )
161
+ height = gr.Slider(
162
+ label="Height",
163
+ minimum=512,
164
+ maximum=MAX_IMAGE_SIZE,
165
+ step=64,
166
+ value=1024,
167
+ )
168
+ with gr.Row():
169
+ guidance_scale = gr.Slider(
170
+ label="Guidance Scale",
171
+ minimum=0.1,
172
+ maximum=6,
173
+ step=0.1,
174
+ value=3.0,
175
+ )
176
+ num_inference_steps = gr.Slider(
177
+ label="Number of inference steps",
178
+ minimum=1,
179
+ maximum=25,
180
+ step=1,
181
+ value=23,
182
+ )
183
+ with gr.Column(scale=1):
184
+ gr.Examples(
185
+ examples=examples,
186
+ inputs=prompt,
187
+ cache_examples=False,
188
+ )
189
 
190
  use_negative_prompt.change(
191
  fn=lambda x: gr.update(visible=x),