Spaces:
Paused
Paused
test gradio
Browse files- app.py +31 -46
- requirements.txt +2 -1
app.py
CHANGED
@@ -3,64 +3,49 @@ from huggingface_hub import login
|
|
3 |
import os
|
4 |
import spaces
|
5 |
import torch
|
6 |
-
from diffusers import
|
7 |
-
from
|
|
|
|
|
8 |
|
9 |
token = os.getenv("HF_TOKEN")
|
10 |
login(token=token)
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
#
|
21 |
-
# # Set up the scheduler and device
|
22 |
-
# pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
23 |
-
# pipe.to("cuda", torch.float16)
|
24 |
-
|
25 |
-
# controlnet = SD3ControlNetModel.from_pretrained("alimama-creative/SD3-Controlnet-Softedge", torch_dtype=torch.float16)
|
26 |
-
|
27 |
-
# pipe = StableDiffusion3ControlNetPipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", controlnet=controlnet)
|
28 |
-
# adapter = T2IAdapter.from_pretrained(
|
29 |
-
# "TencentARC/t2iadapter_color_sd14v1", torch_dtype=torch.float16, varient="fp16"
|
30 |
-
# )
|
31 |
-
#
|
32 |
-
# model_id = 'stabilityai/stable-diffusion-xl-base-1.0'
|
33 |
-
# euler_a = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
|
34 |
-
# vae=AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
35 |
-
# pipe = StableDiffusionXLAdapterPipeline.from_pretrained(
|
36 |
-
# model_id, vae=vae, adapter=adapter, scheduler=euler_a, torch_dtype=torch.float16, variant="fp16",
|
37 |
-
# )
|
38 |
-
|
39 |
-
|
40 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
41 |
-
"Yntec/AbsoluteReality",
|
42 |
-
torch_dtype=torch.float16)
|
43 |
-
pipe.load_ip_adapter("h94/IP-Adapter",
|
44 |
-
subfolder="models",
|
45 |
-
weight_name="ip-adapter_sd15.bin")
|
46 |
|
47 |
-
|
|
|
|
|
48 |
|
49 |
|
50 |
@spaces.GPU
|
51 |
def generate_image(prompt, reference_image, controlnet_conditioning_scale):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
pipe.set_ip_adapter_scale(controlnet_conditioning_scale)
|
55 |
-
generated_image = pipe(prompt=prompt,
|
56 |
-
negative_prompt="low quality",
|
57 |
-
height=768,
|
58 |
-
width=768,
|
59 |
-
ip_adapter_image=load_image(reference_image),
|
60 |
-
guidance_scale=6,
|
61 |
-
num_inference_steps=20,
|
62 |
-
num_images_per_prompt=3).images[0]
|
63 |
-
return generated_image
|
64 |
|
65 |
# Set up Gradio interface
|
66 |
interface = gr.Interface(
|
|
|
3 |
import os
|
4 |
import spaces
|
5 |
import torch
|
6 |
+
from diffusers import StableDiffusionXLPipeline
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
from ip_adapter import IPAdapterXL
|
10 |
|
11 |
token = os.getenv("HF_TOKEN")
|
12 |
login(token=token)
|
13 |
|
14 |
|
15 |
+
base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
|
16 |
+
image_encoder_path = "sdxl_models/image_encoder"
|
17 |
+
ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"
|
18 |
+
device = "cuda"
|
19 |
|
20 |
+
# load SDXL pipeline
|
21 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
22 |
+
base_model_path,
|
23 |
+
torch_dtype=torch.float16,
|
24 |
+
add_watermarker=False,
|
25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# reduce memory consumption
|
28 |
+
pipe.enable_vae_tiling()
|
29 |
+
ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["up_blocks.0.attentions.1"])
|
30 |
|
31 |
|
32 |
@spaces.GPU
|
33 |
def generate_image(prompt, reference_image, controlnet_conditioning_scale):
|
34 |
+
image = Image.open(reference_image)
|
35 |
+
image.resize((512, 512))
|
36 |
+
images = ip_model.generate(pil_image=image,
|
37 |
+
prompt=prompt,
|
38 |
+
negative_prompt="",
|
39 |
+
scale=controlnet_conditioning_scale,
|
40 |
+
guidance_scale=5,
|
41 |
+
num_samples=1,
|
42 |
+
num_inference_steps=30,
|
43 |
+
seed=42,
|
44 |
+
# neg_content_prompt="a rabbit",
|
45 |
+
# neg_content_scale=0.5,
|
46 |
+
)
|
47 |
|
48 |
+
return images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# Set up Gradio interface
|
51 |
interface = gr.Interface(
|
requirements.txt
CHANGED
@@ -5,4 +5,5 @@ accelerate
|
|
5 |
gradio
|
6 |
sentencepiece
|
7 |
spaces
|
8 |
-
pillow
|
|
|
|
5 |
gradio
|
6 |
sentencepiece
|
7 |
spaces
|
8 |
+
pillow
|
9 |
+
ip-adapter
|