AkashKumarave commited on
Commit
b42faf9
·
verified ·
1 Parent(s): 9de5dbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -2,15 +2,15 @@ import cv2
2
  import torch
3
  import numpy as np
4
  import gradio as gr
5
- from diffusers import StableDiffusionXLPipeline, ControlNetModel
6
  from insightface.app import FaceAnalysis
7
  from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
  import os
9
 
10
- # Force offline mode to avoid runtime Hub connections
11
  os.environ["HF_HUB_OFFLINE"] = "1"
12
 
13
- # Set device to CPU (free tier has no GPU)
14
  device = "cpu"
15
  dtype = torch.float32
16
 
@@ -19,29 +19,26 @@ face_app = FaceAnalysis(providers=["CPUExecutionProvider"])
19
  face_app.prepare(ctx_id=0, det_size=(480, 480))
20
 
21
  # Define paths for preloaded weights
22
- controlnet_path = "./ControlNetModel"
23
- face_adapter_path = "./ip-adapter.bin"
24
 
25
  # Check if files exist
26
- if not os.path.exists(controlnet_path) or not os.path.exists(os.path.join(controlnet_path, "config.json")):
27
- raise FileNotFoundError(f"ControlNetModel directory or config.json not found at {controlnet_path}")
28
- if not os.path.exists(face_adapter_path):
29
- raise FileNotFoundError(f"ip-adapter.bin not found at {face_adapter_path}")
30
 
31
- # Initialize models with empty weights
32
  with init_empty_weights():
33
- controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=dtype)
34
  pipe = StableDiffusionXLPipeline.from_pretrained(
35
- "stabilityai/stable-diffusion-xl-base-1.0",
36
- controlnet=controlnet,
37
  torch_dtype=dtype,
38
  safety_checker=None,
39
  )
40
 
41
- # Load and dispatch models with accelerate
42
- controlnet = load_checkpoint_and_dispatch(controlnet, controlnet_path, device_map="cpu", offload_folder=None)
43
- pipe = load_checkpoint_and_dispatch(pipe, "./", device_map="cpu", offload_folder=None)
44
- pipe.load_ip_adapter_instantid(face_adapter_path)
45
 
46
  def generate_image(uploaded_image, prompt):
47
  # Convert Gradio image to OpenCV format
@@ -61,7 +58,6 @@ def generate_image(uploaded_image, prompt):
61
  guidance_scale=7.5,
62
  height=512,
63
  width=512,
64
- controlnet_conditioning_scale=1.0,
65
  ).images[0]
66
  return "Image generated successfully!", image
67
  except Exception as e:
@@ -78,7 +74,7 @@ interface = gr.Interface(
78
  gr.Textbox(label="Status"),
79
  gr.Image(label="Generated Image")
80
  ],
81
- title="Face Reference Image Generator",
82
  description="Upload an image with a face, enter a prompt, and generate a new image preserving the reference face."
83
  )
84
 
 
2
  import torch
3
  import numpy as np
4
  import gradio as gr
5
+ from diffusers import StableDiffusionXLPipeline
6
  from insightface.app import FaceAnalysis
7
  from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
  import os
9
 
10
+ # Force offline mode
11
  os.environ["HF_HUB_OFFLINE"] = "1"
12
 
13
+ # Set device to CPU
14
  device = "cpu"
15
  dtype = torch.float32
16
 
 
19
  face_app.prepare(ctx_id=0, det_size=(480, 480))
20
 
21
  # Define paths for preloaded weights
22
+ model_path = "./" # Kolors base model
23
+ ip_adapter_path = "./ip-adapter-faceid-plus-sdxl"
24
 
25
  # Check if files exist
26
+ if not os.path.exists(model_path + "diffusion_pytorch_model.safetensors"):
27
+ raise FileNotFoundError(f"Kolors model weights not found at {model_path}")
28
+ if not os.path.exists(ip_adapter_path) or not os.path.exists(os.path.join(ip_adapter_path, "config.json")):
29
+ raise FileNotFoundError(f"IP-Adapter weights or config.json not found at {ip_adapter_path}")
30
 
31
+ # Initialize model with empty weights
32
  with init_empty_weights():
 
33
  pipe = StableDiffusionXLPipeline.from_pretrained(
34
+ "Kwai-Kolors/Kolors-diffusers",
 
35
  torch_dtype=dtype,
36
  safety_checker=None,
37
  )
38
 
39
+ # Load and dispatch model with accelerate
40
+ pipe = load_checkpoint_and_dispatch(pipe, model_path, device_map="cpu", offload_folder=None)
41
+ pipe.load_ip_adapter("h94/IP-Adapter-FaceID-Plus-SDXL", subfolder=None, weight_name="ip-adapter.bin")
 
42
 
43
  def generate_image(uploaded_image, prompt):
44
  # Convert Gradio image to OpenCV format
 
58
  guidance_scale=7.5,
59
  height=512,
60
  width=512,
 
61
  ).images[0]
62
  return "Image generated successfully!", image
63
  except Exception as e:
 
74
  gr.Textbox(label="Status"),
75
  gr.Image(label="Generated Image")
76
  ],
77
+ title="Face Reference Image Generator (Kolors with IP-Adapter)",
78
  description="Upload an image with a face, enter a prompt, and generate a new image preserving the reference face."
79
  )
80