Profakerr commited on
Commit
b7a803e
·
verified ·
1 Parent(s): 8d3fced

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -9
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler, AutoencoderKL
 
2
  import torch
3
  import gradio as gr
4
  import spaces
@@ -18,9 +19,23 @@ def generate_image(prompt, negative_prompt, num_inference_steps=30, guidance_sca
18
 
19
  else:
20
  model_id = "SG161222/Realistic_Vision_V6.0_B1_noVAE"
 
 
 
 
 
 
 
 
 
 
21
 
22
-
23
- pipe = DiffusionPipeline.from_pretrained(model_id, vae=vae).to("cuda")
 
 
 
 
24
 
25
  if model == "Real6.0":
26
  pipe.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
@@ -33,16 +48,35 @@ def generate_image(prompt, negative_prompt, num_inference_steps=30, guidance_sca
33
  use_karras_sigmas=True
34
  )
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Generate the image
38
  result = pipe(
39
- prompt = prompt,
40
- negative_prompt = negative_prompt,
41
- cross_attention_kwargs = {"scale":1},
42
- num_inference_steps = num_inference_steps,
43
- guidance_scale = guidance_scale,
44
- width = width,
45
- height = height,
46
  num_images_per_prompt=num_images
47
  )
48
 
 
1
  from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler, AutoencoderKL
2
+ from transformers import CLIPTextModel, CLIPTokenizer
3
  import torch
4
  import gradio as gr
5
  import spaces
 
19
 
20
  else:
21
  model_id = "SG161222/Realistic_Vision_V6.0_B1_noVAE"
22
+
23
+ text_encoder = CLIPTextModel.from_pretrained(
24
+ model_id,
25
+ subfolder="text_encoder"
26
+ ).to("cuda")
27
+
28
+ tokenizer = CLIPTokenizer.from_pretrained(
29
+ model_id,
30
+ subfolder="tokenizer"
31
+ )
32
 
33
+ pipe = DiffusionPipeline.from_pretrained(
34
+ model_id,
35
+ text_encoder=text_encoder,
36
+ tokenizer=tokenizer,
37
+ vae=vae
38
+ ).to("cuda")
39
 
40
  if model == "Real6.0":
41
  pipe.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
 
48
  use_karras_sigmas=True
49
  )
50
 
51
+ text_inputs = tokenizer(
52
+ prompt,
53
+ padding="max_length",
54
+ max_length=tokenizer.model_max_length,
55
+ truncation=True,
56
+ return_tensors="pt"
57
+ ).to("cuda")
58
+
59
+ negative_text_inputs = tokenizer(
60
+ negative_prompt,
61
+ padding="max_length",
62
+ max_length=tokenizer.model_max_length,
63
+ truncation=True,
64
+ return_tensors="pt"
65
+ ).to("cuda")
66
+
67
+ prompt_embeds = text_encoder(text_inputs.input_ids)[0]
68
+ negative_prompt_embeds = text_encoder(negative_text_inputs.input_ids)[0]
69
+
70
 
71
  # Generate the image
72
  result = pipe(
73
+ prompt_embeds=prompt_embeds,
74
+ negative_prompt_embeds=negative_prompt_embeds,
75
+ cross_attention_kwargs={"scale": 1},
76
+ num_inference_steps=num_inference_steps,
77
+ guidance_scale=guidance_scale,
78
+ width=width,
79
+ height=height,
80
  num_images_per_prompt=num_images
81
  )
82