fmasterpro27 commited on
Commit
09a82fa
·
verified ·
1 Parent(s): 7cef76b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -3,18 +3,24 @@ import numpy as np
3
  import random
4
 
5
  # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
  import torch
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "runwayml/stable-diffusion-v1-5" # Replace to the model you would like to use
11
 
12
  if torch.cuda.is_available():
13
  torch_dtype = torch.float16
14
  else:
15
  torch_dtype = torch.float32
16
 
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
 
 
 
 
 
 
18
  pipe = pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
@@ -36,13 +42,16 @@ def infer(
36
  if randomize_seed:
37
  seed = random.randint(0, MAX_SEED)
38
 
39
- generator = torch.Generator().manual_seed(seed)
40
 
 
 
 
41
  image = pipe(
42
  prompt=prompt,
43
  negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
  width=width,
47
  height=height,
48
  generator=generator,
@@ -55,7 +64,7 @@ examples = [
55
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
  "An astronaut riding a green horse",
57
  "A delicious ceviche cheesecake slice",
58
- "Wolf ki bund",
59
  ]
60
 
61
  css = """
@@ -270,7 +279,7 @@ css = """
270
  border: 1px solid var(--border-color) !important;
271
  border-radius: 10px;
272
  padding: 20px;
273
- margin: 20px 0;
274
  }
275
 
276
  .gradio-container .gradio-examples .label {
@@ -346,6 +355,7 @@ css = """
346
  with gr.Blocks(css=css, title="AI Image Generator", theme=gr.themes.Base()) as demo:
347
  with gr.Column(elem_id="col-container"):
348
  gr.Markdown("# AI Image Generator")
 
349
 
350
  with gr.Row():
351
  prompt = gr.Text(
@@ -385,7 +395,7 @@ with gr.Blocks(css=css, title="AI Image Generator", theme=gr.themes.Base()) as d
385
  minimum=256,
386
  maximum=MAX_IMAGE_SIZE,
387
  step=32,
388
- value=1024,
389
  )
390
 
391
  height = gr.Slider(
@@ -393,24 +403,25 @@ with gr.Blocks(css=css, title="AI Image Generator", theme=gr.themes.Base()) as d
393
  minimum=256,
394
  maximum=MAX_IMAGE_SIZE,
395
  step=32,
396
- value=1024,
397
  )
398
 
399
  with gr.Row():
400
  guidance_scale = gr.Slider(
401
- label="Guidance Scale",
402
  minimum=0.0,
403
  maximum=10.0,
404
  step=0.1,
405
- value=7.5,
 
406
  )
407
 
408
  num_inference_steps = gr.Slider(
409
- label="Inference Steps",
410
  minimum=1,
411
- maximum=50,
412
  step=1,
413
- value=20,
414
  )
415
 
416
  gr.Examples(
 
3
  import random
4
 
5
  # import spaces #[uncomment to use ZeroGPU]
6
+ from diffusers import DiffusionPipeline, AutoPipelineForText2Image
7
  import torch
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ model_repo_id = "stabilityai/sdxl-turbo"
11
 
12
  if torch.cuda.is_available():
13
  torch_dtype = torch.float16
14
  else:
15
  torch_dtype = torch.float32
16
 
17
+ # Load pipeline with safety checker disabled
18
+ pipe = AutoPipelineForText2Image.from_pretrained(
19
+ model_repo_id,
20
+ torch_dtype=torch_dtype,
21
+ safety_checker=None,
22
+ requires_safety_checker=False
23
+ )
24
  pipe = pipe.to(device)
25
 
26
  MAX_SEED = np.iinfo(np.int32).max
 
42
  if randomize_seed:
43
  seed = random.randint(0, MAX_SEED)
44
 
45
+ generator = torch.Generator(device=device).manual_seed(seed)
46
 
47
+ # SDXL-Turbo works best with these settings:
48
+ # - guidance_scale should be 0.0 (it's trained without guidance)
49
+ # - num_inference_steps should be 1-4 for best results
50
  image = pipe(
51
  prompt=prompt,
52
  negative_prompt=negative_prompt,
53
+ guidance_scale=0.0, # SDXL-Turbo requires 0.0
54
+ num_inference_steps=max(1, min(4, num_inference_steps)), # Clamp to 1-4
55
  width=width,
56
  height=height,
57
  generator=generator,
 
64
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
65
  "An astronaut riding a green horse",
66
  "A delicious ceviche cheesecake slice",
67
+ "Beautiful landscape with mountains and lake at sunset",
68
  ]
69
 
70
  css = """
 
279
  border: 1px solid var(--border-color) !important;
280
  border-radius: 10px;
281
  padding: 20px;
282
+ margin: 20px;
283
  }
284
 
285
  .gradio-container .gradio-examples .label {
 
355
  with gr.Blocks(css=css, title="AI Image Generator", theme=gr.themes.Base()) as demo:
356
  with gr.Column(elem_id="col-container"):
357
  gr.Markdown("# AI Image Generator")
358
+ gr.Markdown("*Powered by SDXL-Turbo - Optimized for fast generation (1-4 steps)*")
359
 
360
  with gr.Row():
361
  prompt = gr.Text(
 
395
  minimum=256,
396
  maximum=MAX_IMAGE_SIZE,
397
  step=32,
398
+ value=512,
399
  )
400
 
401
  height = gr.Slider(
 
403
  minimum=256,
404
  maximum=MAX_IMAGE_SIZE,
405
  step=32,
406
+ value=512,
407
  )
408
 
409
  with gr.Row():
410
  guidance_scale = gr.Slider(
411
+ label="Guidance Scale (Ignored - SDXL-Turbo uses 0.0)",
412
  minimum=0.0,
413
  maximum=10.0,
414
  step=0.1,
415
+ value=0.0,
416
+ interactive=False,
417
  )
418
 
419
  num_inference_steps = gr.Slider(
420
+ label="Inference Steps (1-4 recommended)",
421
  minimum=1,
422
+ maximum=10,
423
  step=1,
424
+ value=2,
425
  )
426
 
427
  gr.Examples(