AP123 commited on
Commit
9191b3d
·
verified ·
1 Parent(s): b2ef9b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -45
app.py CHANGED
@@ -2,75 +2,55 @@ import gradio as gr
2
  import torch
3
  from PIL import Image
4
  from diffusers import AutoPipelineForText2Image, DDIMScheduler
 
5
  import numpy as np
6
- from torchvision import transforms
7
- import spaces
 
 
 
 
 
 
8
 
9
- # Initialize the pipeline
10
  pipeline = AutoPipelineForText2Image.from_pretrained(
11
  "stabilityai/stable-diffusion-xl-base-1.0",
12
- torch_dtype=torch.float16
 
13
  )
14
-
15
- # Configure the scheduler for the pipeline
16
  pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
17
 
18
- # Load IP adapter with specified weights and set the scale for each component
19
- pipeline.load_ip_adapter(
20
- "h94/IP-Adapter",
21
- subfolder="sdxl_models",
22
- weight_name=[
23
- "ip-adapter-plus_sdxl_vit-h.safetensors",
24
- "ip-adapter-plus-face_sdxl_vit-h.safetensors"
25
- ]
26
- )
27
  pipeline.set_ip_adapter_scale([0.7, 0.5])
28
 
29
- # Define the desired size for the images
30
- desired_size = (1024, 1024)
31
 
32
- @spaces.GPU
33
  def transform_image(face_image):
34
- # Move the pipeline to the GPU inside the function
35
- pipeline.to("cuda")
36
- generator = torch.Generator(device="cuda").manual_seed(0)
37
 
38
- # Process the input face image
39
  if isinstance(face_image, Image.Image):
40
  processed_face_image = face_image
 
41
  elif isinstance(face_image, np.ndarray):
42
  processed_face_image = Image.fromarray(face_image)
43
  else:
44
  raise ValueError("Unsupported image format")
45
-
46
- # Ensure the processed face image is in RGB format
47
- processed_face_image = processed_face_image.convert('RGB')
48
-
49
- # Resize the face image to 1024x1024
50
- processed_face_image = processed_face_image.resize(desired_size, Image.LANCZOS)
51
 
52
- # Load the style image from the local path, resize it to 1024x1024, and convert to tensor
53
- style_image_path = "examples/soyjak2.jpg" # Ensure this path is correct
54
- style_image = Image.open(style_image_path).resize(desired_size, Image.LANCZOS).convert('RGB')
55
- style_image_tensor = transforms.ToTensor()(style_image).unsqueeze(0).to("cuda")
56
-
57
- # Convert the processed face image to tensor and move to GPU
58
- processed_face_image_tensor = transforms.ToTensor()(processed_face_image).unsqueeze(0).to("cuda")
59
 
60
- # Perform the transformation using the configured pipeline
61
  image = pipeline(
62
  prompt="soyjak",
63
- ip_adapter_image=[style_image_tensor, processed_face_image_tensor], # Ensure these are tensors
64
  negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
65
  num_inference_steps=30,
66
  generator=generator,
67
  ).images[0]
68
 
69
- # Convert the tensor to a PIL Image to display it in Gradio
70
- image = transforms.ToPILImage()(image.squeeze(0))
71
-
72
- # Move the pipeline back to CPU after processing to release GPU resources
73
- pipeline.to("cpu")
74
  return image
75
 
76
  # Gradio interface setup
@@ -79,8 +59,8 @@ demo = gr.Interface(
79
  inputs=gr.Image(label="Upload your face image"),
80
  outputs=gr.Image(label="Your Soyjak"),
81
  title="InstaSoyjak - turn anyone into a Soyjak",
82
- description="All you need to do is upload an image. Please use responsibly.",
83
  )
84
 
85
- demo.queue(max_size=20)
86
- demo.launch()
 
2
  import torch
3
  from PIL import Image
4
  from diffusers import AutoPipelineForText2Image, DDIMScheduler
5
+ from transformers import CLIPVisionModelWithProjection
6
  import numpy as np
7
+ import spaces # Import ZeroGPU decorator
8
+
9
+ # Load models and configure pipeline
10
+ image_encoder = CLIPVisionModelWithProjection.from_pretrained(
11
+ "h94/IP-Adapter",
12
+ subfolder="models/image_encoder",
13
+ torch_dtype=torch.float16,
14
+ )
15
 
 
16
  pipeline = AutoPipelineForText2Image.from_pretrained(
17
  "stabilityai/stable-diffusion-xl-base-1.0",
18
+ torch_dtype=torch.float16,
19
+ image_encoder=image_encoder,
20
  )
 
 
21
  pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
22
 
23
+ pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name=["ip-adapter-plus_sdxl_vit-h.safetensors", "ip-adapter-plus-face_sdxl_vit-h.safetensors"])
 
 
 
 
 
 
 
 
24
  pipeline.set_ip_adapter_scale([0.7, 0.5])
25
 
26
+ pipeline.enable_model_cpu_offload()
 
27
 
28
+ @spaces.ZeroGPU # Apply ZeroGPU decorator to the function
29
  def transform_image(face_image):
30
+ generator = torch.Generator(device="cpu").manual_seed(0)
 
 
31
 
32
+ # Check if the input is already a PIL Image
33
  if isinstance(face_image, Image.Image):
34
  processed_face_image = face_image
35
+ # If the input is a NumPy array, convert it to a PIL Image
36
  elif isinstance(face_image, np.ndarray):
37
  processed_face_image = Image.fromarray(face_image)
38
  else:
39
  raise ValueError("Unsupported image format")
 
 
 
 
 
 
40
 
41
+ # Load the style image from the local path
42
+ style_image_path = "/content/soyjak2.jpeg"
43
+ style_image = Image.open(style_image_path)
 
 
 
 
44
 
45
+ # Perform the transformation
46
  image = pipeline(
47
  prompt="soyjak",
48
+ ip_adapter_image=[style_image, processed_face_image],
49
  negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
50
  num_inference_steps=30,
51
  generator=generator,
52
  ).images[0]
53
 
 
 
 
 
 
54
  return image
55
 
56
  # Gradio interface setup
 
59
  inputs=gr.Image(label="Upload your face image"),
60
  outputs=gr.Image(label="Your Soyjak"),
61
  title="InstaSoyjak - turn anyone into a Soyjak",
62
+ description="All you need to do is upload an image. Please use responsibly. Please follow me on Twitter if you like this space: https://twitter.com/angrypenguinPNG. Idea from Yacine, please give him a follow: https://twitter.com/yacineMTB.",
63
  )
64
 
65
+ demo.queue(max_size=20) # Configures the queue with a maximum size of 20
66
+ demo.launch()