Prgckwb commited on
Commit
90deeeb
1 Parent(s): eef5127

:art: Improve structure

Browse files
Files changed (3) hide show
  1. app.py +2 -119
  2. src/const.py +18 -0
  3. src/inference.py +119 -0
app.py CHANGED
@@ -1,125 +1,8 @@
1
  import gradio as gr
2
- import spaces
3
- import torch
4
- from PIL import Image
5
- from compel import Compel, DiffusersTextualInversionManager
6
- from diffusers import DiffusionPipeline
7
- from diffusers.utils import make_image_grid
8
 
 
9
  from src.example import EXAMPLES
10
-
11
- DIFFUSERS_MODEL_IDS = [
12
- # SD Models
13
- "stabilityai/stable-diffusion-3-medium-diffusers",
14
- "stabilityai/stable-diffusion-xl-base-1.0",
15
- "stabilityai/stable-diffusion-2-1",
16
- "runwayml/stable-diffusion-v1-5",
17
-
18
- # Other Models
19
- "Prgckwb/trpfrog-diffusion",
20
- ]
21
- EXTERNAL_MODEL_MAPPING = {
22
- "Beautiful Realistic Asians": "checkpoints/diffusers/Beautiful Realistic Asians v7",
23
- }
24
- MODEL_CHOICES = DIFFUSERS_MODEL_IDS + list(EXTERNAL_MODEL_MAPPING.keys())
25
-
26
- device = "cuda" if torch.cuda.is_available() else "cpu"
27
-
28
-
29
- def load_pipeline(model_id, use_model_offload, safety_checker):
30
- # Diffusers リポジトリ内のモデル
31
- if model_id in DIFFUSERS_MODEL_IDS:
32
- pipe = DiffusionPipeline.from_pretrained(
33
- model_id,
34
- torch_dtype=torch.float16,
35
- )
36
-
37
- # CIVITAI 系列由来のモデル
38
- else:
39
- pipe = DiffusionPipeline.from_pretrained(
40
- EXTERNAL_MODEL_MAPPING[model_id],
41
- torch_dtype=torch.float16,
42
- )
43
-
44
- # Load Textual Inversion
45
- pipe.load_textual_inversion("checkpoints/embeddings/BadNegAnatomyV1 neg.pt", token='BadNegAnatomyV1-neg')
46
- pipe.load_textual_inversion("checkpoints/embeddings/Deep Negative V1 75T.pt", token='DeepNegative')
47
- pipe.load_textual_inversion("checkpoints/embeddings/easynegative.safetensors", token='EasyNegative')
48
- pipe.load_textual_inversion("checkpoints/embeddings/Negative Hand Embedding.pt", token='negative_hand-neg')
49
-
50
- # Load LoRA
51
- pipe.load_lora_weights("checkpoints/lora/detailed style SD1.5.safetensors", adapter_name='detail')
52
- pipe.load_lora_weights("checkpoints/lora/perfection style SD1.5.safetensors", adapter_name='perfection')
53
- pipe.load_lora_weights("checkpoints/lora/Hand v3 SD1.5.safetensors", adapter_name='hands')
54
- pipe.set_adapters(['detail', 'hands'], adapter_weights=[0.5, 0.5])
55
-
56
- # VRAM が少ないとき用の対策
57
- if use_model_offload:
58
- pipe.enable_model_cpu_offload()
59
- else:
60
- pipe = pipe.to(device)
61
-
62
- if not safety_checker:
63
- pipe.safety_checker = None
64
-
65
- return pipe
66
-
67
-
68
- @spaces.GPU(duration=120)
69
- @torch.inference_mode()
70
- def inference(
71
- prompt: str,
72
- model_id: str = "stabilityai/stable-diffusion-3-medium-diffusers",
73
- negative_prompt: str = "",
74
- width: int = 512,
75
- height: int = 512,
76
- guidance_scale: float = 7.5,
77
- num_inference_steps: int = 50,
78
- num_images: int = 4,
79
- safety_checker: bool = True,
80
- use_model_offload: bool = False,
81
- seed: int = 8888,
82
- progress=gr.Progress(track_tqdm=True),
83
- ) -> Image.Image:
84
- progress(0, 'Loading pipeline...')
85
- pipe = load_pipeline(model_id, use_model_offload, safety_checker)
86
-
87
- # For Compel
88
- textual_inversion_manager = DiffusersTextualInversionManager(pipe)
89
- compel_procs = Compel(
90
- tokenizer=pipe.tokenizer,
91
- text_encoder=pipe.text_encoder,
92
- textual_inversion_manager=textual_inversion_manager,
93
- truncate_long_prompts=False,
94
- )
95
- prompt_embed = compel_procs(prompt)
96
- negative_prompt_embed = compel_procs(negative_prompt)
97
-
98
- prompt_embed, negative_prompt_embed = compel_procs.pad_conditioning_tensors_to_same_length(
99
- [prompt_embed, negative_prompt_embed]
100
- )
101
-
102
- generator = torch.Generator(device=device).manual_seed(seed)
103
-
104
- progress(0.3, 'Generating images...')
105
- images = pipe(
106
- prompt_embeds=prompt_embed,
107
- negative_prompt_embeds=negative_prompt_embed,
108
- width=width,
109
- height=height,
110
- guidance_scale=guidance_scale,
111
- num_inference_steps=num_inference_steps,
112
- num_images_per_prompt=num_images,
113
- generator=generator,
114
- ).images
115
-
116
- progress(0.9, f'Done generating {num_images} images')
117
- if num_images % 2 == 1:
118
- image = make_image_grid(images, rows=num_images, cols=1)
119
- else:
120
- image = make_image_grid(images, rows=2, cols=num_images // 2)
121
-
122
- return image
123
 
124
 
125
  def build_interface():
 
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
+ from src.const import MODEL_CHOICES
4
  from src.example import EXAMPLES
5
+ from src.inference import inference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
 
8
  def build_interface():
src/const.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ DIFFUSERS_MODEL_IDS = [
4
+ # SD Models
5
+ "stabilityai/stable-diffusion-3-medium-diffusers",
6
+ "stabilityai/stable-diffusion-xl-base-1.0",
7
+ "stabilityai/stable-diffusion-2-1",
8
+ "runwayml/stable-diffusion-v1-5",
9
+
10
+ # Other Models
11
+ "Prgckwb/trpfrog-diffusion",
12
+ ]
13
+ EXTERNAL_MODEL_MAPPING = {
14
+ "Beautiful Realistic Asians": "checkpoints/diffusers/Beautiful Realistic Asians v7",
15
+ }
16
+ MODEL_CHOICES = DIFFUSERS_MODEL_IDS + list(EXTERNAL_MODEL_MAPPING.keys())
17
+
18
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
src/inference.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import torch
4
+ from PIL import Image
5
+ from compel import Compel, DiffusersTextualInversionManager
6
+ from diffusers import DiffusionPipeline, StableDiffusionPipeline
7
+ from diffusers.utils import make_image_grid
8
+
9
+ from src.const import DIFFUSERS_MODEL_IDS, EXTERNAL_MODEL_MAPPING, DEVICE
10
+
11
+
12
+ def load_pipeline(model_id, use_model_offload, safety_checker):
13
+ # Diffusers リポジトリ内のモデル
14
+ if model_id in DIFFUSERS_MODEL_IDS:
15
+ pipe = DiffusionPipeline.from_pretrained(
16
+ model_id,
17
+ torch_dtype=torch.float16,
18
+ )
19
+
20
+ # CIVITAI 系列由来のモデル
21
+ else:
22
+ pipe = DiffusionPipeline.from_pretrained(
23
+ EXTERNAL_MODEL_MAPPING[model_id],
24
+ torch_dtype=torch.float16,
25
+ )
26
+
27
+ # Load Textual Inversion
28
+ pipe.load_textual_inversion("checkpoints/embeddings/BadNegAnatomyV1 neg.pt", token='BadNegAnatomyV1-neg')
29
+ pipe.load_textual_inversion("checkpoints/embeddings/Deep Negative V1 75T.pt", token='DeepNegative')
30
+ pipe.load_textual_inversion("checkpoints/embeddings/easynegative.safetensors", token='EasyNegative')
31
+ pipe.load_textual_inversion("checkpoints/embeddings/Negative Hand Embedding.pt", token='negative_hand-neg')
32
+
33
+ # Load LoRA
34
+ pipe.load_lora_weights("checkpoints/lora/detailed style SD1.5.safetensors", adapter_name='detail')
35
+ pipe.load_lora_weights("checkpoints/lora/perfection style SD1.5.safetensors", adapter_name='perfection')
36
+ pipe.load_lora_weights("checkpoints/lora/Hand v3 SD1.5.safetensors", adapter_name='hands')
37
+ pipe.set_adapters(['detail', 'hands'], adapter_weights=[0.5, 0.5])
38
+
39
+ # VRAM が少ないとき用の対策
40
+ if use_model_offload:
41
+ pipe.enable_model_cpu_offload()
42
+ else:
43
+ pipe = pipe.to(DEVICE)
44
+
45
+ if not safety_checker:
46
+ pipe.safety_checker = None
47
+
48
+ return pipe
49
+
50
+
51
+ @spaces.GPU(duration=120)
52
+ @torch.inference_mode()
53
+ def inference(
54
+ prompt: str,
55
+ model_id: str = "stabilityai/stable-diffusion-3-medium-diffusers",
56
+ negative_prompt: str = "",
57
+ width: int = 512,
58
+ height: int = 512,
59
+ guidance_scale: float = 7.5,
60
+ num_inference_steps: int = 50,
61
+ num_images: int = 4,
62
+ safety_checker: bool = True,
63
+ use_model_offload: bool = False,
64
+ seed: int = 8888,
65
+ progress=gr.Progress(track_tqdm=True),
66
+ ) -> Image.Image:
67
+ progress(0, 'Loading pipeline...')
68
+ pipe = load_pipeline(model_id, use_model_offload, safety_checker)
69
+
70
+ # Seed 固定
71
+ generator = torch.Generator(device=DEVICE).manual_seed(seed)
72
+
73
+ if isinstance(pipe, StableDiffusionPipeline):
74
+ # For Compel
75
+ textual_inversion_manager = DiffusersTextualInversionManager(pipe)
76
+ compel_procs = Compel(
77
+ tokenizer=pipe.tokenizer,
78
+ text_encoder=pipe.text_encoder,
79
+ textual_inversion_manager=textual_inversion_manager,
80
+ truncate_long_prompts=False,
81
+ )
82
+ prompt_embed = compel_procs(prompt)
83
+ negative_prompt_embed = compel_procs(negative_prompt)
84
+
85
+ prompt_embed, negative_prompt_embed = compel_procs.pad_conditioning_tensors_to_same_length(
86
+ [prompt_embed, negative_prompt_embed]
87
+ )
88
+
89
+ progress(0.3, 'Generating images...')
90
+ images = pipe(
91
+ prompt_embeds=prompt_embed,
92
+ negative_prompt_embeds=negative_prompt_embed,
93
+ width=width,
94
+ height=height,
95
+ guidance_scale=guidance_scale,
96
+ num_inference_steps=num_inference_steps,
97
+ num_images_per_prompt=num_images,
98
+ generator=generator,
99
+ ).images
100
+ else:
101
+ progress(0.3, 'Generating images...')
102
+ images = pipe(
103
+ prompt=prompt,
104
+ negative_prompt=negative_prompt,
105
+ width=width,
106
+ height=height,
107
+ guidance_scale=guidance_scale,
108
+ num_inference_steps=num_inference_steps,
109
+ num_images=num_images,
110
+ generator=generator,
111
+ ).images
112
+
113
+ progress(0.9, f'Done generating {num_images} images')
114
+ if num_images % 2 == 1:
115
+ image = make_image_grid(images, rows=num_images, cols=1)
116
+ else:
117
+ image = make_image_grid(images, rows=2, cols=num_images // 2)
118
+
119
+ return image