File size: 17,835 Bytes
7ce094c
75786fa
02350f6
 
 
 
 
 
 
75786fa
02350f6
 
 
 
 
75786fa
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75786fa
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75786fa
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75786fa
02350f6
75786fa
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ce094c
75786fa
7ce094c
02350f6
 
 
 
7ce094c
 
e411def
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e411def
 
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ce094c
 
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e411def
7ce094c
 
 
02350f6
 
 
7ce094c
02350f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ce094c
02350f6
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import os
import gradio as gr
from diffusers import DiffusionPipeline
import torch
import requests
from PIL import Image
from io import BytesIO
import concurrent.futures
import threading

# ==============================
#   CONFIGURACIÓN BASE CPU
# ==============================
DEVICE = "cpu"
torch.set_grad_enabled(False)

# PARÁMETROS POR DEFECTO AJUSTADOS PARA CPU MÁS RÁPIDO
DEFAULT_STEPS = 15  # Reducido de 20 para más velocidad
DEFAULT_WIDTH = 512  # Reducido de 576 para menos carga
DEFAULT_HEIGHT = 768  # Reducido de 1024 para menos carga (mantiene relación aproximada)

def load_flux(model_id):
    pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
    pipe.to(DEVICE)
    pipe.enable_attention_slicing()
    return pipe

# Cache de modelos
MODEL_CACHE = {}

# ==============================
#   GENERADOR FLUX
# ==============================
def generate_flux(model_name, prompt, steps, guidance, width, height, seed):
    if model_name not in MODEL_CACHE:
        MODEL_CACHE[model_name] = load_flux(model_name)
    pipe = MODEL_CACHE[model_name]

    generator = torch.manual_seed(seed) if seed else None

    image = pipe(
        prompt=prompt,
        num_inference_steps=steps,
        guidance_scale=guidance,
        width=width,
        height=height,
        generator=generator
    ).images[0]

    out = "/tmp/flux_output.png"
    image.save(out)
    return out

# ==============================
#   GENERADOR SD1.5
# ==============================
def load_sd15():
    pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
    pipe.to(DEVICE)
    pipe.enable_attention_slicing()
    return pipe

# SD15 load único
def generate_sd(prompt, steps, guidance, width, height, seed):
    if "sd15" not in MODEL_CACHE:
        MODEL_CACHE["sd15"] = load_sd15()
    pipe = MODEL_CACHE["sd15"]

    generator = torch.manual_seed(seed) if seed else None

    image = pipe(
        prompt=prompt,
        num_inference_steps=steps,
        guidance_scale=guidance,
        width=width,
        height=height,
        generator=generator
    ).images[0]

    out = "/tmp/sd15_output.png"
    image.save(out)
    return out

# ==============================
#   REVE CREATE MODIFICADO (Generación múltiple con threads)
# ==============================
def generate_single_reve_image(prompt, key, model, index, results_list, lock, progress_callback=None):
    """Función auxiliar para generar una sola imagen"""
    try:
        url = "https://api.reveai.xyz/v1/images"
        headers = {"Authorization": f"Bearer {key}"}
        data = {"prompt": prompt, "model": model}
        
        resp = requests.post(url, json=data, headers=headers, timeout=30)
        
        if resp.status_code != 200:
            print(f"Error en imagen {index+1}: {resp.status_code}")
            return
        
        img_url = resp.json().get("image")
        if not img_url:
            return
            
        img_data = requests.get(img_url, timeout=30).content
        img = Image.open(BytesIO(img_data))
        
        out = f"/tmp/reve_{index}_{threading.current_thread().ident}.png"
        img.save(out)
        
        with lock:
            results_list.append(out)
        
        # Notificar progreso si hay callback
        if progress_callback:
            progress_callback(index + 1)
            
    except Exception as e:
        print(f"Error generando imagen {index+1}: {e}")

def reve_generate_multiple(prompt, key, model, num_images, progress_callback=None):
    if not key:
        return None
    
    num_images = min(num_images, 8)  # Máximo 8 imágenes
    results = []
    lock = threading.Lock()
    
    # Usamos ThreadPoolExecutor para generar imágenes en paralelo
    with concurrent.futures.ThreadPoolExecutor(max_workers=min(num_images, 4)) as executor:
        futures = []
        for i in range(num_images):
            future = executor.submit(
                generate_single_reve_image,
                prompt, key, model, i, results, lock, progress_callback
            )
            futures.append(future)
        
        # Esperar a que todas terminen
        concurrent.futures.wait(futures)
    
    return results if results else None

# ==============================
#   UI COMPLETA
# ==============================
def build_ui():
    with gr.Blocks(title="BATUTO-ART MIX", theme=gr.themes.Soft()) as demo:

        gr.Markdown("""
        # 🖼️ **BATUTO-ART MIX**
        *Generador de imágenes con FLUX, Stable Diffusion 1.5 y REVE CREATE*
        """)

        with gr.Tabs():

            # ============================
            # TAB: FLUX
            # ============================
            with gr.Tab("FLUX.2 / 1-Schnell"):
                with gr.Row():
                    with gr.Column(scale=1):
                        flux_prompt = gr.Textbox(
                            label="Prompt", 
                            lines=3,
                            placeholder="Describe la imagen que quieres generar con FLUX..."
                        )
                        model_select = gr.Dropdown([
                            "black-forest-labs/FLUX.1-schnell",
                            "black-forest-labs/FLUX.1-dev",
                            "black-forest-labs/FLUX.2-dev"
                        ], value="black-forest-labs/FLUX.1-schnell", label="Modelo FLUX")
                        
                        with gr.Row():
                            steps = gr.Slider(5, 50, value=DEFAULT_STEPS, step=1, label="Steps")
                            guidance = gr.Slider(0, 10, value=3, step=0.1, label="Guidance Scale")
                        
                        with gr.Row():
                            width = gr.Number(value=DEFAULT_WIDTH, label="Width", precision=0)
                            height = gr.Number(value=DEFAULT_HEIGHT, label="Height", precision=0)
                        
                        seed = gr.Number(value=0, label="Seed (0 = aleatorio)", precision=0)
                        
                        btn_flux = gr.Button("✨ Generar Imagen", variant="primary", size="lg")
                        
                    with gr.Column(scale=1):
                        out_flux_img = gr.Image(
                            label="Resultado FLUX",
                            height=400,
                            interactive=False
                        )
                        out_flux_file = gr.File(
                            label="Descargar imagen",
                            visible=False
                        )
                
                # Acción del botón FLUX
                def generate_flux_wrapper(model_name, prompt, steps, guidance, width, height, seed):
                    if not prompt.strip():
                        return None, "❌ Error: Ingresa un prompt válido"
                    try:
                        file_path = generate_flux(
                            model_name, prompt, int(steps), float(guidance), 
                            int(width), int(height), int(seed)
                        )
                        return file_path, gr.update(visible=True)
                    except Exception as e:
                        return None, f"❌ Error: {str(e)}"
                
                btn_flux.click(
                    fn=generate_flux_wrapper,
                    inputs=[model_select, flux_prompt, steps, guidance, width, height, seed],
                    outputs=[out_flux_file, out_flux_file]
                )
                
                # Mostrar imagen automáticamente
                out_flux_file.change(
                    fn=lambda f: Image.open(f) if f else None, 
                    inputs=[out_flux_file], 
                    outputs=[out_flux_img]
                )

            # ============================
            # TAB: SD1.5
            # ============================
            with gr.Tab("Stable Diffusion 1.5"):
                with gr.Row():
                    with gr.Column(scale=1):
                        sd_prompt = gr.Textbox(
                            label="Prompt", 
                            lines=3,
                            placeholder="Describe la imagen que quieres generar con SD1.5..."
                        )
                        
                        with gr.Row():
                            sd_steps = gr.Slider(5, 50, value=DEFAULT_STEPS, step=1, label="Steps")
                            sd_guidance = gr.Slider(0, 10, value=3, step=0.1, label="Guidance Scale")
                        
                        with gr.Row():
                            sd_width = gr.Number(value=DEFAULT_WIDTH, label="Width", precision=0)
                            sd_height = gr.Number(value=DEFAULT_HEIGHT, label="Height", precision=0)
                        
                        sd_seed = gr.Number(value=0, label="Seed (0 = aleatorio)", precision=0)
                        
                        btn_sd = gr.Button("✨ Generar Imagen", variant="primary", size="lg")
                        
                    with gr.Column(scale=1):
                        out_sd_img = gr.Image(
                            label="Resultado SD1.5",
                            height=400,
                            interactive=False
                        )
                        out_sd_file = gr.File(
                            label="Descargar imagen",
                            visible=False
                        )
                
                # Acción del botón SD1.5
                def generate_sd_wrapper(prompt, steps, guidance, width, height, seed):
                    if not prompt.strip():
                        return None, "❌ Error: Ingresa un prompt válido"
                    try:
                        file_path = generate_sd(
                            prompt, int(steps), float(guidance), 
                            int(width), int(height), int(seed)
                        )
                        return file_path, gr.update(visible=True)
                    except Exception as e:
                        return None, f"❌ Error: {str(e)}"
                
                btn_sd.click(
                    fn=generate_sd_wrapper,
                    inputs=[sd_prompt, sd_steps, sd_guidance, sd_width, sd_height, sd_seed],
                    outputs=[out_sd_file, out_sd_file]
                )
                
                out_sd_file.change(
                    fn=lambda f: Image.open(f) if f else None, 
                    inputs=[out_sd_file], 
                    outputs=[out_sd_img]
                )

            # ============================
            # TAB: REVE CREATE MODIFICADA
            # ============================
            with gr.Tab("REVE CREATE"):
                with gr.Row():
                    with gr.Column(scale=1):
                        reve_api = gr.Textbox(
                            label="API Key REVE", 
                            type="password",
                            placeholder="Ingresa tu API key de REVE",
                            info="Necesitas una clave API válida de REVE"
                        )
                        reve_prompt = gr.Textbox(
                            label="Prompt", 
                            lines=3,
                            placeholder="Describe la imagen que quieres generar..."
                        )
                        reve_model = gr.Dropdown([
                            "reve-1",
                            "reve-2",
                            "reve-fast"
                        ], value="reve-fast", label="Modelo REVE")
                        
                        # Slider para cantidad de imágenes
                        num_images_slider = gr.Slider(
                            minimum=1, 
                            maximum=8, 
                            value=4, 
                            step=1,
                            label="Cantidad de imágenes a generar"
                        )
                        
                        with gr.Row():
                            btn_reve = gr.Button("🚀 Generar Imágenes", variant="primary", size="lg")
                            btn_clear = gr.Button("🗑️ Limpiar", variant="secondary")
                        
                        # Indicador de progreso
                        progress_info = gr.Markdown("Esperando generación...")
                        
                    with gr.Column(scale=2):
                        # Galería para mostrar múltiples imágenes
                        gallery = gr.Gallery(
                            label="Imágenes generadas",
                            show_label=True,
                            columns=4,
                            rows=2,
                            height="auto",
                            object_fit="contain"
                        )
                        
                        # Información de resultados
                        result_info = gr.Markdown("")
                        
                        # Botón de descarga
                        with gr.Row():
                            btn_download_all = gr.Button(
                                "📥 Descargar todas las imágenes", 
                                size="lg",
                                variant="secondary"
                            )
                            download_output = gr.File(
                                label="Archivos descargables", 
                                file_count="multiple",
                                visible=False
                            )
                
                # Estado para almacenar las rutas de archivos
                last_files = gr.State([])
                
                # Función para generar múltiples imágenes con progreso
                def generate_and_update_gallery(prompt, key, model, num_images, progress=gr.Progress()):
                    if not key:
                        return [], [], "❌ Error: Ingresa una API key válida", progress_info.update(value="")
                    
                    if not prompt.strip():
                        return [], [], "❌ Error: Ingresa un prompt válido", progress_info.update(value="")
                    
                    progress_info.update(value="⏳ Iniciando generación...")
                    
                    # Función de callback para progreso
                    def update_progress(current):
                        progress_info.update(value=f"⏳ Generando imagen {current}/{num_images}...")
                    
                    files = reve_generate_multiple(prompt, key, model, num_images, update_progress)
                    
                    if files:
                        # Crear lista de imágenes para la galería
                        images = [(file,) for file in files]
                        info_text = f"✅ Generadas {len(files)} de {num_images} imágenes"
                        progress_info.update(value="✅ Generación completada")
                        return images, files, info_text
                    else:
                        progress_info.update(value="❌ Error en la generación")
                        return [], [], "❌ Error: No se pudieron generar imágenes. Verifica tu API key y conexión."
                
                # Acción del botón de generación
                btn_reve.click(
                    fn=generate_and_update_gallery,
                    inputs=[reve_prompt, reve_api, reve_model, num_images_slider],
                    outputs=[gallery, last_files, result_info]
                )
                
                # Acción del botón de descarga
                def prepare_download(files):
                    if files:
                        return gr.update(value=files, visible=True)
                    return gr.update(value=[], visible=False)
                
                btn_download_all.click(
                    fn=prepare_download,
                    inputs=[last_files],
                    outputs=[download_output]
                )
                
                # Acción del botón de limpiar
                def clear_gallery():
                    return [], [], "✅ Galería limpiada", gr.update(value="Listo para nueva generación")
                
                btn_clear.click(
                    fn=clear_gallery,
                    outputs=[gallery, last_files, result_info, progress_info]
                ).then(
                    fn=lambda: gr.update(visible=False),
                    outputs=[download_output]
                )

    return demo

# ==============================
#   EJECUCIÓN PRINCIPAL
# ==============================
if __name__ == "__main__":
    # Configuración para HuggingFace Spaces
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--share", action="store_true", help="Crear enlace público")
    parser.add_argument("--server-name", type=str, default="0.0.0.0", help="Dirección del servidor")
    parser.add_argument("--server-port", type=int, default=7860, help="Puerto del servidor")
    args = parser.parse_args()
    
    print("=" * 50)
    print("🚀 Iniciando BATUTO-ART MIX")
    print("=" * 50)
    print(f"📱 Dispositivo: {DEVICE}")
    print(f"⚡ Steps por defecto: {DEFAULT_STEPS}")
    print(f"📐 Resolución por defecto: {DEFAULT_WIDTH}x{DEFAULT_HEIGHT}")
    print("=" * 50)
    
    demo = build_ui()
    demo.launch(
        share=args.share,
        server_name=args.server_name,
        server_port=args.server_port,
        show_error=True
    )