ciover2024 commited on
Commit
dcb1138
·
verified ·
1 Parent(s): aaadd05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -4
app.py CHANGED
@@ -1,18 +1,24 @@
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
 
 
 
 
4
  #import torch.nn.functional as F
5
  #import torchvision
6
  #import torchvision.transforms as T
 
 
7
  from diffusers import StableDiffusionInpaintPipeline
8
  import numpy as np
9
- #import cv2
10
  import os
11
  import shutil
12
  from gradio_client import Client, handle_file
13
 
14
  # Load the model once globally to avoid repeated loading
15
  def load_inpainting_model():
 
16
  model_path = "uberRealisticPornMerge_urpmv13Inpainting.safetensors"
17
  #model_path = "uberRealisticPornMerge_v23Inpainting.safetensors"
18
  #model_path = "pornmasterFantasy_v4-inpainting.safetensors"
@@ -25,16 +31,48 @@ def load_inpainting_model():
25
  ).to(device)
26
  return pipe
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Preload the model once
29
  inpaint_pipeline = load_inpainting_model()
 
 
30
 
31
  # Function to resize image (simpler interpolation method for speed)
32
  def resize_to_match(input_image, output_image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  #torch_img = pil_to_torch(input_image)
34
  #torch_img_scaled = F.interpolate(torch_img.unsqueeze(0),mode='trilinear').squeeze(0)
35
  #output_image = torchvision.transforms.functional.to_pil_image(torch_img_scaled, mode=None)
36
- #return output_image
37
- return output_image.resize(input_image.size, Image.BICUBIC) # Use BILINEAR for faster resizing
38
 
39
  # Function to generate the mask using Florence SAM Masking API (Replicate)
40
  def generate_mask(image_path, text_prompt="clothing"):
@@ -58,7 +96,7 @@ def inpaint_image(input_image, mask_image):
58
  prompt = "undress, naked"
59
  result = inpaint_pipeline(prompt=prompt, image=input_image, mask_image=mask_image)
60
  inpainted_image = result.images[0]
61
- #inpainted_image = resize_to_match(input_image, inpainted_image)
62
  return inpainted_image
63
 
64
  # Function to process input image and mask
 
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
+ from diffusers.utils import load_image
5
+ from diffusers import FluxControlNetModel
6
+ from diffusers.pipelines import FluxControlNetPipeline
7
+
8
  #import torch.nn.functional as F
9
  #import torchvision
10
  #import torchvision.transforms as T
11
+ #import cv2
12
+
13
  from diffusers import StableDiffusionInpaintPipeline
14
  import numpy as np
 
15
  import os
16
  import shutil
17
  from gradio_client import Client, handle_file
18
 
19
  # Load the model once globally to avoid repeated loading
20
  def load_inpainting_model():
21
+ # Load pipeline
22
  model_path = "uberRealisticPornMerge_urpmv13Inpainting.safetensors"
23
  #model_path = "uberRealisticPornMerge_v23Inpainting.safetensors"
24
  #model_path = "pornmasterFantasy_v4-inpainting.safetensors"
 
31
  ).to(device)
32
  return pipe
33
 
34
+ # Load the model once globally to avoid repeated loading
35
+ def load_upscaling_model():
36
+ # Load pipeline
37
+ device = "cpu" # Explicitly use CPU
38
+ controlnet = FluxControlNetModel.from_pretrained(
39
+ "jasperai/Flux.1-dev-Controlnet-Upscaler",
40
+ torch_dtype=torch.float32
41
+ )
42
+ pipe = FluxControlNetPipeline.from_pretrained(
43
+ "black-forest-labs/FLUX.1-dev",
44
+ controlnet=controlnet,
45
+ torch_dtype=torch.float32
46
+ ).to(device)
47
+ return pipe
48
+
49
  # Preload the model once
50
  inpaint_pipeline = load_inpainting_model()
51
+ # Preload the model once
52
+ upscale_pipeline = load_upscaling_model()
53
 
54
  # Function to resize image (simpler interpolation method for speed)
55
  def resize_to_match(input_image, output_image):
56
+
57
+ w, h = output_image.size
58
+ control_image = output_image.resize((w * 4, h * 4))
59
+
60
+ scaled_image = pipe(
61
+ prompt="",
62
+ control_image=control_image,
63
+ controlnet_conditioning_scale=0.6,
64
+ num_inference_steps=28,
65
+ guidance_scale=3.5,
66
+ height=control_image.size[1],
67
+ width=control_image.size[0]
68
+ ).images[0]
69
+
70
+ return scaled_image
71
+
72
  #torch_img = pil_to_torch(input_image)
73
  #torch_img_scaled = F.interpolate(torch_img.unsqueeze(0),mode='trilinear').squeeze(0)
74
  #output_image = torchvision.transforms.functional.to_pil_image(torch_img_scaled, mode=None)
75
+ #return output_image.resize(input_image.size, Image.BICUBIC) # Use BILINEAR for faster resizing
 
76
 
77
  # Function to generate the mask using Florence SAM Masking API (Replicate)
78
  def generate_mask(image_path, text_prompt="clothing"):
 
96
  prompt = "undress, naked"
97
  result = inpaint_pipeline(prompt=prompt, image=input_image, mask_image=mask_image)
98
  inpainted_image = result.images[0]
99
+ inpainted_image = resize_to_match(input_image, inpainted_image)
100
  return inpainted_image
101
 
102
  # Function to process input image and mask