zaikaman commited on
Commit
6a9f219
·
verified ·
1 Parent(s): 85b4cf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -49
app.py CHANGED
@@ -8,31 +8,25 @@ import os
8
  import shutil
9
  from gradio_client import Client, handle_file
10
 
11
- # Function to load Stable Diffusion Inpainting model
12
- def load_inpainting_model(model_name):
13
- model_path = model_name
14
- device = "cuda" if torch.cuda.is_available() else "cpu"
15
  pipe = StableDiffusionInpaintPipeline.from_single_file(
16
  model_path,
17
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
18
  safety_checker=None
19
  ).to(device)
20
  return pipe
21
 
22
- # Function to resize image
 
 
 
23
  def resize_to_match(input_image, output_image):
24
- return output_image.resize(input_image.size, Image.LANCZOS)
25
-
26
- # Function to upscale image
27
- def upscale_image(image):
28
- scale_factor = 2 if torch.cuda.is_available() else 1
29
- image_np = np.array(image)
30
- if scale_factor == 1:
31
- return image
32
- upscaled_image_np = cv2.resize(image_np, (0, 0), fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_CUBIC)
33
- return Image.fromarray(upscaled_image_np)
34
-
35
- # Step 1: Generate the mask using Florence SAM Masking API (Replicate)
36
  def generate_mask(image_path, text_prompt="clothing"):
37
  client_sam = Client("SkalskiP/florence-sam-masking")
38
  mask_result = client_sam.predict(
@@ -42,49 +36,41 @@ def generate_mask(image_path, text_prompt="clothing"):
42
  )
43
  return mask_result # This is the local path to the generated mask
44
 
45
- # Step 2: Save the generated mask to the same directory
46
  def save_mask(mask_local_path, save_path="generated_mask.png"):
47
  try:
48
  shutil.copy(mask_local_path, save_path)
49
- print(f"Generated mask saved to {save_path}")
50
  except Exception as e:
51
  print(f"Failed to save the mask: {e}")
52
 
53
  # Function to perform inpainting
54
- def inpaint_image(input_image, mask_image, inpaint_pipeline):
55
  prompt = "undress, naked"
56
- device = "cuda" if torch.cuda.is_available() else "cpu"
57
  result = inpaint_pipeline(prompt=prompt, image=input_image, mask_image=mask_image)
58
  inpainted_image = result.images[0]
59
  inpainted_image = resize_to_match(input_image, inpainted_image)
60
- inpainted_image = upscale_image(inpainted_image)
61
  return inpainted_image
62
 
63
- # Function to process input image and mask with the selected model
64
- def process_image(input_image, model_choice):
65
  # Save the input image temporarily to process with Replicate
66
  input_image_path = "temp_input_image.png"
67
  input_image.save(input_image_path)
68
 
69
- # Step 1: Generate the mask using the Florence SAM Masking API
70
  mask_local_path = generate_mask(image_path=input_image_path)
71
 
72
- # Step 2: Save the generated mask
73
  mask_image_path = "generated_mask.png"
74
  save_mask(mask_local_path, save_path=mask_image_path)
75
 
76
- # Step 3: Load the selected inpainting model
77
- inpaint_pipeline = load_inpainting_model(model_choice)
78
-
79
- # Step 4: Open the mask image and perform inpainting
80
  mask_image = Image.open(mask_image_path)
81
- result_image = inpaint_image(input_image, mask_image, inpaint_pipeline)
82
 
83
- # Step 5: Clean up temporary files
84
- if os.path.exists(input_image_path):
85
- os.remove(input_image_path)
86
- if os.path.exists(mask_image_path):
87
- os.remove(mask_image_path)
88
 
89
  return result_image
90
 
@@ -94,22 +80,12 @@ with gr.Blocks() as demo:
94
  input_image = gr.Image(label="Upload Input Image", type="pil")
95
  output_image = gr.Image(type="pil", label="Output Image")
96
 
97
- # Dropdown for selecting the model checkpoint
98
- model_choice = gr.Dropdown(
99
- choices=[
100
- "uberRealisticPornMerge_v13Inpainting.safetensors",
101
- "realvisxlInpainting_v50Inpainting.safetensors"
102
- ],
103
- label="Choose Inpainting Model",
104
- value="uberRealisticPornMerge_v13Inpainting.safetensors" # Default model
105
- )
106
-
107
  # Button to trigger the process
108
  with gr.Row():
109
  btn = gr.Button("Run Inpainting")
110
 
111
  # Function to run when button is clicked
112
- btn.click(fn=process_image, inputs=[input_image, model_choice], outputs=output_image)
113
 
114
- # Launch the Gradio app with share=True to allow public access
115
  demo.launch(share=True)
 
8
  import shutil
9
  from gradio_client import Client, handle_file
10
 
11
+ # Load the model once globally to avoid repeated loading
12
+ def load_inpainting_model():
13
+ model_path = "uberRealisticPornMerge_v13Inpainting.safetensors"
14
+ device = "cpu" # Explicitly use CPU
15
  pipe = StableDiffusionInpaintPipeline.from_single_file(
16
  model_path,
17
+ torch_dtype=torch.float32, # Use float32 for CPU
18
  safety_checker=None
19
  ).to(device)
20
  return pipe
21
 
22
+ # Preload the model once
23
+ inpaint_pipeline = load_inpainting_model()
24
+
25
+ # Function to resize image (simpler interpolation method for speed)
26
  def resize_to_match(input_image, output_image):
27
+ return output_image.resize(input_image.size, Image.BILINEAR) # Use BILINEAR for faster resizing
28
+
29
+ # Function to generate the mask using Florence SAM Masking API (Replicate)
 
 
 
 
 
 
 
 
 
30
  def generate_mask(image_path, text_prompt="clothing"):
31
  client_sam = Client("SkalskiP/florence-sam-masking")
32
  mask_result = client_sam.predict(
 
36
  )
37
  return mask_result # This is the local path to the generated mask
38
 
39
+ # Save the generated mask
40
  def save_mask(mask_local_path, save_path="generated_mask.png"):
41
  try:
42
  shutil.copy(mask_local_path, save_path)
 
43
  except Exception as e:
44
  print(f"Failed to save the mask: {e}")
45
 
46
  # Function to perform inpainting
47
+ def inpaint_image(input_image, mask_image):
48
  prompt = "undress, naked"
 
49
  result = inpaint_pipeline(prompt=prompt, image=input_image, mask_image=mask_image)
50
  inpainted_image = result.images[0]
51
  inpainted_image = resize_to_match(input_image, inpainted_image)
 
52
  return inpainted_image
53
 
54
+ # Function to process input image and mask
55
+ def process_image(input_image):
56
  # Save the input image temporarily to process with Replicate
57
  input_image_path = "temp_input_image.png"
58
  input_image.save(input_image_path)
59
 
60
+ # Generate the mask using Florence SAM API
61
  mask_local_path = generate_mask(image_path=input_image_path)
62
 
63
+ # Save the generated mask
64
  mask_image_path = "generated_mask.png"
65
  save_mask(mask_local_path, save_path=mask_image_path)
66
 
67
+ # Open the mask image and perform inpainting
 
 
 
68
  mask_image = Image.open(mask_image_path)
69
+ result_image = inpaint_image(input_image, mask_image)
70
 
71
+ # Clean up temporary files
72
+ os.remove(input_image_path)
73
+ os.remove(mask_image_path)
 
 
74
 
75
  return result_image
76
 
 
80
  input_image = gr.Image(label="Upload Input Image", type="pil")
81
  output_image = gr.Image(type="pil", label="Output Image")
82
 
 
 
 
 
 
 
 
 
 
 
83
  # Button to trigger the process
84
  with gr.Row():
85
  btn = gr.Button("Run Inpainting")
86
 
87
  # Function to run when button is clicked
88
+ btn.click(fn=process_image, inputs=[input_image], outputs=output_image)
89
 
90
+ # Launch the Gradio app
91
  demo.launch(share=True)