amos1088 commited on
Commit
97c3973
·
1 Parent(s): 2ac8e8a

test gradio

Browse files
Files changed (2) hide show
  1. app.py +32 -29
  2. requirements.txt +1 -3
app.py CHANGED
@@ -5,46 +5,49 @@ import spaces
5
  import torch
6
  from diffusers import StableDiffusionXLPipeline
7
  from PIL import Image
8
- from ip_adapter import IPAdapterXL
 
 
 
9
 
10
  token = os.getenv("HF_TOKEN")
11
  login(token=token)
12
 
13
-
14
- base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
15
- image_encoder_path = "sdxl_models/image_encoder"
16
- ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"
17
- device = "cuda"
18
-
19
- # load SDXL pipeline
20
- pipe = StableDiffusionXLPipeline.from_pretrained(
21
- base_model_path,
22
  torch_dtype=torch.float16,
23
- add_watermarker=False,
24
  )
25
 
26
- # reduce memory consumption
27
- pipe.enable_vae_tiling()
28
- ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device,token=token)
 
 
 
 
 
 
 
 
 
 
29
 
30
 
31
  @spaces.GPU
32
  def generate_image(prompt, reference_image, controlnet_conditioning_scale):
33
- image = Image.open(reference_image)
34
- image.resize((512, 512))
35
- images = ip_model.generate(pil_image=image,
36
- prompt=prompt,
37
- negative_prompt="",
38
- scale=controlnet_conditioning_scale,
39
- guidance_scale=5,
40
- num_samples=1,
41
- num_inference_steps=30,
42
- seed=42,
43
- # neg_content_prompt="a rabbit",
44
- # neg_content_scale=0.5,
45
- )
46
-
47
- return images[0]
48
 
49
  # Set up Gradio interface
50
  interface = gr.Interface(
 
5
  import torch
6
  from diffusers import StableDiffusionXLPipeline
7
  from PIL import Image
8
+ import torch
9
+ from diffusers import AutoPipelineForText2Image, DDIMScheduler
10
+ from transformers import CLIPVisionModelWithProjection
11
+ from diffusers.utils import load_image
12
 
13
  token = os.getenv("HF_TOKEN")
14
  login(token=token)
15
 
16
+ image_encoder = CLIPVisionModelWithProjection.from_pretrained(
17
+ "h94/IP-Adapter",
18
+ subfolder="models/image_encoder",
 
 
 
 
 
 
19
  torch_dtype=torch.float16,
 
20
  )
21
 
22
+ pipeline = AutoPipelineForText2Image.from_pretrained(
23
+ "stabilityai/stable-diffusion-xl-base-1.0",
24
+ torch_dtype=torch.float16,
25
+ image_encoder=image_encoder,
26
+ )
27
+ pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
28
+ pipeline.load_ip_adapter(
29
+ "h94/IP-Adapter",
30
+ subfolder="sdxl_models",
31
+ weight_name=["ip-adapter-plus_sdxl_vit-h.safetensors", "ip-adapter-plus-face_sdxl_vit-h.safetensors"]
32
+ )
33
+ pipeline.set_ip_adapter_scale([0.7, 0.3])
34
+ pipeline.enable_model_cpu_offload()
35
 
36
 
37
  @spaces.GPU
38
  def generate_image(prompt, reference_image, controlnet_conditioning_scale):
39
+ reference_image = Image.open(reference_image)
40
+ # reference_image.resize((512, 512))
41
+ pipeline.set_ip_adapter_scale([controlnet_conditioning_scale])
42
+
43
+ image = pipeline(
44
+ prompt=prompt,
45
+ ip_adapter_image=[reference_image],
46
+ negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
47
+ num_inference_steps=50, num_images_per_prompt=1,
48
+ ).images[0]
49
+
50
+ return image
 
 
 
51
 
52
  # Set up Gradio interface
53
  interface = gr.Interface(
requirements.txt CHANGED
@@ -5,6 +5,4 @@ accelerate
5
  gradio
6
  sentencepiece
7
  spaces
8
- pillow
9
- ip-adapter
10
- einops
 
5
  gradio
6
  sentencepiece
7
  spaces
8
+ pillow