Mobil-DataLab commited on
Commit
01c5659
1 Parent(s): 884bbd0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
61
+ low_thresh = gr.Slider(label="Umbral Bajo Canny", minimum=0, maximum=255, step=1, value=50)
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__":
73
+ demo.launch()