kasper-boy commited on
Commit
208fcb6
1 Parent(s): 5887921

Update main_app.py

Browse files
Files changed (1) hide show
  1. main_app.py +78 -107
main_app.py CHANGED
@@ -37,19 +37,16 @@ style_list = [
37
  "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
38
  "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
39
  },
40
-
41
  {
42
  "name": "Photo",
43
  "prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
44
  "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
45
- },
46
-
47
  {
48
  "name": "Cinematic",
49
  "prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
50
  "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
51
  },
52
-
53
  {
54
  "name": "Anime",
55
  "prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
@@ -77,25 +74,11 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
77
  negative = ""
78
  return p.replace("{prompt}", positive), n + negative
79
 
80
- DESCRIPTIONs = """
81
- # 🖼️ AI Image Generator
82
-
83
- Generate stunning images using various styles with advanced AI technology. Customize prompts, negative prompts, and other parameters to create your unique masterpiece.
84
-
85
- *Note: Running on CPU may take longer than on GPU.*
86
- """
87
-
88
- DESCRIPTION = """
89
- ### Powered by AI
90
-
91
- This application leverages cutting-edge AI models to produce high-quality images based on your prompts.
92
-
93
- ---
94
-
95
- """
96
 
97
  if not torch.cuda.is_available():
98
- DESCRIPTION += "<p style='color: red;'><strong>⚠️ Warning:</strong> Running on CPU, this may take longer to generate images.</p>"
99
 
100
  MAX_SEED = np.iinfo(np.int32).max
101
  CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
@@ -126,30 +109,14 @@ if torch.cuda.is_available():
126
  pipe.enable_model_cpu_offload()
127
  pipe2.enable_model_cpu_offload()
128
  else:
129
- pipe.to(device)
130
- pipe2.to(device)
131
  print("Loaded on Device!")
132
 
133
  if USE_TORCH_COMPILE:
134
  pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
135
  pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
136
  print("Model Compiled!")
137
- else:
138
- pipe = DiffusionPipeline.from_pretrained(
139
- "SG161222/RealVisXL_V4.0",
140
- torch_dtype=torch.float32,
141
- use_safetensors=True,
142
- add_watermarker=False
143
- )
144
- pipe2 = DiffusionPipeline.from_pretrained(
145
- "SG161222/RealVisXL_V3.0",
146
- torch_dtype=torch.float32,
147
- use_safetensors=True,
148
- add_watermarker=False
149
- )
150
- pipe.to(device)
151
- pipe2.to(device)
152
- print("Loaded on Device!")
153
 
154
  def save_image(img):
155
  unique_name = str(uuid.uuid4()) + ".png"
@@ -161,7 +128,7 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
161
  seed = random.randint(0, MAX_SEED)
162
  return seed
163
 
164
- @spaces.GPU(enable_queue=True)
165
  def generate(
166
  prompt: str,
167
  negative_prompt: str = "",
@@ -184,7 +151,7 @@ def generate(
184
 
185
  if not use_negative_prompt:
186
  negative_prompt = "" # type: ignore
187
- negative_prompt += default_negative
188
 
189
  options = {
190
  "prompt": prompt,
@@ -212,66 +179,78 @@ examples = [
212
  ]
213
 
214
  css = '''
215
- body {
216
- background: #2e2e2e;
217
- color: #f0f0f0;
218
- font-family: 'Arial', sans-serif;
219
- }
220
-
221
  .gradio-container {
222
  max-width: 800px !important;
223
- margin: 0 auto;
224
- padding: 20px;
225
- border: 2px solid #444;
226
- border-radius: 10px;
227
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
228
  }
229
-
230
- .gradio-button {
231
- background-color: #5a9;
 
 
 
 
 
 
 
 
232
  border: none;
 
 
 
 
 
 
 
 
 
233
  padding: 10px 20px;
234
- font-size: 16px;
235
  color: white;
 
236
  border-radius: 5px;
237
  cursor: pointer;
238
- margin-top: 10px;
239
  }
240
-
241
  .gradio-button:hover {
242
- background-color: #48c;
243
  }
244
-
245
- h1, h2, h3, h4, h5, h6 {
246
- color: #ddd;
 
 
247
  }
248
-
249
- .gradio-textbox {
250
- background-color: #444;
251
- color: #f0f0f0;
252
- border: 1px solid #555;
253
  }
254
-
255
- .gradio-slider {
256
- background-color: #444;
257
- color: #f0f0f0;
258
  }
259
-
260
- .gradio-radio {
261
- background-color: #444;
262
- color: #f0f0f0;
263
  }
264
-
265
- .gradio-gallery {
266
- background-color: #444;
267
- color: #f0f0f0;
268
- border: 1px solid #555;
269
  }
270
-
271
- .gradio-accordion {
272
- background-color: #444;
273
- color: #f0f0f0;
274
- border: 1px solid #555;
 
 
 
 
 
 
 
 
 
 
 
275
  }
276
  '''
277
 
@@ -286,10 +265,10 @@ with gr.Blocks(css=css, theme="default") as demo:
286
  placeholder="Enter your prompt",
287
  container=True
288
  )
289
- run_button = gr.Button("Run", css=".gradio-button")
290
- result = gr.Gallery(label="Result", columns=2, preview=True, css=".gradio-gallery { width: 100%; }")
291
- with gr.Accordion("Advanced options", open=False, css=".gradio-accordion"):
292
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True, css=".gradio-checkbox")
293
  negative_prompt = gr.Textbox(
294
  label="Negative prompt",
295
  max_lines=1,
@@ -303,8 +282,7 @@ with gr.Blocks(css=css, theme="default") as demo:
303
  minimum=10,
304
  maximum=60,
305
  step=1,
306
- value=30,
307
- css=".gradio-slider"
308
  )
309
  with gr.Row():
310
  num_images_per_prompt = gr.Slider(
@@ -312,8 +290,7 @@ with gr.Blocks(css=css, theme="default") as demo:
312
  minimum=1,
313
  maximum=5,
314
  step=1,
315
- value=2,
316
- css=".gradio-slider"
317
  )
318
  seed = gr.Slider(
319
  label="Seed",
@@ -321,26 +298,23 @@ with gr.Blocks(css=css, theme="default") as demo:
321
  maximum=MAX_SEED,
322
  step=1,
323
  value=0,
324
- visible=True,
325
- css=".gradio-slider"
326
  )
327
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True, css=".gradio-checkbox")
328
  with gr.Row(visible=True):
329
  width = gr.Slider(
330
  label="Width",
331
  minimum=512,
332
  maximum=2048,
333
  step=8,
334
- value=1024,
335
- css=".gradio-slider"
336
  )
337
  height = gr.Slider(
338
  label="Height",
339
  minimum=512,
340
  maximum=2048,
341
  step=8,
342
- value=1024,
343
- css=".gradio-slider"
344
  )
345
  with gr.Row():
346
  guidance_scale = gr.Slider(
@@ -348,8 +322,7 @@ with gr.Blocks(css=css, theme="default") as demo:
348
  minimum=0.1,
349
  maximum=20.0,
350
  step=0.1,
351
- value=6,
352
- css=".gradio-slider"
353
  )
354
  with gr.Row(visible=True):
355
  style_selection = gr.Radio(
@@ -358,16 +331,14 @@ with gr.Blocks(css=css, theme="default") as demo:
358
  interactive=True,
359
  choices=STYLE_NAMES,
360
  value=DEFAULT_STYLE_NAME,
361
- label="Image Style",
362
- css=".gradio-radio"
363
  )
364
  gr.Examples(
365
  examples=examples,
366
  inputs=prompt,
367
  outputs=[result, seed],
368
  fn=generate,
369
- cache_examples=CACHE_EXAMPLES,
370
- css=".gradio-examples"
371
  )
372
 
373
  use_negative_prompt.change(
@@ -399,6 +370,6 @@ with gr.Blocks(css=css, theme="default") as demo:
399
  api_name="run",
400
  )
401
  gr.Markdown(DESCRIPTION)
 
402
  if __name__ == "__main__":
403
  demo.queue(max_size=20).launch()
404
-
 
37
  "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
38
  "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
39
  },
 
40
  {
41
  "name": "Photo",
42
  "prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
43
  "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
44
+ },
 
45
  {
46
  "name": "Cinematic",
47
  "prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
48
  "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
49
  },
 
50
  {
51
  "name": "Anime",
52
  "prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
 
74
  negative = ""
75
  return p.replace("{prompt}", positive), n + negative
76
 
77
+ DESCRIPTIONs = """ㅤㅤㅤ """
78
+ DESCRIPTION = """ㅤㅤㅤ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  if not torch.cuda.is_available():
81
+ DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
82
 
83
  MAX_SEED = np.iinfo(np.int32).max
84
  CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
 
109
  pipe.enable_model_cpu_offload()
110
  pipe2.enable_model_cpu_offload()
111
  else:
112
+ pipe.to(device)
113
+ pipe2.to(device)
114
  print("Loaded on Device!")
115
 
116
  if USE_TORCH_COMPILE:
117
  pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
118
  pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
119
  print("Model Compiled!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  def save_image(img):
122
  unique_name = str(uuid.uuid4()) + ".png"
 
128
  seed = random.randint(0, MAX_SEED)
129
  return seed
130
 
131
+ @spaces.GPU()
132
  def generate(
133
  prompt: str,
134
  negative_prompt: str = "",
 
151
 
152
  if not use_negative_prompt:
153
  negative_prompt = "" # type: ignore
154
+ negative_prompt += default_negative
155
 
156
  options = {
157
  "prompt": prompt,
 
179
  ]
180
 
181
  css = '''
 
 
 
 
 
 
182
  .gradio-container {
183
  max-width: 800px !important;
184
+ margin: auto;
 
 
 
 
185
  }
186
+ h1 {
187
+ text-align: center;
188
+ font-size: 2.5em;
189
+ color: #4CAF50;
190
+ }
191
+ #duplicate-button {
192
+ display: block;
193
+ margin: 10px auto;
194
+ padding: 10px 20px;
195
+ background-color: #4CAF50;
196
+ color: white;
197
  border: none;
198
+ border-radius: 5px;
199
+ cursor: pointer;
200
+ }
201
+ #duplicate-button:hover {
202
+ background-color: #45a049;
203
+ }
204
+ .gradio-button {
205
+ display: block;
206
+ margin: 20px auto;
207
  padding: 10px 20px;
208
+ background-color: #2196F3;
209
  color: white;
210
+ border: none;
211
  border-radius: 5px;
212
  cursor: pointer;
 
213
  }
 
214
  .gradio-button:hover {
215
+ background-color: #0b7dda;
216
  }
217
+ .gradio-gallery {
218
+ width: 100%;
219
+ display: flex;
220
+ flex-wrap: wrap;
221
+ gap: 10px;
222
  }
223
+ .gradio-gallery img {
224
+ width: calc(50% - 10px);
225
+ height: auto;
226
+ border-radius: 5px;
 
227
  }
228
+ .gradio-accordion, .gradio-slider, .gradio-radio {
229
+ margin-bottom: 20px;
 
 
230
  }
231
+ .gradio-checkbox {
232
+ margin-top: 10px;
233
+ margin-bottom: 10px;
 
234
  }
235
+ .gradio-slider label, .gradio-checkbox label, .gradio-radio label {
236
+ color: #3E4C59;
 
 
 
237
  }
238
+ .gradio-examples {
239
+ display: flex;
240
+ flex-wrap: wrap;
241
+ justify-content: space-between;
242
+ }
243
+ .gradio-examples > div {
244
+ flex: 1;
245
+ margin: 5px;
246
+ padding: 10px;
247
+ background-color: #f9f9f9;
248
+ border: 1px solid #ddd;
249
+ border-radius: 5px;
250
+ cursor: pointer;
251
+ }
252
+ .gradio-examples > div:hover {
253
+ background-color: #f1f1f1;
254
  }
255
  '''
256
 
 
265
  placeholder="Enter your prompt",
266
  container=True
267
  )
268
+ run_button = gr.Button("Run")
269
+ result = gr.Gallery(label="Result", columns=2, preview=True)
270
+ with gr.Accordion("Advanced options", open=False):
271
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
272
  negative_prompt = gr.Textbox(
273
  label="Negative prompt",
274
  max_lines=1,
 
282
  minimum=10,
283
  maximum=60,
284
  step=1,
285
+ value=30
 
286
  )
287
  with gr.Row():
288
  num_images_per_prompt = gr.Slider(
 
290
  minimum=1,
291
  maximum=5,
292
  step=1,
293
+ value=2
 
294
  )
295
  seed = gr.Slider(
296
  label="Seed",
 
298
  maximum=MAX_SEED,
299
  step=1,
300
  value=0,
301
+ visible=True
 
302
  )
303
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
304
  with gr.Row(visible=True):
305
  width = gr.Slider(
306
  label="Width",
307
  minimum=512,
308
  maximum=2048,
309
  step=8,
310
+ value=1024
 
311
  )
312
  height = gr.Slider(
313
  label="Height",
314
  minimum=512,
315
  maximum=2048,
316
  step=8,
317
+ value=1024
 
318
  )
319
  with gr.Row():
320
  guidance_scale = gr.Slider(
 
322
  minimum=0.1,
323
  maximum=20.0,
324
  step=0.1,
325
+ value=6
 
326
  )
327
  with gr.Row(visible=True):
328
  style_selection = gr.Radio(
 
331
  interactive=True,
332
  choices=STYLE_NAMES,
333
  value=DEFAULT_STYLE_NAME,
334
+ label="Image Style"
 
335
  )
336
  gr.Examples(
337
  examples=examples,
338
  inputs=prompt,
339
  outputs=[result, seed],
340
  fn=generate,
341
+ cache_examples=CACHE_EXAMPLES
 
342
  )
343
 
344
  use_negative_prompt.change(
 
370
  api_name="run",
371
  )
372
  gr.Markdown(DESCRIPTION)
373
+
374
  if __name__ == "__main__":
375
  demo.queue(max_size=20).launch()