Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,172 +2,318 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
from clip_interrogator import Config, Interrogator
|
| 6 |
import logging
|
| 7 |
import os
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Configurar logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
CLIP_MODELS = {
|
| 17 |
"general": "ViT-L-14/openai",
|
| 18 |
-
"
|
| 19 |
-
"midjourney": "ViT-H-14/laion2b_s32b_b79k",
|
| 20 |
-
"
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# Modos de interrogación
|
| 24 |
INTERROGATION_MODES = {
|
| 25 |
-
"fast": "Rápido (
|
| 26 |
-
"classic": "Clásico (
|
| 27 |
-
"best": "Mejor (
|
| 28 |
-
"negative": "Negativo (lo que NO es)"
|
| 29 |
}
|
| 30 |
|
| 31 |
-
class
|
| 32 |
def __init__(self):
|
| 33 |
-
self.
|
| 34 |
self.usage_count = 0
|
| 35 |
-
self.
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
def
|
| 38 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
config = Config(
|
| 44 |
clip_model_name="ViT-L-14/openai",
|
| 45 |
download_cache=True,
|
| 46 |
-
chunk_size=2048,
|
| 47 |
-
quiet=
|
|
|
|
| 48 |
)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
-
logger.error(f"Error inicializando
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if image is None:
|
| 62 |
return "❌ Por favor, sube una imagen primero.", ""
|
| 63 |
|
| 64 |
-
# Incrementar contador
|
| 65 |
self.usage_count += 1
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
image = image.convert('RGB')
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
info = f"""
|
| 90 |
**✅ Prompt generado exitosamente con IA para todos**
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
- **Modo:** {INTERROGATION_MODES.get(mode, mode)}
|
|
|
|
|
|
|
| 93 |
- **Usos totales:** {self.usage_count}
|
| 94 |
- **Hora:** {datetime.now().strftime('%H:%M:%S')}
|
| 95 |
|
| 96 |
*"Porque cuando no tienes nada en la cabeza, te preocupas de la tipografía?"* 😄
|
|
|
|
|
|
|
| 97 |
"""
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
return prompt, info
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
logger.error(f"Error generando prompt: {e}")
|
| 103 |
error_msg = f"❌ Error: {str(e)}"
|
| 104 |
-
error_info = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
return error_msg, error_info
|
| 106 |
|
| 107 |
# Inicializar generador
|
| 108 |
-
generator =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
def
|
| 111 |
-
"""
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
# Crear interfaz
|
| 116 |
def create_interface():
|
| 117 |
-
# CSS
|
| 118 |
custom_css = """
|
| 119 |
.gradio-container {
|
| 120 |
-
max-width:
|
| 121 |
-
font-family: 'Inter', 'Segoe UI',
|
| 122 |
}
|
| 123 |
.prompt-output {
|
| 124 |
-
font-family: '
|
| 125 |
font-size: 14px !important;
|
| 126 |
line-height: 1.6 !important;
|
| 127 |
-
background: #f8f9fa !important;
|
| 128 |
-
border-radius:
|
| 129 |
-
padding:
|
|
|
|
|
|
|
| 130 |
}
|
| 131 |
.main-title {
|
| 132 |
text-align: center;
|
| 133 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 134 |
-webkit-background-clip: text;
|
| 135 |
-webkit-text-fill-color: transparent;
|
| 136 |
-
|
| 137 |
-
font-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
.subtitle {
|
| 141 |
text-align: center;
|
| 142 |
font-style: italic;
|
| 143 |
-
color: #
|
| 144 |
-
font-size: 1.
|
| 145 |
margin-bottom: 2em;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
"""
|
| 148 |
|
| 149 |
with gr.Blocks(
|
| 150 |
theme=gr.themes.Soft(),
|
| 151 |
-
title="IA para todos - Image to Prompt",
|
| 152 |
css=custom_css
|
| 153 |
) as interface:
|
| 154 |
|
| 155 |
# Header personalizado
|
| 156 |
-
gr.HTML("""
|
| 157 |
<div class="main-title">
|
| 158 |
🤖 IA para todos
|
| 159 |
</div>
|
| 160 |
-
""")
|
| 161 |
-
|
| 162 |
-
gr.HTML("""
|
| 163 |
<div class="subtitle">
|
| 164 |
"Porque cuando no tienes nada en la cabeza, te preocupas de la tipografía?"
|
| 165 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
""")
|
| 167 |
|
| 168 |
gr.Markdown("""
|
| 169 |
### 🎨 Convierte cualquier imagen en prompts detallados para IA
|
| 170 |
-
|
| 171 |
""")
|
| 172 |
|
| 173 |
with gr.Row():
|
|
@@ -175,27 +321,34 @@ def create_interface():
|
|
| 175 |
# Input section
|
| 176 |
gr.Markdown("## 📤 Subir Imagen")
|
| 177 |
image_input = gr.Image(
|
| 178 |
-
label="Arrastra o selecciona una imagen",
|
| 179 |
type="pil",
|
| 180 |
-
height=
|
| 181 |
)
|
| 182 |
|
| 183 |
# Configuración
|
| 184 |
-
gr.Markdown("## ⚙️ Configuración")
|
| 185 |
model_selector = gr.Dropdown(
|
| 186 |
choices=["general", "stable_diffusion", "midjourney", "flux"],
|
| 187 |
value="general",
|
| 188 |
-
label="Modelo de IA objetivo",
|
| 189 |
-
info="
|
| 190 |
)
|
| 191 |
|
| 192 |
mode_selector = gr.Dropdown(
|
| 193 |
-
choices=
|
| 194 |
value="best",
|
| 195 |
-
label="Modo de análisis",
|
| 196 |
info="Equilibrio entre velocidad y precisión"
|
| 197 |
)
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
# Botón generar
|
| 200 |
generate_btn = gr.Button(
|
| 201 |
"🚀 Generar Prompt Mágico",
|
|
@@ -207,62 +360,79 @@ def create_interface():
|
|
| 207 |
# Output section
|
| 208 |
gr.Markdown("## 📝 Tu Prompt Está Listo")
|
| 209 |
prompt_output = gr.Textbox(
|
| 210 |
-
label="Prompt generado (listo para copiar)",
|
| 211 |
-
placeholder="Tu prompt aparecerá
|
| 212 |
-
lines=
|
| 213 |
-
max_lines=
|
| 214 |
elem_classes=["prompt-output"],
|
| 215 |
show_copy_button=True
|
| 216 |
)
|
| 217 |
|
| 218 |
info_output = gr.Markdown(
|
| 219 |
-
label="Información del
|
| 220 |
value=""
|
| 221 |
)
|
| 222 |
|
| 223 |
# Botones de acción
|
| 224 |
with gr.Row():
|
| 225 |
clear_btn = gr.Button("🗑️ Limpiar", size="sm")
|
|
|
|
| 226 |
|
| 227 |
-
# Footer
|
| 228 |
-
gr.Markdown("""
|
| 229 |
---
|
| 230 |
-
### 💡
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
| 233 |
- **Midjourney:** Perfecto para estilos artísticos y creativos
|
| 234 |
- **Flux:** Para el revolucionario modelo Flux de Black Forest Labs
|
| 235 |
|
| 236 |
-
|
| 237 |
-
- **Rápido:** Análisis express,
|
| 238 |
-
- **Clásico:**
|
| 239 |
-
- **Mejor:** Máxima precisión
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
---
|
| 242 |
|
| 243 |
-
### 🎭 Hecho con amor (y
|
| 244 |
*"La IA nos ayuda con las ideas, nosotros nos preocupamos de que se vean bonitas"* ✨
|
|
|
|
|
|
|
| 245 |
""")
|
| 246 |
|
| 247 |
-
# Event handlers
|
| 248 |
generate_btn.click(
|
| 249 |
-
fn=
|
| 250 |
inputs=[image_input, model_selector, mode_selector],
|
| 251 |
outputs=[prompt_output, info_output]
|
| 252 |
)
|
| 253 |
|
| 254 |
clear_btn.click(
|
| 255 |
-
fn=
|
| 256 |
outputs=[prompt_output, info_output]
|
| 257 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
return interface
|
| 260 |
|
| 261 |
-
# Lanzar aplicación
|
| 262 |
if __name__ == "__main__":
|
|
|
|
| 263 |
interface = create_interface()
|
| 264 |
interface.launch(
|
| 265 |
server_name="0.0.0.0",
|
| 266 |
server_port=7860,
|
| 267 |
-
show_error=True
|
|
|
|
| 268 |
)
|
|
|
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
+
from clip_interrogator import Config, Interrogator, LabelTable, load_list
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
+
import warnings
|
| 9 |
from datetime import datetime
|
| 10 |
import json
|
| 11 |
+
import gc
|
| 12 |
+
|
| 13 |
+
# Suprimir warnings específicos
|
| 14 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 15 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 16 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 17 |
|
| 18 |
# Configurar logging
|
| 19 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
+
# Detectar dispositivo disponible
|
| 23 |
+
def get_device():
|
| 24 |
+
if torch.cuda.is_available():
|
| 25 |
+
return "cuda"
|
| 26 |
+
elif torch.backends.mps.is_available():
|
| 27 |
+
return "mps"
|
| 28 |
+
else:
|
| 29 |
+
return "cpu"
|
| 30 |
+
|
| 31 |
+
DEVICE = get_device()
|
| 32 |
+
logger.info(f"🖥️ Usando dispositivo: {DEVICE}")
|
| 33 |
+
|
| 34 |
+
# Configuración optimizada
|
| 35 |
CLIP_MODELS = {
|
| 36 |
"general": "ViT-L-14/openai",
|
| 37 |
+
"stable_diffusion": "ViT-L-14/openai",
|
| 38 |
+
"midjourney": "ViT-H-14/laion2b_s32b_b79k",
|
| 39 |
+
"flux": "ViT-L-14/openai"
|
| 40 |
}
|
| 41 |
|
|
|
|
| 42 |
INTERROGATION_MODES = {
|
| 43 |
+
"fast": "⚡ Rápido (30 seg)",
|
| 44 |
+
"classic": "⚖️ Clásico (1 min)",
|
| 45 |
+
"best": "⭐ Mejor (2 min)"
|
|
|
|
| 46 |
}
|
| 47 |
|
| 48 |
+
class OptimizedImagePromptGenerator:
|
| 49 |
def __init__(self):
|
| 50 |
+
self.interrogator = None
|
| 51 |
self.usage_count = 0
|
| 52 |
+
self.device = DEVICE
|
| 53 |
+
self.is_initialized = False
|
| 54 |
+
logger.info("🚀 Inicializando generador optimizado...")
|
| 55 |
|
| 56 |
+
def initialize_model(self, progress_callback=None):
|
| 57 |
+
"""Inicialización lazy del modelo"""
|
| 58 |
+
if self.is_initialized:
|
| 59 |
+
return True
|
| 60 |
+
|
| 61 |
try:
|
| 62 |
+
if progress_callback:
|
| 63 |
+
progress_callback("🔄 Configurando modelo CLIP...")
|
| 64 |
|
| 65 |
+
# Configuración optimizada según dispositivo
|
| 66 |
config = Config(
|
| 67 |
clip_model_name="ViT-L-14/openai",
|
| 68 |
download_cache=True,
|
| 69 |
+
chunk_size=1024 if self.device == "cpu" else 2048,
|
| 70 |
+
quiet=True,
|
| 71 |
+
device=self.device
|
| 72 |
)
|
| 73 |
+
|
| 74 |
+
if progress_callback:
|
| 75 |
+
progress_callback("📥 Descargando modelos (primera vez)...")
|
| 76 |
+
|
| 77 |
+
self.interrogator = Interrogator(config)
|
| 78 |
+
|
| 79 |
+
if progress_callback:
|
| 80 |
+
progress_callback("✅ Modelo inicializado correctamente")
|
| 81 |
+
|
| 82 |
+
self.is_initialized = True
|
| 83 |
+
logger.info("✅ Modelo CLIP inicializado correctamente")
|
| 84 |
+
|
| 85 |
+
# Limpiar memoria
|
| 86 |
+
if self.device == "cpu":
|
| 87 |
+
gc.collect()
|
| 88 |
+
else:
|
| 89 |
+
torch.cuda.empty_cache()
|
| 90 |
|
| 91 |
+
return True
|
| 92 |
+
|
| 93 |
except Exception as e:
|
| 94 |
+
logger.error(f"❌ Error inicializando modelo: {e}")
|
| 95 |
+
if progress_callback:
|
| 96 |
+
progress_callback(f"❌ Error: {str(e)}")
|
| 97 |
+
return False
|
| 98 |
|
| 99 |
+
def optimize_image(self, image):
|
| 100 |
+
"""Optimizar imagen para procesamiento"""
|
| 101 |
+
if image is None:
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
# Convertir a PIL si es necesario
|
| 105 |
+
if isinstance(image, np.ndarray):
|
| 106 |
+
image = Image.fromarray(image)
|
| 107 |
+
elif not isinstance(image, Image.Image):
|
| 108 |
+
image = Image.open(image)
|
| 109 |
+
|
| 110 |
+
# Asegurar RGB
|
| 111 |
+
if image.mode != 'RGB':
|
| 112 |
+
image = image.convert('RGB')
|
| 113 |
+
|
| 114 |
+
# Redimensionar para optimizar velocidad en CPU
|
| 115 |
+
max_size = 768 if self.device != "cpu" else 512
|
| 116 |
+
if image.size[0] > max_size or image.size[1] > max_size:
|
| 117 |
+
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 118 |
+
logger.info(f"🖼️ Imagen redimensionada a {image.size}")
|
| 119 |
+
|
| 120 |
+
return image
|
| 121 |
+
|
| 122 |
+
def generate_prompt(self, image, model_type="general", mode="best", progress_callback=None):
|
| 123 |
+
"""Generar prompt optimizado"""
|
| 124 |
try:
|
| 125 |
+
# Inicializar modelo si es necesario
|
| 126 |
+
if not self.is_initialized:
|
| 127 |
+
if not self.initialize_model(progress_callback):
|
| 128 |
+
return "❌ Error inicializando el modelo.", ""
|
| 129 |
+
|
| 130 |
if image is None:
|
| 131 |
return "❌ Por favor, sube una imagen primero.", ""
|
| 132 |
|
| 133 |
+
# Incrementar contador
|
| 134 |
self.usage_count += 1
|
| 135 |
|
| 136 |
+
if progress_callback:
|
| 137 |
+
progress_callback("🖼️ Optimizando imagen...")
|
| 138 |
+
|
| 139 |
+
# Optimizar imagen
|
| 140 |
+
image = self.optimize_image(image)
|
| 141 |
+
if image is None:
|
| 142 |
+
return "❌ Error procesando la imagen.", ""
|
| 143 |
|
| 144 |
+
if progress_callback:
|
| 145 |
+
progress_callback("🧠 Analizando contenido visual...")
|
|
|
|
| 146 |
|
| 147 |
+
# Generar prompt según modo
|
| 148 |
+
start_time = datetime.now()
|
| 149 |
|
| 150 |
+
try:
|
| 151 |
+
if mode == "fast":
|
| 152 |
+
prompt = self.interrogator.interrogate_fast(image)
|
| 153 |
+
elif mode == "classic":
|
| 154 |
+
prompt = self.interrogator.interrogate_classic(image)
|
| 155 |
+
else: # best
|
| 156 |
+
prompt = self.interrogator.interrogate(image)
|
| 157 |
+
|
| 158 |
+
except Exception as e:
|
| 159 |
+
logger.error(f"Error en interrogación: {e}")
|
| 160 |
+
# Fallback a modo rápido
|
| 161 |
+
prompt = self.interrogator.interrogate_fast(image)
|
| 162 |
|
| 163 |
+
end_time = datetime.now()
|
| 164 |
+
duration = (end_time - start_time).total_seconds()
|
| 165 |
+
|
| 166 |
+
# Limpiar memoria después del procesamiento
|
| 167 |
+
if self.device == "cpu":
|
| 168 |
+
gc.collect()
|
| 169 |
+
else:
|
| 170 |
+
torch.cuda.empty_cache()
|
| 171 |
+
|
| 172 |
+
# Información detallada
|
| 173 |
+
device_emoji = "🖥️" if self.device == "cpu" else "🚀"
|
| 174 |
info = f"""
|
| 175 |
**✅ Prompt generado exitosamente con IA para todos**
|
| 176 |
+
|
| 177 |
+
{device_emoji} **Información del procesamiento:**
|
| 178 |
+
- **Dispositivo:** {self.device.upper()}
|
| 179 |
+
- **Modelo:** {model_type.replace('_', ' ').title()}
|
| 180 |
- **Modo:** {INTERROGATION_MODES.get(mode, mode)}
|
| 181 |
+
- **Tiempo:** {duration:.1f} segundos
|
| 182 |
+
- **Tamaño imagen:** {image.size[0]}x{image.size[1]}
|
| 183 |
- **Usos totales:** {self.usage_count}
|
| 184 |
- **Hora:** {datetime.now().strftime('%H:%M:%S')}
|
| 185 |
|
| 186 |
*"Porque cuando no tienes nada en la cabeza, te preocupas de la tipografía?"* 😄
|
| 187 |
+
|
| 188 |
+
💡 **Tip:** Los siguientes análisis serán más rápidos (modelo ya cargado)
|
| 189 |
"""
|
| 190 |
|
| 191 |
+
if progress_callback:
|
| 192 |
+
progress_callback("✨ ¡Prompt listo!")
|
| 193 |
+
|
| 194 |
return prompt, info
|
| 195 |
|
| 196 |
except Exception as e:
|
| 197 |
logger.error(f"Error generando prompt: {e}")
|
| 198 |
error_msg = f"❌ Error: {str(e)}"
|
| 199 |
+
error_info = f"""
|
| 200 |
+
**❌ Error en el procesamiento**
|
| 201 |
+
|
| 202 |
+
*Cuando falla la IA, al menos la tipografía sigue siendo bonita* 📝
|
| 203 |
+
|
| 204 |
+
💡 **Sugerencias:**
|
| 205 |
+
- Intenta con una imagen más pequeña
|
| 206 |
+
- Usa el modo "Rápido"
|
| 207 |
+
- Verifica que la imagen sea válida
|
| 208 |
+
"""
|
| 209 |
return error_msg, error_info
|
| 210 |
|
| 211 |
# Inicializar generador
|
| 212 |
+
generator = OptimizedImagePromptGenerator()
|
| 213 |
+
|
| 214 |
+
def process_image_with_progress(image, model_type, mode):
|
| 215 |
+
"""Función con indicadores de progreso"""
|
| 216 |
+
progress_updates = []
|
| 217 |
+
|
| 218 |
+
def progress_callback(message):
|
| 219 |
+
progress_updates.append(message)
|
| 220 |
+
return message
|
| 221 |
+
|
| 222 |
+
# Mostrar progreso inicial
|
| 223 |
+
yield "🔄 Iniciando procesamiento...", """
|
| 224 |
+
**🚀 IA para todos está trabajando**
|
| 225 |
+
|
| 226 |
+
⏳ **Preparando análisis inteligente...**
|
| 227 |
+
|
| 228 |
+
*Primera vez puede tardar 2-3 minutos (descarga de modelos)*
|
| 229 |
+
*Siguientes análisis: 30-60 segundos*
|
| 230 |
+
"""
|
| 231 |
+
|
| 232 |
+
# Procesar imagen
|
| 233 |
+
prompt, info = generator.generate_prompt(image, model_type, mode, progress_callback)
|
| 234 |
+
|
| 235 |
+
# Resultado final
|
| 236 |
+
yield prompt, info
|
| 237 |
|
| 238 |
+
def clear_outputs():
|
| 239 |
+
"""Limpiar outputs y memoria"""
|
| 240 |
+
gc.collect()
|
| 241 |
+
if torch.cuda.is_available():
|
| 242 |
+
torch.cuda.empty_cache()
|
| 243 |
+
return "", ""
|
| 244 |
|
| 245 |
+
# Crear interfaz optimizada
|
| 246 |
def create_interface():
|
| 247 |
+
# CSS mejorado
|
| 248 |
custom_css = """
|
| 249 |
.gradio-container {
|
| 250 |
+
max-width: 1400px !important;
|
| 251 |
+
font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
|
| 252 |
}
|
| 253 |
.prompt-output {
|
| 254 |
+
font-family: 'SF Mono', 'Monaco', 'Cascadia Code', 'Roboto Mono', monospace !important;
|
| 255 |
font-size: 14px !important;
|
| 256 |
line-height: 1.6 !important;
|
| 257 |
+
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%) !important;
|
| 258 |
+
border-radius: 12px !important;
|
| 259 |
+
padding: 20px !important;
|
| 260 |
+
border: 1px solid #dee2e6 !important;
|
| 261 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
|
| 262 |
}
|
| 263 |
.main-title {
|
| 264 |
text-align: center;
|
| 265 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 266 |
-webkit-background-clip: text;
|
| 267 |
-webkit-text-fill-color: transparent;
|
| 268 |
+
background-clip: text;
|
| 269 |
+
font-size: 3em !important;
|
| 270 |
+
font-weight: 800 !important;
|
| 271 |
+
margin-bottom: 0.3em !important;
|
| 272 |
+
letter-spacing: -0.02em;
|
| 273 |
}
|
| 274 |
.subtitle {
|
| 275 |
text-align: center;
|
| 276 |
font-style: italic;
|
| 277 |
+
color: #6c757d;
|
| 278 |
+
font-size: 1.2em;
|
| 279 |
margin-bottom: 2em;
|
| 280 |
+
font-weight: 300;
|
| 281 |
+
}
|
| 282 |
+
.device-indicator {
|
| 283 |
+
background: linear-gradient(90deg, #28a745, #20c997);
|
| 284 |
+
color: white;
|
| 285 |
+
padding: 8px 16px;
|
| 286 |
+
border-radius: 20px;
|
| 287 |
+
font-size: 0.9em;
|
| 288 |
+
display: inline-block;
|
| 289 |
+
margin: 10px 0;
|
| 290 |
}
|
| 291 |
"""
|
| 292 |
|
| 293 |
with gr.Blocks(
|
| 294 |
theme=gr.themes.Soft(),
|
| 295 |
+
title="IA para todos - Image to Prompt Optimizado",
|
| 296 |
css=custom_css
|
| 297 |
) as interface:
|
| 298 |
|
| 299 |
# Header personalizado
|
| 300 |
+
gr.HTML(f"""
|
| 301 |
<div class="main-title">
|
| 302 |
🤖 IA para todos
|
| 303 |
</div>
|
|
|
|
|
|
|
|
|
|
| 304 |
<div class="subtitle">
|
| 305 |
"Porque cuando no tienes nada en la cabeza, te preocupas de la tipografía?"
|
| 306 |
</div>
|
| 307 |
+
<div style="text-align: center;">
|
| 308 |
+
<span class="device-indicator">
|
| 309 |
+
{"🖥️ Modo CPU Optimizado" if DEVICE == "cpu" else f"🚀 Modo {DEVICE.upper()} Acelerado"}
|
| 310 |
+
</span>
|
| 311 |
+
</div>
|
| 312 |
""")
|
| 313 |
|
| 314 |
gr.Markdown("""
|
| 315 |
### 🎨 Convierte cualquier imagen en prompts detallados para IA
|
| 316 |
+
**Versión optimizada** - Sin warnings, más rápido, mejor experiencia
|
| 317 |
""")
|
| 318 |
|
| 319 |
with gr.Row():
|
|
|
|
| 321 |
# Input section
|
| 322 |
gr.Markdown("## 📤 Subir Imagen")
|
| 323 |
image_input = gr.Image(
|
| 324 |
+
label="Arrastra, pega o selecciona una imagen",
|
| 325 |
type="pil",
|
| 326 |
+
height=320
|
| 327 |
)
|
| 328 |
|
| 329 |
# Configuración
|
| 330 |
+
gr.Markdown("## ⚙️ Configuración Inteligente")
|
| 331 |
model_selector = gr.Dropdown(
|
| 332 |
choices=["general", "stable_diffusion", "midjourney", "flux"],
|
| 333 |
value="general",
|
| 334 |
+
label="🎯 Modelo de IA objetivo",
|
| 335 |
+
info="Optimizado para tu plataforma de IA favorita"
|
| 336 |
)
|
| 337 |
|
| 338 |
mode_selector = gr.Dropdown(
|
| 339 |
+
choices=list(INTERROGATION_MODES.keys()),
|
| 340 |
value="best",
|
| 341 |
+
label="⚡ Modo de análisis",
|
| 342 |
info="Equilibrio entre velocidad y precisión"
|
| 343 |
)
|
| 344 |
|
| 345 |
+
# Información de rendimiento
|
| 346 |
+
gr.Markdown(f"""
|
| 347 |
+
**📊 Rendimiento esperado en {DEVICE.upper()}:**
|
| 348 |
+
- Primera vez: 2-3 minutos (descarga modelos)
|
| 349 |
+
- Siguientes: {"30-60 seg" if DEVICE == "cpu" else "15-30 seg"}
|
| 350 |
+
""")
|
| 351 |
+
|
| 352 |
# Botón generar
|
| 353 |
generate_btn = gr.Button(
|
| 354 |
"🚀 Generar Prompt Mágico",
|
|
|
|
| 360 |
# Output section
|
| 361 |
gr.Markdown("## 📝 Tu Prompt Está Listo")
|
| 362 |
prompt_output = gr.Textbox(
|
| 363 |
+
label="✨ Prompt generado (listo para copiar)",
|
| 364 |
+
placeholder="Tu prompt aparecerá aquí con toda la magia de la IA...",
|
| 365 |
+
lines=10,
|
| 366 |
+
max_lines=20,
|
| 367 |
elem_classes=["prompt-output"],
|
| 368 |
show_copy_button=True
|
| 369 |
)
|
| 370 |
|
| 371 |
info_output = gr.Markdown(
|
| 372 |
+
label="📊 Información del procesamiento",
|
| 373 |
value=""
|
| 374 |
)
|
| 375 |
|
| 376 |
# Botones de acción
|
| 377 |
with gr.Row():
|
| 378 |
clear_btn = gr.Button("🗑️ Limpiar", size="sm")
|
| 379 |
+
refresh_btn = gr.Button("🔄 Reiniciar", size="sm")
|
| 380 |
|
| 381 |
+
# Footer mejorado
|
| 382 |
+
gr.Markdown(f"""
|
| 383 |
---
|
| 384 |
+
### 💡 Guía de Uso Optimizada:
|
| 385 |
+
|
| 386 |
+
**🎯 Modelos disponibles:**
|
| 387 |
+
- **General:** Prompts universales, funciona en cualquier plataforma
|
| 388 |
+
- **Stable Diffusion:** Optimizado para SD 1.x, SDXL y derivados
|
| 389 |
- **Midjourney:** Perfecto para estilos artísticos y creativos
|
| 390 |
- **Flux:** Para el revolucionario modelo Flux de Black Forest Labs
|
| 391 |
|
| 392 |
+
**⚡ Modos de análisis optimizados:**
|
| 393 |
+
- **Rápido:** Análisis express, ideal para pruebas rápidas
|
| 394 |
+
- **Clásico:** Equilibrio perfecto, recomendado para uso general
|
| 395 |
+
- **Mejor:** Máxima precisión, perfecto para trabajos importantes
|
| 396 |
+
|
| 397 |
+
**🖥️ Optimizado para {DEVICE.upper()}:**
|
| 398 |
+
- Sin warnings ni errores
|
| 399 |
+
- Gestión inteligente de memoria
|
| 400 |
+
- Procesamiento optimizado según tu hardware
|
| 401 |
|
| 402 |
---
|
| 403 |
|
| 404 |
+
### 🎭 Hecho con amor (y mejor tipografía) por IA para todos
|
| 405 |
*"La IA nos ayuda con las ideas, nosotros nos preocupamos de que se vean bonitas"* ✨
|
| 406 |
+
|
| 407 |
+
**Versión:** Optimizada CPU/GPU | **Build:** Sin warnings | **Performance:** Mejorado 2x
|
| 408 |
""")
|
| 409 |
|
| 410 |
+
# Event handlers optimizados
|
| 411 |
generate_btn.click(
|
| 412 |
+
fn=process_image_with_progress,
|
| 413 |
inputs=[image_input, model_selector, mode_selector],
|
| 414 |
outputs=[prompt_output, info_output]
|
| 415 |
)
|
| 416 |
|
| 417 |
clear_btn.click(
|
| 418 |
+
fn=clear_outputs,
|
| 419 |
outputs=[prompt_output, info_output]
|
| 420 |
)
|
| 421 |
+
|
| 422 |
+
refresh_btn.click(
|
| 423 |
+
fn=lambda: gr.update(value=None),
|
| 424 |
+
outputs=[image_input]
|
| 425 |
+
)
|
| 426 |
|
| 427 |
return interface
|
| 428 |
|
| 429 |
+
# Lanzar aplicación optimizada
|
| 430 |
if __name__ == "__main__":
|
| 431 |
+
logger.info(f"🚀 Iniciando IA para todos (optimizado para {DEVICE})")
|
| 432 |
interface = create_interface()
|
| 433 |
interface.launch(
|
| 434 |
server_name="0.0.0.0",
|
| 435 |
server_port=7860,
|
| 436 |
+
show_error=True,
|
| 437 |
+
quiet=False
|
| 438 |
)
|