ds1david commited on
Commit
2718083
·
1 Parent(s): 1b614d0

Trying fix variants

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -1,33 +1,31 @@
1
  import gradio as gr
2
  import torch
3
  import numpy as np
4
- from diffusers import StableDiffusionXLImg2ImgPipeline
5
  from transformers import DPTFeatureExtractor, DPTForDepthEstimation
6
  from PIL import Image, ImageEnhance, ImageOps
7
 
8
- # Configuração de dispositivo
9
  device = "cpu" # or "cuda" if you have a GPU
10
  torch_dtype = torch.float32
11
 
12
- print("Carregando modelo SDXL Img2Img...")
13
- pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
14
  "stabilityai/stable-diffusion-xl-base-1.0",
15
  torch_dtype=torch_dtype
16
  ).to(device)
17
 
18
- print("Carregando pesos LoRA weights with PEFT...")
19
  pipe.load_lora_weights(
20
- "KappaNeuro/bas-relief",
21
  weight_name="BAS-RELIEF.safetensors",
22
- peft_backend="peft"
23
  )
24
 
25
- print("Carregando modelo de profundidade...")
26
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
27
  depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(device)
28
 
29
-
30
- def processar_profundidade(depth_arr: np.ndarray) -> Image.Image:
31
  d_min, d_max = depth_arr.min(), depth_arr.max()
32
  depth_stretched = (depth_arr - d_min) / (d_max - d_min + 1e-8)
33
  depth_stretched = (depth_stretched * 255).astype(np.uint8)
@@ -40,16 +38,16 @@ def processar_profundidade(depth_arr: np.ndarray) -> Image.Image:
40
 
41
  return depth_pil
42
 
43
-
44
- def processar_imagem(imagem: Image.Image):
45
- # Pré-processamento
46
  print("Generating image with LoRA style...")
47
  result = pipe(
48
- prompt="BAS-RELIEF",
49
  image=imagem,
50
- num_inference_steps=15, # reduce if too slow
51
  guidance_scale=7.5,
52
- height=512, # reduce if you still get timeouts
53
  width=512
54
  )
55
  image = result.images[0]
@@ -67,16 +65,13 @@ def processar_imagem(imagem: Image.Image):
67
  align_corners=False
68
  ).squeeze()
69
 
70
- depth_map_pil = processar_profundidade(prediction.cpu().numpy())
71
 
72
  return image, depth_map_pil
73
 
74
- # return resultado.images[0], processar_profundidade(depth_map)
75
-
76
-
77
  # Interface Gradio
78
  interface = gr.Interface(
79
- fn=processar_imagem,
80
  inputs=gr.Image(type="pil"),
81
  outputs=[gr.Image(label="Resultado"), gr.Image(label="Profundidade")],
82
  title="Conversor para Baixo-relevo",
 
1
  import gradio as gr
2
  import torch
3
  import numpy as np
4
+ from diffusers import StableDiffusionXLPipeline
5
  from transformers import DPTFeatureExtractor, DPTForDepthEstimation
6
  from PIL import Image, ImageEnhance, ImageOps
7
 
 
8
  device = "cpu" # or "cuda" if you have a GPU
9
  torch_dtype = torch.float32
10
 
11
+ print("Loading SDXL Base model...")
12
+ pipe = StableDiffusionXLPipeline.from_pretrained(
13
  "stabilityai/stable-diffusion-xl-base-1.0",
14
  torch_dtype=torch_dtype
15
  ).to(device)
16
 
17
+ print("Loading bas-relief LoRA weights with PEFT...")
18
  pipe.load_lora_weights(
19
+ "KappaNeuro/bas-relief", # The HF repo with BAS-RELIEF.safetensors
20
  weight_name="BAS-RELIEF.safetensors",
21
+ peft_backend="peft" # This is crucial
22
  )
23
 
24
+ print("Loading DPT Depth Model...")
25
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
26
  depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(device)
27
 
28
+ def enhance_depth_map(depth_arr: np.ndarray) -> Image.Image:
 
29
  d_min, d_max = depth_arr.min(), depth_arr.max()
30
  depth_stretched = (depth_arr - d_min) / (d_max - d_min + 1e-8)
31
  depth_stretched = (depth_stretched * 255).astype(np.uint8)
 
38
 
39
  return depth_pil
40
 
41
+ def generate_bas_relief_and_depth(imagem):
42
+ # Use the token "BAS-RELIEF" so the LoRA triggers
43
+ full_prompt = f"BAS-RELIEF {prompt}"
44
  print("Generating image with LoRA style...")
45
  result = pipe(
46
+ prompt=full_prompt,
47
  image=imagem,
48
+ num_inference_steps=15, # reduce if too slow
49
  guidance_scale=7.5,
50
+ height=512, # reduce if you still get timeouts
51
  width=512
52
  )
53
  image = result.images[0]
 
65
  align_corners=False
66
  ).squeeze()
67
 
68
+ depth_map_pil = enhance_depth_map(prediction.cpu().numpy())
69
 
70
  return image, depth_map_pil
71
 
 
 
 
72
  # Interface Gradio
73
  interface = gr.Interface(
74
+ fn=generate_bas_relief_and_depth,
75
  inputs=gr.Image(type="pil"),
76
  outputs=[gr.Image(label="Resultado"), gr.Image(label="Profundidade")],
77
  title="Conversor para Baixo-relevo",