Mobil-DataLab commited on
Commit
6ddaf9f
1 Parent(s): 14006da

Update app.py

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