prithivMLmods commited on
Commit
3b1b420
·
verified ·
1 Parent(s): 2381eb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -55
app.py CHANGED
@@ -1,23 +1,21 @@
1
  import gradio as gr
2
- import spaces
3
  import numpy as np
4
  import random
 
 
5
  from diffusers import DiffusionPipeline
6
  import torch
7
  from PIL import Image
8
 
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
11
 
12
  torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
13
-
14
  pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
15
  pipe = pipe.to(device)
16
 
17
- pipe.load_lora_weights("strangerzonehf/SD3.5-Turbo-Portrait-LoRA", weight_name="SD3.5-Turbo-Portrait.safetensors")
18
- trigger_word = "Turbo Portrait"
19
- pipe.fuse_lora(lora_scale=1.0)
20
-
21
  MAX_SEED = np.iinfo(np.int32).max
22
  MAX_IMAGE_SIZE = 1024
23
 
@@ -44,65 +42,66 @@ style_list = [
44
  "negative_prompt": "",
45
  },
46
  ]
47
-
48
  STYLE_NAMES = [s["name"] for s in style_list]
49
  DEFAULT_STYLE_NAME = STYLE_NAMES[0]
50
 
 
51
  grid_sizes = {
52
  "2x1": (2, 1),
53
  "1x2": (1, 2),
54
  "2x2": (2, 2),
55
  "2x3": (2, 3),
56
  "3x2": (3, 2),
57
- "1x1": (1, 1)
58
  }
59
-
60
- @spaces.GPU(duration=60)
61
- def infer(
62
- prompt,
63
- negative_prompt="",
64
- seed=42,
65
- randomize_seed=False,
66
- width=1024,
67
- height=1024,
68
- guidance_scale=7.5,
69
- num_inference_steps=10,
70
- style="Style Zero",
71
- grid_size="1x1",
72
- progress=gr.Progress(track_tqdm=True),
73
- ):
74
- selected_style = next(s for s in style_list if s["name"] == style)
75
- styled_prompt = selected_style["prompt"].format(prompt=prompt)
76
- styled_negative_prompt = selected_style["negative_prompt"]
77
-
78
- if randomize_seed:
79
- seed = random.randint(0, MAX_SEED)
80
-
81
- generator = torch.Generator().manual_seed(seed)
82
- grid_x, grid_y = grid_sizes.get(grid_size, (1, 1))
83
- num_images = grid_x * grid_y
84
-
85
- opts = {
86
- "prompt": styled_prompt,
87
- "negative_prompt": styled_negative_prompt,
88
- "guidance_scale": guidance_scale,
89
- "num_inference_steps": num_inference_steps,
90
- "width": width,
91
- "height": height,
92
- "generator": generator,
93
- "num_images_per_prompt": num_images,
94
- }
95
-
96
- torch.cuda.empty_cache()
97
- res = pipe(**opts)
98
-
99
- grid_img = Image.new('RGB', (width * grid_x, height * grid_y))
100
- for i, img in enumerate(res.images[:num_images]):
101
- x = (i % grid_x) * width
102
- y = (i // grid_x) * height
103
- grid_img.paste(img, (x, y))
104
-
105
- return grid_img, seed
 
106
 
107
  examples = [
108
  "A tiny astronaut hatching from an egg on the moon, 4k, planet theme",
 
1
  import gradio as gr
 
2
  import numpy as np
3
  import random
4
+
5
+ import spaces
6
  from diffusers import DiffusionPipeline
7
  import torch
8
  from PIL import Image
9
 
10
+ # Device and model setup
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
13
 
14
  torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
 
15
  pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
16
  pipe = pipe.to(device)
17
 
18
+ # Constants
 
 
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
 
42
  "negative_prompt": "",
43
  },
44
  ]
 
45
  STYLE_NAMES = [s["name"] for s in style_list]
46
  DEFAULT_STYLE_NAME = STYLE_NAMES[0]
47
 
48
+ # Define grid layouts
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
+ def infer(
59
+ prompt,
60
+ negative_prompt="",
61
+ seed=42,
62
+ randomize_seed=False,
63
+ width=1024,
64
+ height=1024,
65
+ guidance_scale=7.5,
66
+ num_inference_steps=10,
67
+ style="Style Zero",
68
+ grid_size="1x1",
69
+ progress=gr.Progress(track_tqdm=True),
70
+ ):
71
+ # Apply seed
72
+ if randomize_seed:
73
+ seed = random.randint(0, MAX_SEED)
74
+ generator = torch.Generator().manual_seed(seed)
75
+
76
+ # Style formatting
77
+ selected_style = next(s for s in style_list if s["name"] == style)
78
+ styled_prompt = selected_style["prompt"].format(prompt=prompt)
79
+ styled_negative = selected_style["negative_prompt"] or negative_prompt
80
+
81
+ # Grid calculation
82
+ grid_x, grid_y = grid_sizes.get(grid_size, (1, 1))
83
+ num_images = grid_x * grid_y
84
+
85
+ # Inference
86
+ output = pipe(
87
+ prompt=styled_prompt,
88
+ negative_prompt=styled_negative,
89
+ width=width,
90
+ height=height,
91
+ guidance_scale=guidance_scale,
92
+ num_inference_steps=num_inference_steps,
93
+ generator=generator,
94
+ num_images_per_prompt=num_images,
95
+ )
96
+
97
+ # Combine into grid
98
+ grid_img = Image.new('RGB', (width * grid_x, height * grid_y))
99
+ for i, img in enumerate(output.images[:num_images]):
100
+ x = (i % grid_x) * width
101
+ y = (i // grid_x) * height
102
+ grid_img.paste(img, (x, y))
103
+
104
+ return grid_img, seed
105
 
106
  examples = [
107
  "A tiny astronaut hatching from an egg on the moon, 4k, planet theme",