codeignite commited on
Commit
aafc75e
·
verified ·
1 Parent(s): 64f9388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -1,25 +1,24 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- import spaces # <--- UNCOMMENTED
5
  from diffusers import DiffusionPipeline
6
  import torch
7
 
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
  model_repo_id = "stabilityai/sdxl-turbo"
 
 
 
 
10
 
11
- if torch.cuda.is_available():
12
- torch_dtype = torch.float16
13
- else:
14
- torch_dtype = torch.float32
15
 
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
- pipe = pipe.to(device)
18
- pipe.enable_xformers_memory_efficient_attention() # <--- ADD THIS LINE
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
22
- @spaces.GPU # <--- UNCOMMENTED: This gives you the H200 GPU power
23
  def infer(
24
  prompt,
25
  negative_prompt,
@@ -33,7 +32,15 @@ def infer(
33
  ):
34
  if randomize_seed:
35
  seed = random.randint(0, MAX_SEED)
36
- generator = torch.Generator().manual_seed(seed)
 
 
 
 
 
 
 
 
37
 
38
  image = pipe(
39
  prompt=prompt,
@@ -44,6 +51,7 @@ def infer(
44
  height=height,
45
  generator=generator,
46
  ).images[0]
 
47
  return image, seed
48
 
49
  # UI Styling
@@ -51,39 +59,32 @@ css = """#col-container { margin: 0 auto; max-width: 640px; }"""
51
 
52
  with gr.Blocks(css=css) as demo:
53
  with gr.Column(elem_id="col-container"):
54
- gr.Markdown(" # CodeIgnite Image Engine (SDXL-Turbo)")
55
 
56
  with gr.Row():
57
- prompt = gr.Text(
58
- label="Prompt",
59
- show_label=False,
60
- max_lines=1,
61
- placeholder="Enter your prompt",
62
- container=False,
63
- )
64
- run_button = gr.Button("Run", scale=0, variant="primary")
65
 
66
  result = gr.Image(label="Result", show_label=False)
67
 
68
  with gr.Accordion("Advanced Settings", open=False):
69
- negative_prompt = gr.Text(label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", visible=True)
70
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
71
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
72
 
73
  with gr.Row():
74
- width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512) # Lowered for speed
75
  height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
76
 
77
  with gr.Row():
78
  guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0)
79
- num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=4, step=1, value=2)
80
 
81
- # This 'api_name' is CRITICAL for your WhatsApp Bot to find the function
82
  run_button.click(
83
  fn=infer,
84
  inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
85
  outputs=[result, seed],
86
- api_name="predict"
87
  )
88
 
89
  if __name__ == "__main__":
 
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
+ # 1. Load the model on CPU first (Safe for startup)
9
  model_repo_id = "stabilityai/sdxl-turbo"
10
+ pipe = DiffusionPipeline.from_pretrained(
11
+ model_repo_id,
12
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
13
+ )
14
 
15
+ # NOTE: We do NOT move to cuda or enable xformers here.
16
+ # That would cause the crash you saw.
 
 
17
 
 
 
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 1024
20
 
21
+ @spaces.GPU(duration=60) # Magic starts here
22
  def infer(
23
  prompt,
24
  negative_prompt,
 
32
  ):
33
  if randomize_seed:
34
  seed = random.randint(0, MAX_SEED)
35
+
36
+ # 2. Move to GPU and enable speed boosts ONLY when the function runs
37
+ pipe.to("cuda")
38
+ try:
39
+ pipe.enable_xformers_memory_efficient_attention()
40
+ except Exception:
41
+ pass # Fallback if xformers isn't needed
42
+
43
+ generator = torch.Generator("cuda").manual_seed(seed)
44
 
45
  image = pipe(
46
  prompt=prompt,
 
51
  height=height,
52
  generator=generator,
53
  ).images[0]
54
+
55
  return image, seed
56
 
57
  # UI Styling
 
59
 
60
  with gr.Blocks(css=css) as demo:
61
  with gr.Column(elem_id="col-container"):
62
+ gr.Markdown(" # CodeIgnite Image Engine")
63
 
64
  with gr.Row():
65
+ prompt = gr.Text(label="Prompt", show_label=False, placeholder="Enter your prompt", container=False)
66
+ run_button = gr.Button("Run", variant="primary")
 
 
 
 
 
 
67
 
68
  result = gr.Image(label="Result", show_label=False)
69
 
70
  with gr.Accordion("Advanced Settings", open=False):
71
+ negative_prompt = gr.Text(label="Negative prompt", placeholder="Optional")
72
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
73
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
74
 
75
  with gr.Row():
76
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
77
  height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
78
 
79
  with gr.Row():
80
  guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0)
81
+ num_inference_steps = gr.Slider(label="Steps", minimum=1, maximum=4, step=1, value=2)
82
 
 
83
  run_button.click(
84
  fn=infer,
85
  inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
86
  outputs=[result, seed],
87
+ api_name="predict"
88
  )
89
 
90
  if __name__ == "__main__":