Mobil-DataLab commited on
Commit
6aa35ae
1 Parent(s): ff77928

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -41
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import torch
2
  from controlnet_aux import CannyDetector
3
- from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
4
  from diffusers.utils import load_image
5
  import gradio as gr
6
  import random
@@ -9,69 +9,41 @@ import os
9
  # Instalar dependencias en Hugging Face Spaces
10
  os.system("pip install -U diffusers controlnet_aux mediapipe")
11
 
 
12
  MAX_SEED = 2**32 - 1
13
  MAX_IMAGE_SIZE = 1024
14
 
15
- # Inicializar el modelo ControlNet
16
- controlnet = ControlNetModel.from_pretrained(
17
- "black-forest-labs/FLUX.1-Canny-dev", torch_dtype=torch.float32
18
- )
19
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
20
- "black-forest-labs/FLUX.1-Canny-dev", controlnet=controlnet, torch_dtype=torch.float32
21
  ).to("cuda" if torch.cuda.is_available() else "cpu")
22
 
23
  # Procesador Canny
24
  processor = CannyDetector()
25
 
 
26
  def generate_image(image, prompt, seed, random_seed, low_thresh, high_thresh, guidance_scale, num_inference_steps):
 
27
  if random_seed:
28
  seed = random.randint(0, MAX_SEED)
29
  generator = torch.manual_seed(seed)
30
-
31
- # Procesar la imagen con CannyDetector
32
  control_image = processor(
33
  image, low_threshold=low_thresh, high_threshold=high_thresh,
34
  detect_resolution=MAX_IMAGE_SIZE, image_resolution=MAX_IMAGE_SIZE
35
  )
36
-
37
- # Generar la imagen
38
  result = pipe(
39
  prompt=prompt,
40
  control_image=control_image,
41
- height=image.height,
42
- width=image.width,
43
- num_inference_steps=num_inference_steps,
44
  guidance_scale=guidance_scale,
45
  generator=generator
46
  ).images[0]
47
-
48
  return result, seed
49
 
50
- # Interfaz Gradio
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# Generador de Imágenes con FLUX.1 Canny-dev 🚀")
53
-
54
- with gr.Row():
55
- input_image = gr.Image(type="pil", label="Sube tu imagen de control")
56
- output_image = gr.Image(label="Imagen Generada")
57
-
58
- with gr.Row():
59
- prompt = gr.Textbox(label="Prompt", placeholder="Describe la imagen que deseas generar...")
60
- generate_button = gr.Button("Generar Imagen")
61
-
62
- with gr.Accordion("Configuración Avanzada", open=False):
63
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
64
- random_seed = gr.Checkbox(label="Randomizar Seed", value=True)
65
- low_thresh = gr.Slider(label="Umbral Bajo Canny", minimum=0, maximum=255, step=1, value=50)
66
- high_thresh = gr.Slider(label="Umbral Alto Canny", minimum=0, maximum=255, step=1, value=200)
67
- guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=30.0, step=0.1, value=7.5)
68
- num_inference_steps = gr.Slider(label="Pasos de Inferencia", minimum=1, maximum=50, step=1, value=20)
69
-
70
- generate_button.click(
71
- generate_image,
72
- inputs=[input_image, prompt, seed, random_seed, low_thresh, high_thresh, guidance_scale, num_inference_steps],
73
- outputs=[output_image, seed]
74
- )
75
-
76
- if __name__ == "__main__":
77
- demo.launch()
 
1
  import torch
2
  from controlnet_aux import CannyDetector
3
+ from diffusers import FluxControlPipeline
4
  from diffusers.utils import load_image
5
  import gradio as gr
6
  import random
 
9
  # Instalar dependencias en Hugging Face Spaces
10
  os.system("pip install -U diffusers controlnet_aux mediapipe")
11
 
12
+ # Máximo valor para seeds y tamaño de imagen
13
  MAX_SEED = 2**32 - 1
14
  MAX_IMAGE_SIZE = 1024
15
 
16
+ # Inicialización del pipeline Flux Canny Dev
17
+ pipe = FluxControlPipeline.from_pretrained(
18
+ "black-forest-labs/FLUX.1-Canny-dev", torch_dtype=torch.float16
 
 
 
19
  ).to("cuda" if torch.cuda.is_available() else "cpu")
20
 
21
  # Procesador Canny
22
  processor = CannyDetector()
23
 
24
+ @spaces.GPU
25
  def generate_image(image, prompt, seed, random_seed, low_thresh, high_thresh, guidance_scale, num_inference_steps):
26
+ # Controlar el seed para reproducibilidad
27
  if random_seed:
28
  seed = random.randint(0, MAX_SEED)
29
  generator = torch.manual_seed(seed)
30
+
31
+ # Aplicar CannyDetector a la imagen de control
32
  control_image = processor(
33
  image, low_threshold=low_thresh, high_threshold=high_thresh,
34
  detect_resolution=MAX_IMAGE_SIZE, image_resolution=MAX_IMAGE_SIZE
35
  )
36
+
37
+ # Generar imagen con el modelo Flux
38
  result = pipe(
39
  prompt=prompt,
40
  control_image=control_image,
 
 
 
41
  guidance_scale=guidance_scale,
42
  generator=generator
43
  ).images[0]
44
+
45
  return result, seed
46
 
47
+ # Interfaz de usuario Gradio
48
  with gr.Blocks() as demo:
49
  gr.Markdown("# Generador de Imágenes con FLUX.1 Canny-dev 🚀")