Add 6 new high-quality models and improve FLUX authentication - Add OpenJourney v4, Anything v3, Waifu Diffusion, Deliberate v2, Dreamlike Diffusion - Improve HF_TOKEN detection (supports both HF_TOKEN and HUGGING_FACE_HUB_TOKEN) - Add conditional FLUX models loading (only if token is available) - Add specific configurations for each new model - Improve logging and error handling - Update README with new models and better documentation
Browse files
README.md
CHANGED
|
@@ -30,6 +30,13 @@ Un generador de imágenes con IA con múltiples modelos de alta calidad optimiza
|
|
| 30 |
- **SDXL Base** - Modelo base de Stable Diffusion XL
|
| 31 |
- **Realistic Vision** - Especializado en retratos realistas
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
## 🔐 Configuración para Modelos Gated (FLUX)
|
| 34 |
|
| 35 |
Para usar los modelos FLUX, necesitas configurar tu token de Hugging Face:
|
|
@@ -72,18 +79,38 @@ Para usar los modelos FLUX, necesitas configurar tu token de Hugging Face:
|
|
| 72 |
- **Configuraciones específicas**: Cada modelo tiene sus propios parámetros de guidance y steps
|
| 73 |
- **API Externa**: FLUX.1-Kontext-Dev usa la API del Space oficial
|
| 74 |
- **Autenticación automática**: Manejo automático de tokens para modelos gated
|
|
|
|
| 75 |
|
| 76 |
## 🔧 Configuraciones por Modelo
|
| 77 |
|
|
|
|
| 78 |
- **Turbo/Lightning**: guidance_scale=0.0, steps=1-4
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
- **FLUX.1-Kontext-Dev**: guidance_scale=2.5, steps=28 (via API)
|
|
|
|
|
|
|
| 81 |
- **Realistic Vision**: guidance_scale=7.5, steps=25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
## 🌐 Modelo Externo
|
| 84 |
|
| 85 |
**FLUX.1-Kontext-Dev** se conecta a la API oficial del Space `black-forest-labs/FLUX.1-Kontext-Dev` para generar imágenes de alta calidad. Este modelo requiere conexión a internet y puede tener tiempos de respuesta variables.
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
## 🐛 Solución de Problemas
|
| 88 |
|
| 89 |
### Error de Autenticación
|
|
@@ -91,12 +118,18 @@ Si ves errores como "Cannot access gated repo":
|
|
| 91 |
1. Verifica que el `HF_TOKEN` esté configurado correctamente
|
| 92 |
2. Asegúrate de tener acceso a los modelos FLUX
|
| 93 |
3. Reinicia el Space después de configurar el token
|
|
|
|
| 94 |
|
| 95 |
### Error de API Externa
|
| 96 |
Si la API externa falla:
|
| 97 |
- El modelo FLUX.1-Kontext-Dev mostrará una imagen roja de error
|
| 98 |
- Usa los modelos locales como alternativa
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
---
|
| 101 |
|
| 102 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 30 |
- **SDXL Base** - Modelo base de Stable Diffusion XL
|
| 31 |
- **Realistic Vision** - Especializado en retratos realistas
|
| 32 |
|
| 33 |
+
### Modelos Especializados 🎯
|
| 34 |
+
- **OpenJourney v4** - Especializado en arte Midjourney-style
|
| 35 |
+
- **Anything v3** - Modelo versátil para cualquier estilo
|
| 36 |
+
- **Waifu Diffusion** - Especializado en anime y manga
|
| 37 |
+
- **Deliberate v2** - Modelo de alta calidad para arte digital
|
| 38 |
+
- **Dreamlike Diffusion** - Especializado en arte surrealista y onírico
|
| 39 |
+
|
| 40 |
## 🔐 Configuración para Modelos Gated (FLUX)
|
| 41 |
|
| 42 |
Para usar los modelos FLUX, necesitas configurar tu token de Hugging Face:
|
|
|
|
| 79 |
- **Configuraciones específicas**: Cada modelo tiene sus propios parámetros de guidance y steps
|
| 80 |
- **API Externa**: FLUX.1-Kontext-Dev usa la API del Space oficial
|
| 81 |
- **Autenticación automática**: Manejo automático de tokens para modelos gated
|
| 82 |
+
- **Detección inteligente**: Los modelos FLUX solo aparecen si hay token configurado
|
| 83 |
|
| 84 |
## 🔧 Configuraciones por Modelo
|
| 85 |
|
| 86 |
+
### Modelos Rápidos
|
| 87 |
- **Turbo/Lightning**: guidance_scale=0.0, steps=1-4
|
| 88 |
+
|
| 89 |
+
### Modelos FLUX
|
| 90 |
+
- **FLUX.1-dev**: guidance_scale=7.5, steps=20
|
| 91 |
+
- **FLUX.1-schnell**: guidance_scale=7.5, steps=20
|
| 92 |
- **FLUX.1-Kontext-Dev**: guidance_scale=2.5, steps=28 (via API)
|
| 93 |
+
|
| 94 |
+
### Modelos Especializados
|
| 95 |
- **Realistic Vision**: guidance_scale=7.5, steps=25
|
| 96 |
+
- **OpenJourney v4**: guidance_scale=7.5, steps=20
|
| 97 |
+
- **Anything v3**: guidance_scale=7.5, steps=20
|
| 98 |
+
- **Waifu Diffusion**: guidance_scale=7.5, steps=20
|
| 99 |
+
- **Deliberate v2**: guidance_scale=7.5, steps=20
|
| 100 |
+
- **Dreamlike Diffusion**: guidance_scale=7.5, steps=20
|
| 101 |
|
| 102 |
## 🌐 Modelo Externo
|
| 103 |
|
| 104 |
**FLUX.1-Kontext-Dev** se conecta a la API oficial del Space `black-forest-labs/FLUX.1-Kontext-Dev` para generar imágenes de alta calidad. Este modelo requiere conexión a internet y puede tener tiempos de respuesta variables.
|
| 105 |
|
| 106 |
+
## 🎨 Ejemplos de Prompts
|
| 107 |
+
|
| 108 |
+
- **Anime**: "Anime girl with blue hair, detailed, high quality"
|
| 109 |
+
- **Cyberpunk**: "Cyberpunk city at night, neon lights, detailed, 8k"
|
| 110 |
+
- **Fantasy**: "Fantasy landscape with mountains and dragons, epic, detailed"
|
| 111 |
+
- **Realistic**: "Portrait of a beautiful woman, realistic, high quality, detailed"
|
| 112 |
+
- **Sci-fi**: "Futuristic AI assistant in a glowing galaxy, neon lights, sci-fi style, cinematic"
|
| 113 |
+
|
| 114 |
## 🐛 Solución de Problemas
|
| 115 |
|
| 116 |
### Error de Autenticación
|
|
|
|
| 118 |
1. Verifica que el `HF_TOKEN` esté configurado correctamente
|
| 119 |
2. Asegúrate de tener acceso a los modelos FLUX
|
| 120 |
3. Reinicia el Space después de configurar el token
|
| 121 |
+
4. Los modelos FLUX solo aparecen en el dropdown si hay token configurado
|
| 122 |
|
| 123 |
### Error de API Externa
|
| 124 |
Si la API externa falla:
|
| 125 |
- El modelo FLUX.1-Kontext-Dev mostrará una imagen roja de error
|
| 126 |
- Usa los modelos locales como alternativa
|
| 127 |
|
| 128 |
+
### Modelos No Disponibles
|
| 129 |
+
Si no ves los modelos FLUX en el dropdown:
|
| 130 |
+
- Verifica que el `HF_TOKEN` esté configurado
|
| 131 |
+
- Los modelos gated solo aparecen con autenticación válida
|
| 132 |
+
|
| 133 |
---
|
| 134 |
|
| 135 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -14,11 +14,12 @@ import torch
|
|
| 14 |
from huggingface_hub import login
|
| 15 |
|
| 16 |
# Configurar autenticación con Hugging Face
|
| 17 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 18 |
if HF_TOKEN:
|
| 19 |
try:
|
| 20 |
login(token=HF_TOKEN)
|
| 21 |
print("✅ Autenticado con Hugging Face")
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
print(f"⚠️ Error de autenticación: {e}")
|
| 24 |
else:
|
|
@@ -30,16 +31,30 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 30 |
# Modelos disponibles de alta calidad
|
| 31 |
MODELS = {
|
| 32 |
"SDXL Turbo (stabilityai/sdxl-turbo)": "stabilityai/sdxl-turbo",
|
| 33 |
-
"FLUX.1-dev (black-forest-labs/FLUX.1-dev)": "black-forest-labs/FLUX.1-dev",
|
| 34 |
-
"FLUX.1-schnell (black-forest-labs/FLUX.1-schnell)": "black-forest-labs/FLUX.1-schnell",
|
| 35 |
-
"FLUX.1-Kontext-Dev (API External)": "api_external",
|
| 36 |
"SDXL Lightning (ByteDance/SDXL-Lightning)": "ByteDance/SDXL-Lightning",
|
| 37 |
"SDXL Lightning 4Step (ByteDance/SDXL-Lightning-4Step)": "ByteDance/SDXL-Lightning-4Step",
|
| 38 |
"SD Turbo (stabilityai/sd-turbo)": "stabilityai/sd-turbo",
|
| 39 |
"SDXL Base (stabilityai/stable-diffusion-xl-base-1.0)": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 40 |
-
"Realistic Vision (SG161222/Realistic_Vision_V5.1_noVAE)": "SG161222/Realistic_Vision_V5.1_noVAE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Estado del pipeline
|
| 44 |
pipe = None
|
| 45 |
current_model_id = None
|
|
@@ -58,13 +73,15 @@ def load_model(model_id):
|
|
| 58 |
if pipe is None or model_id != current_model_id:
|
| 59 |
try:
|
| 60 |
# Usar token de autenticación si está disponible
|
| 61 |
-
if HF_TOKEN:
|
|
|
|
| 62 |
pipe = DiffusionPipeline.from_pretrained(
|
| 63 |
model_id,
|
| 64 |
torch_dtype=torch_dtype,
|
| 65 |
use_auth_token=HF_TOKEN
|
| 66 |
)
|
| 67 |
else:
|
|
|
|
| 68 |
pipe = DiffusionPipeline.from_pretrained(
|
| 69 |
model_id,
|
| 70 |
torch_dtype=torch_dtype
|
|
@@ -79,6 +96,7 @@ def load_model(model_id):
|
|
| 79 |
# Función para usar la API externa de FLUX.1-Kontext-Dev
|
| 80 |
def use_external_api(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 81 |
try:
|
|
|
|
| 82 |
# URL de la API del Space externo
|
| 83 |
api_url = "https://black-forest-labs-flux-1-kontext-dev.hf.space/api/predict/"
|
| 84 |
|
|
@@ -113,12 +131,13 @@ def use_external_api(prompt, negative_prompt, seed, randomize_seed, width, heigh
|
|
| 113 |
image_bytes = base64.b64decode(image_data.split(',')[1])
|
| 114 |
image = Image.open(io.BytesIO(image_bytes))
|
| 115 |
|
|
|
|
| 116 |
return image, new_seed
|
| 117 |
else:
|
| 118 |
raise Exception(f"API Error: {response.status_code} - {response.text}")
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
-
print(f"Error usando API externa: {e}")
|
| 122 |
# Fallback: crear una imagen de error
|
| 123 |
error_image = Image.new('RGB', (width, height), color='red')
|
| 124 |
return error_image, seed
|
|
@@ -163,6 +182,26 @@ def infer(
|
|
| 163 |
# Realistic Vision
|
| 164 |
guidance_scale = 7.5
|
| 165 |
num_inference_steps = 25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
image = pipe(
|
| 168 |
prompt=prompt,
|
|
@@ -177,7 +216,7 @@ def infer(
|
|
| 177 |
return image, seed
|
| 178 |
|
| 179 |
except Exception as e:
|
| 180 |
-
print(f"Error en inferencia: {e}")
|
| 181 |
# Crear imagen de error
|
| 182 |
error_image = Image.new('RGB', (width, height), color='red')
|
| 183 |
return error_image, seed
|
|
@@ -189,6 +228,9 @@ examples = [
|
|
| 189 |
"A delicious ceviche cheesecake slice",
|
| 190 |
"Futuristic AI assistant in a glowing galaxy, neon lights, sci-fi style, cinematic",
|
| 191 |
"Portrait of a beautiful woman, realistic, high quality, detailed",
|
|
|
|
|
|
|
|
|
|
| 192 |
]
|
| 193 |
|
| 194 |
css = """
|
|
@@ -220,7 +262,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 220 |
choices=list(MODELS.keys()),
|
| 221 |
value=list(MODELS.keys())[0],
|
| 222 |
label="Model",
|
| 223 |
-
info="Select a high-quality model (FLUX
|
| 224 |
)
|
| 225 |
|
| 226 |
negative_prompt = gr.Text(
|
|
|
|
| 14 |
from huggingface_hub import login
|
| 15 |
|
| 16 |
# Configurar autenticación con Hugging Face
|
| 17 |
+
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
|
| 18 |
if HF_TOKEN:
|
| 19 |
try:
|
| 20 |
login(token=HF_TOKEN)
|
| 21 |
print("✅ Autenticado con Hugging Face")
|
| 22 |
+
print(f"🔑 Token configurado: {HF_TOKEN[:10]}...")
|
| 23 |
except Exception as e:
|
| 24 |
print(f"⚠️ Error de autenticación: {e}")
|
| 25 |
else:
|
|
|
|
| 31 |
# Modelos disponibles de alta calidad
|
| 32 |
MODELS = {
|
| 33 |
"SDXL Turbo (stabilityai/sdxl-turbo)": "stabilityai/sdxl-turbo",
|
|
|
|
|
|
|
|
|
|
| 34 |
"SDXL Lightning (ByteDance/SDXL-Lightning)": "ByteDance/SDXL-Lightning",
|
| 35 |
"SDXL Lightning 4Step (ByteDance/SDXL-Lightning-4Step)": "ByteDance/SDXL-Lightning-4Step",
|
| 36 |
"SD Turbo (stabilityai/sd-turbo)": "stabilityai/sd-turbo",
|
| 37 |
"SDXL Base (stabilityai/stable-diffusion-xl-base-1.0)": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 38 |
+
"Realistic Vision (SG161222/Realistic_Vision_V5.1_noVAE)": "SG161222/Realistic_Vision_V5.1_noVAE",
|
| 39 |
+
"OpenJourney v4 (prompthero/openjourney-v4)": "prompthero/openjourney-v4",
|
| 40 |
+
"Anything v3 (Linaqruf/anything-v3.0)": "Linaqruf/anything-v3.0",
|
| 41 |
+
"Waifu Diffusion (hakurei/waifu-diffusion)": "hakurei/waifu-diffusion",
|
| 42 |
+
"Deliberate v2 (XpucT/deliberate-v2)": "XpucT/deliberate-v2",
|
| 43 |
+
"Dreamlike Diffusion (dreamlike-art/dreamlike-diffusion-1.0)": "dreamlike-art/dreamlike-diffusion-1.0",
|
| 44 |
+
"FLUX.1-Kontext-Dev (API External)": "api_external",
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# Modelos FLUX (solo si hay token)
|
| 48 |
+
if HF_TOKEN:
|
| 49 |
+
FLUX_MODELS = {
|
| 50 |
+
"FLUX.1-dev (black-forest-labs/FLUX.1-dev)": "black-forest-labs/FLUX.1-dev",
|
| 51 |
+
"FLUX.1-schnell (black-forest-labs/FLUX.1-schnell)": "black-forest-labs/FLUX.1-schnell",
|
| 52 |
+
}
|
| 53 |
+
MODELS.update(FLUX_MODELS)
|
| 54 |
+
print("🔓 Modelos FLUX habilitados con autenticación")
|
| 55 |
+
else:
|
| 56 |
+
print("🔒 Modelos FLUX deshabilitados - requiere HF_TOKEN")
|
| 57 |
+
|
| 58 |
# Estado del pipeline
|
| 59 |
pipe = None
|
| 60 |
current_model_id = None
|
|
|
|
| 73 |
if pipe is None or model_id != current_model_id:
|
| 74 |
try:
|
| 75 |
# Usar token de autenticación si está disponible
|
| 76 |
+
if HF_TOKEN and ("flux" in model_id.lower() or "black-forest" in model_id.lower()):
|
| 77 |
+
print(f"🔐 Cargando modelo gated: {model_id}")
|
| 78 |
pipe = DiffusionPipeline.from_pretrained(
|
| 79 |
model_id,
|
| 80 |
torch_dtype=torch_dtype,
|
| 81 |
use_auth_token=HF_TOKEN
|
| 82 |
)
|
| 83 |
else:
|
| 84 |
+
print(f"📦 Cargando modelo público: {model_id}")
|
| 85 |
pipe = DiffusionPipeline.from_pretrained(
|
| 86 |
model_id,
|
| 87 |
torch_dtype=torch_dtype
|
|
|
|
| 96 |
# Función para usar la API externa de FLUX.1-Kontext-Dev
|
| 97 |
def use_external_api(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 98 |
try:
|
| 99 |
+
print("🌐 Conectando a API externa FLUX.1-Kontext-Dev...")
|
| 100 |
# URL de la API del Space externo
|
| 101 |
api_url = "https://black-forest-labs-flux-1-kontext-dev.hf.space/api/predict/"
|
| 102 |
|
|
|
|
| 131 |
image_bytes = base64.b64decode(image_data.split(',')[1])
|
| 132 |
image = Image.open(io.BytesIO(image_bytes))
|
| 133 |
|
| 134 |
+
print("✅ API externa exitosa")
|
| 135 |
return image, new_seed
|
| 136 |
else:
|
| 137 |
raise Exception(f"API Error: {response.status_code} - {response.text}")
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
+
print(f"❌ Error usando API externa: {e}")
|
| 141 |
# Fallback: crear una imagen de error
|
| 142 |
error_image = Image.new('RGB', (width, height), color='red')
|
| 143 |
return error_image, seed
|
|
|
|
| 182 |
# Realistic Vision
|
| 183 |
guidance_scale = 7.5
|
| 184 |
num_inference_steps = 25
|
| 185 |
+
elif "openjourney" in model_id.lower():
|
| 186 |
+
# OpenJourney
|
| 187 |
+
guidance_scale = 7.5
|
| 188 |
+
num_inference_steps = 20
|
| 189 |
+
elif "anything" in model_id.lower():
|
| 190 |
+
# Anything v3
|
| 191 |
+
guidance_scale = 7.5
|
| 192 |
+
num_inference_steps = 20
|
| 193 |
+
elif "waifu" in model_id.lower():
|
| 194 |
+
# Waifu Diffusion
|
| 195 |
+
guidance_scale = 7.5
|
| 196 |
+
num_inference_steps = 20
|
| 197 |
+
elif "deliberate" in model_id.lower():
|
| 198 |
+
# Deliberate v2
|
| 199 |
+
guidance_scale = 7.5
|
| 200 |
+
num_inference_steps = 20
|
| 201 |
+
elif "dreamlike" in model_id.lower():
|
| 202 |
+
# Dreamlike Diffusion
|
| 203 |
+
guidance_scale = 7.5
|
| 204 |
+
num_inference_steps = 20
|
| 205 |
|
| 206 |
image = pipe(
|
| 207 |
prompt=prompt,
|
|
|
|
| 216 |
return image, seed
|
| 217 |
|
| 218 |
except Exception as e:
|
| 219 |
+
print(f"❌ Error en inferencia: {e}")
|
| 220 |
# Crear imagen de error
|
| 221 |
error_image = Image.new('RGB', (width, height), color='red')
|
| 222 |
return error_image, seed
|
|
|
|
| 228 |
"A delicious ceviche cheesecake slice",
|
| 229 |
"Futuristic AI assistant in a glowing galaxy, neon lights, sci-fi style, cinematic",
|
| 230 |
"Portrait of a beautiful woman, realistic, high quality, detailed",
|
| 231 |
+
"Anime girl with blue hair, detailed, high quality",
|
| 232 |
+
"Cyberpunk city at night, neon lights, detailed, 8k",
|
| 233 |
+
"Fantasy landscape with mountains and dragons, epic, detailed",
|
| 234 |
]
|
| 235 |
|
| 236 |
css = """
|
|
|
|
| 262 |
choices=list(MODELS.keys()),
|
| 263 |
value=list(MODELS.keys())[0],
|
| 264 |
label="Model",
|
| 265 |
+
info="Select a high-quality model (FLUX models require HF_TOKEN)"
|
| 266 |
)
|
| 267 |
|
| 268 |
negative_prompt = gr.Text(
|