Prgckwb commited on
Commit
4fe0601
1 Parent(s): c3d14af

:tada: add negative embedding

Browse files
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import dataclasses
2
 
3
  import gradio as gr
4
- import requests
5
  import spaces
6
  import torch
7
  from PIL import Image
@@ -25,15 +24,17 @@ EXTERNAL_MODEL_MAPPING = {
25
 
26
  MODEL_CHOICES = DIFFUSERS_MODEL_IDS + list(EXTERNAL_MODEL_MAPPING.keys())
27
 
28
- # Global Variables
29
  current_model_id = "stabilityai/stable-diffusion-3-medium-diffusers"
30
- device = "cuda" if torch.cuda.is_available() else "cpu"
31
 
 
32
  if device == 'cuda':
 
33
  pipe = DiffusionPipeline.from_pretrained(
34
  current_model_id,
35
- torch_dtype=torch.float16,
36
  ).to(device)
 
 
37
 
38
 
39
  @dataclasses.dataclass
@@ -61,6 +62,15 @@ EXAMPLES = [
61
  prompt='Beautiful pixel art of a Wizard with hovering text "Achivement unlocked: Diffusion models can spell now"'
62
  ).to_list(),
63
  Input(prompt='A corgi wearing sunglasses says "U-Net is OVER!!"').to_list(),
 
 
 
 
 
 
 
 
 
64
  ]
65
 
66
 
@@ -79,24 +89,28 @@ def inference(
79
  ) -> Image.Image:
80
  progress(0, "Starting inference...")
81
 
82
- if device != 'cuda':
83
- raise gr.Error("This model requires a GPU to run. Please switch to a GPU runtime.")
84
-
85
  global current_model_id, pipe
86
 
87
  if model_id != current_model_id:
88
  try:
 
89
  if model_id not in DIFFUSERS_MODEL_IDS:
90
  model_id = EXTERNAL_MODEL_MAPPING[model_id]
91
 
92
  pipe = DiffusionPipeline.from_pretrained(
93
  model_id,
94
- torch_dtype=torch.float16,
95
  ).to(device)
 
96
  current_model_id = model_id
97
  except Exception as e:
98
  raise gr.Error(str(e))
99
 
 
 
 
 
 
100
  # Generation
101
  images = pipe(
102
  prompt,
@@ -141,8 +155,9 @@ if __name__ == "__main__":
141
  num_images = gr.Number(label="Num Images", value=4, minimum=1, maximum=10, step=1)
142
 
143
  guidance_scale = gr.Slider(label="Guidance Scale", value=7.5, step=0.5, minimum=0, maximum=10)
144
- num_inference_step = gr.Slider(label="Num Inference Steps", value=50, minimum=1, maximum=100,
145
- step=1)
 
146
 
147
  with gr.Column():
148
  output_image = gr.Image(label="Image", type="pil")
 
1
  import dataclasses
2
 
3
  import gradio as gr
 
4
  import spaces
5
  import torch
6
  from PIL import Image
 
24
 
25
  MODEL_CHOICES = DIFFUSERS_MODEL_IDS + list(EXTERNAL_MODEL_MAPPING.keys())
26
 
 
27
  current_model_id = "stabilityai/stable-diffusion-3-medium-diffusers"
 
28
 
29
+ device = "cuda" if torch.cuda.is_available() else "cpu"
30
  if device == 'cuda':
31
+ dtype = torch.float16
32
  pipe = DiffusionPipeline.from_pretrained(
33
  current_model_id,
34
+ torch_dtype=dtype,
35
  ).to(device)
36
+ else:
37
+ dtype = torch.float32
38
 
39
 
40
  @dataclasses.dataclass
 
62
  prompt='Beautiful pixel art of a Wizard with hovering text "Achivement unlocked: Diffusion models can spell now"'
63
  ).to_list(),
64
  Input(prompt='A corgi wearing sunglasses says "U-Net is OVER!!"').to_list(),
65
+ Input(
66
+ prompt='Cinematic Photo of a beautiful korean fashion model bokeh train',
67
+ model_id='Beautiful Realistic Asians',
68
+ negative_prompt='(worst_quality:2.0) (MajicNegative_V2:0.8) BadNegAnatomyV1-neg bradhands cartoon, cgi, render, illustration, painting, drawing',
69
+ width=512,
70
+ height=512,
71
+ guidance_scale=5.0,
72
+ num_inference_step=50,
73
+ ).to_list()
74
  ]
75
 
76
 
 
89
  ) -> Image.Image:
90
  progress(0, "Starting inference...")
91
 
 
 
 
92
  global current_model_id, pipe
93
 
94
  if model_id != current_model_id:
95
  try:
96
+ # For NOT Diffusers' Models
97
  if model_id not in DIFFUSERS_MODEL_IDS:
98
  model_id = EXTERNAL_MODEL_MAPPING[model_id]
99
 
100
  pipe = DiffusionPipeline.from_pretrained(
101
  model_id,
102
+ torch_dtype=dtype,
103
  ).to(device)
104
+
105
  current_model_id = model_id
106
  except Exception as e:
107
  raise gr.Error(str(e))
108
 
109
+ # Load Textual Inversion
110
+ pipe.load_textual_inversion(
111
+ "checkpoints/embeddings/BadNegAnatomyV1 neg.pt", token='BadNegAnatomyV1-neg'
112
+ )
113
+
114
  # Generation
115
  images = pipe(
116
  prompt,
 
155
  num_images = gr.Number(label="Num Images", value=4, minimum=1, maximum=10, step=1)
156
 
157
  guidance_scale = gr.Slider(label="Guidance Scale", value=7.5, step=0.5, minimum=0, maximum=10)
158
+ num_inference_step = gr.Slider(
159
+ label="Num Inference Steps", value=50, minimum=1, maximum=100, step=2
160
+ )
161
 
162
  with gr.Column():
163
  output_image = gr.Image(label="Image", type="pil")
checkpoints/embeddings/BadNegAnatomyV1 neg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b389c1e5f96e23612272607e980d4ddab62183b7bfecc631293ebd700ce8f96a
3
+ size 215915