import os
# Descargas grandes (modelo base ~51GB): usar hf_transfer (multi-conexión, resumible)
# y subir el timeout de lectura para tolerar cortes de red transitorios.
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
os.environ.setdefault("HF_HUB_DOWNLOAD_TIMEOUT", "120")
MOCK = False #not os.environ.get("HF_SPACE_ID")
if (MOCK):
import sys
from unittest.mock import MagicMock
# Creamos un módulo falso llamado 'spaces'
mock_spaces = MagicMock()
# Definimos el decorador GPU para que simplemente devuelva la función original sin cambios
def mock_gpu_decorator(duration=None):
def decorator(func):
return func
return decorator
mock_spaces.GPU = mock_gpu_decorator
# Lo insertamos en los módulos del sistema para que 'import spaces' funcione
sys.modules["spaces"] = mock_spaces
import gradio as gr
import json
import logging
from PIL import Image
import spaces
import copy
import random
import time
import re
import math
import numpy as np
import traceback
import tempfile
from prompt_rewrite import rewrite
from gradio_client import Client, handle_file
import hashlib
from functools import partial
if (not MOCK):
import torch
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
NUM_LORAS = 4
base_model = "Qwen/Qwen-Image-2512"
def apply_aspect_ratio(ratio):
sizes = {
"1:1": (1024, 1024),
"16:9": (1365, 768),
"9:16": (768, 1365),
"3:2": (1254, 836),
"2:3": (836, 1254),
"3:1": (1774, 591),
"2:1": (1448, 724),
}
return sizes.get(ratio, (1024, 1024))
DEFAULT_ASPECT_RATIO = "16:9"
# ✅ NUEVO: importar optimización avanzada tipo Qwen-Image-MultipleAngles
#from optimization import optimize_pipeline_
LORAS_CACHE = {
"data": [],
"last_hash": None,
}
def load_loras_hot():
if MOCK:
return load_loras_from_file()
"""Load loras.json and detect changes."""
path = hf_hub_download(
repo_id="lichorosario/qwen-image-lora-dlc-v3",
filename="loras.json",
repo_type="space",
)
with open(path, "r", encoding="utf-8") as f:
raw = f.read()
current_hash = hashlib.sha256(raw.encode("utf-8")).hexdigest()
if current_hash != LORAS_CACHE["last_hash"]:
LORAS_CACHE["data"] = json.loads(raw)
LORAS_CACHE["last_hash"] = current_hash
print("🔁 LoRA config updated")
return LORAS_CACHE["data"]
# Load LoRAs from JSON file
def load_loras_from_file():
"""Load LoRA configurations from external JSON file."""
try:
with open('loras.json', 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
print("Warning: loras.json file not found. Using empty list.")
return []
except json.JSONDecodeError as e:
print(f"Error parsing loras.json: {e}")
return []
# Load the LoRAs
#loras = load_loras_from_file()
loras = load_loras_hot()
# loras = load_loras_hot()
loras = load_loras_hot()
selected_loras = []
if not MOCK:
# Initialize the base model
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
# Scheduler configuration from the Qwen-Image-Lightning repository
scheduler_config = {
"base_image_seq_len": 256,
"base_shift": math.log(3),
"invert_sigmas": False,
"max_image_seq_len": 8192,
"max_shift": math.log(3),
"num_train_timesteps": 1000,
"shift": 1.0,
"shift_terminal": None,
"stochastic_sampling": False,
"time_shift_type": "exponential",
"use_beta_sigmas": False,
"use_dynamic_shifting": True,
"use_exponential_sigmas": False,
"use_karras_sigmas": False,
}
if not MOCK:
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
pipe = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image-2512", scheduler=scheduler, torch_dtype=dtype
).to(device)
# Lightning LoRA info (no global state)
LIGHTNING_LORA_REPO = "lightx2v/Qwen-Image-2512-Lightning"
LIGHTNING_LORA_WEIGHT = "Qwen-Image-2512-Lightning-4steps-V1.0-fp32.safetensors"
LIGHTNING8_LORA_WEIGHT = "Qwen-Image-2512-Lightning-8steps-V1.0-fp32.safetensors"
LIGHTNING_FP8_4STEPS_LORA_WEIGHT = "Qwen-Image-fp8-e4m3fn-Lightning-4steps-V1.0-bf16.safetensors"
WULI_LORA_REPO = "Wuli-art/Qwen-Image-2512-Turbo-LoRA"
WULI_LORA_WEIGHT = "Wuli-Qwen-Image-2512-Turbo-LoRA-4steps-V3.0-bf16.safetensors"
WULI2STP_LORA_REPO = "Wuli-art/Qwen-Image-2512-Turbo-LoRA-2-Steps"
WULI2STP_LORA_WEIGHT = "Wuli-Qwen-Image-2512-Turbo-LoRA-2steps-V1.0-bf16.safetensors"
MAX_SEED = np.iinfo(np.int32).max
### MODIFICACIÓN 1: AÑADIR FUNCIONES PARA GESTIONAR EL HISTORIAL ###
def update_history(new_images, history):
"""Añade las nuevas imágenes generadas al principio de la lista del historial."""
if history is None:
history = []
if new_images is not None and len(new_images) > 0:
updated_history = new_images + history
return updated_history[:24]
return history
def clear_history():
"""Devuelve una lista vacía para limpiar la galería de historial."""
return []
PID_UPSCALER_SPACE = "prithivMLmods/PiD-Image-Upscaler"
def select_history_image(history, evt: gr.SelectData):
"""Guarda la imagen del historial que se acaba de clickear."""
if not history or evt.index is None or evt.index >= len(history):
return None
return history[evt.index][0]
def upscale_history_image(image, oauth_token: gr.OAuthToken | None = None):
"""Escala 4x la imagen elegida del historial llamando a la API del space
prithivMLmods/PiD-Image-Upscaler: el modelo corre en el hardware de ese
space, no en el nuestro. Con el token OAuth del visitante la cuota ZeroGPU
de esa llamada se le factura a él; sin login sale anónima."""
if image is None:
raise gr.Error("Click an image in the History gallery first.")
input_path = os.path.join(tempfile.mkdtemp(), "upscale_input.png")
image.save(input_path)
try:
client = Client(PID_UPSCALER_SPACE, hf_token=getattr(oauth_token, "token", None))
_, slider = client.predict(handle_file(input_path), "", api_name="/upscaler_run")
except Exception as e:
raise gr.Error(f"PiD upscaler failed: {e}")
# El endpoint remoto devuelve el gr.update(...) sin resolver: un dict
# {"value": [original, upscalada]}, no la tupla ya lista.
original, upscaled = slider["value"]
return gr.update(visible=True, value=(original, upscaled))
### FIN DE LA MODIFICACIÓN 1 ###
class calculateDuration:
def __init__(self, activity_name=""):
self.activity_name = activity_name
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.end_time = time.time()
self.elapsed_time = self.end_time - self.start_time
if self.activity_name:
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
else:
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
def update_selection(evt: gr.SelectData, width, height):
selected_lora = loras[evt.index]
versions = selected_lora.get("versions", [])
if versions:
# Use first version or logic to pick one? Logic in get_selection handles the "active" one.
# This function updates just the placeholder and info text, might not be fully used anymore or overlapping logic.
# We will keep it but maybe it's not the primary update mechanism for state.
pass
new_placeholder = f"Type a prompt for {selected_lora['title']}"
lora_repo = selected_lora["repo"]
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
examples_list = []
try:
model_card = ModelCard.load(lora_repo)
widget_data = model_card.data.get("widget", [])
if widget_data and len(widget_data) > 0:
for example in widget_data[:4]:
if "output" in example and "url" in example["output"]:
image_url = f"https://huggingface.co/{lora_repo}/resolve/main/{example['output']['url']}"
prompt_text = example.get("text", "")
examples_list.append([prompt_text])
except Exception as e:
print(f"Could not load model card for {lora_repo}: {e}")
return (
gr.update(placeholder=new_placeholder),
updated_text,
evt.index,
width,
width,
height
)
def update_version_selection(version_title, current_state, idx):
"""
Handle version dropdown change.
Update state and the specific Markdown to show the version's image.
"""
# current_state is list of tuples: (image_index, version_index, scale)
# idx is the column index (0-based)
new_state = [tuple(item) for item in current_state]
current_image_index = new_state[idx][0]
if current_image_index is None:
return gr.update(), new_state # Should not happen if logic is correct
# Handle legacy state (2 elements) or new state (3 elements)
if len(new_state[idx]) == 3:
current_image_index, current_version_index, current_scale = new_state[idx]
else:
current_image_index, current_scale = new_state[idx]
current_version_index = None
lora = loras[current_image_index]
versions = lora.get("versions", [])
# Find selected version object
selected_version = next((v for i, v in enumerate(versions) if v["title"] == version_title), None)
selected_version_index = next((i for i, v in enumerate(versions) if v["title"] == version_title), None)
if selected_version:
# Update Markdown image
lora_name = lora["title"]
version_image = selected_version.get("image", lora.get("image"))
markdown_content = f"\n\n**{lora_name}** ({version_title})"
# Update state: (image_index, version_index, scale)
new_state[idx] = (current_image_index, selected_version_index, current_scale)
return gr.update(value=markdown_content), new_state
return gr.update(), new_state
def handle_speed_mode(speed_mode):
"""Update UI based on speed/quality toggle."""
if speed_mode == "light 4":
return gr.update(value="Light mode (4 steps) selected"), 4, 1.0
elif speed_mode == "light 4 fp8":
return gr.update(value="Light mode (4 steps fp8) selected"), 4, 1.0
elif speed_mode == "light 8":
return gr.update(value="Light mode (8 steps) selected"), 8, 1.0
elif speed_mode == "Wuli-art":
return gr.update(value="Light mode (4 steps) Wuli-art selected"), 4, 1.0
elif speed_mode == "Wuli2Step-art":
return gr.update(value="Light mode (2 steps) Wuli-art selected"), 2, 1.0
else:
return gr.update(value="Normal quality (45 steps) selected"), 30, 3.5
def gpu_duration(*args, **kwargs):
"""Presupuesto de GPU dinámico: pasos × imágenes × resolución, con margen.
La cuota se cobra por tiempo real usado; esto solo ajusta la reserva
(mejor prioridad de cola y menos daño si un task cuelga). Ante cualquier
duda cae al default de 120s.
"""
try:
prompts = [p for p in args[0:4] if p and str(p).strip()]
n_prompts = max(1, len(prompts))
steps = int(args[6])
width, height = int(args[10]), int(args[11])
mult = float(str(args[13]).replace("x", ""))
real_quantity = int(args[14]) + 1
n_images = n_prompts * real_quantity
pixels_factor = max(1.0, (width * mult * height * mult) / (1024 * 1024))
per_image = 3.0 * steps * pixels_factor
return int(min(900, 60 + per_image * n_images))
except Exception:
return 120
@spaces.GPU(duration=gpu_duration, size="xlarge")
def run_lora_multi(
prompt_1, prompt_2, prompt_3, prompt_4,
negative_prompt,
cfg_scale, steps,
selected_loras_state, # Changed from selected_index
randomize_seed, seed,
width, height,
speed_mode,
quality_multiplier,
quantity,
history,
prompt_enhance=False,
progress=gr.Progress(track_tqdm=True)
):
# selected_loras_state is a list of tuples: [(image_index, version_index, scale), ...]
if selected_loras_state is None:
selected_loras_state = []
# Filter to get only columns with loaded LoRAs
loaded_loras = []
for idx, item in enumerate(selected_loras_state):
if item[0] is not None:
# item structure: (image_index, version_index, scale)
if len(item) == 3:
loaded_loras.append((idx, item[0], item[1], item[2]))
else:
# Legacy state support
loaded_loras.append((idx, item[0], None, item[1]))
print(f"Loaded LoRAs: {loaded_loras}")
print(f"Quantity: {quantity}")
print(f"Selected LoRAs: {selected_loras_state}")
if not loaded_loras:
raise gr.Error("You must select at least one LoRA before proceeding.")
prompts = [
p.strip() for p in [prompt_1, prompt_2, prompt_3, prompt_4]
if p and p.strip()
]
if not prompts:
raise gr.Error("You must fill at least one prompt.")
# limpiar LoRAs previas
pipe.unload_lora_weights()
# 🔥 CARGA DE MÚLTIPLES LORAs
adapter_names = []
adapter_weights = []
# Add lightning LoRA if in speed mode
if speed_mode == "light 4":
pipe.load_lora_weights(
LIGHTNING_LORA_REPO,
weight_name=LIGHTNING_LORA_WEIGHT,
adapter_name="lightning"
)
adapter_names.append("lightning")
adapter_weights.append(1.0)
elif speed_mode == "light 8":
pipe.load_lora_weights(
LIGHTNING_LORA_REPO,
weight_name=LIGHTNING8_LORA_WEIGHT,
adapter_name="lightning"
)
adapter_names.append("lightning")
adapter_weights.append(1.0)
elif speed_mode == "light 4 fp8":
pipe.load_lora_weights(
LIGHTNING_LORA_REPO,
weight_name=LIGHTNING_FP8_4STEPS_LORA_WEIGHT,
adapter_name="lightning"
)
adapter_names.append("lightning")
adapter_weights.append(1.0)
elif speed_mode == "Wuli-art":
pipe.load_lora_weights(
WULI_LORA_REPO,
weight_name=WULI_LORA_WEIGHT,
adapter_name="lightning"
)
adapter_names.append("lightning")
adapter_weights.append(1.0)
elif speed_mode == "Wuli2Step-art":
pipe.load_lora_weights(
WULI2STP_LORA_REPO,
weight_name=WULI2STP_LORA_WEIGHT,
adapter_name="lightning"
)
adapter_names.append("lightning")
adapter_weights.append(1.0)
# Load all selected LoRAs from columns
for col_idx, image_idx, version_idx, scale in loaded_loras:
selected_lora = loras[image_idx]
# Determine weights and repo based on version
if version_idx is not None and "versions" in selected_lora:
version_data = selected_lora["versions"][version_idx]
lora_path = version_data.get("repo") or selected_lora.get("repo")
weight_name = version_data.get("weights", selected_lora.get("weights"))
print(f"Using version '{version_data['title']}' for LoRA {selected_lora['title']}")
else:
lora_path = selected_lora.get("repo")
weight_name = selected_lora.get("weights")
adapter_name = f"lora_{col_idx}"
pipe.load_lora_weights(
lora_path,
weight_name=weight_name,
adapter_name=adapter_name
)
print(f"Loaded LoRA: {lora_path} as {adapter_name} with scale {scale}")
adapter_names.append(adapter_name)
adapter_weights.append(scale)
# Set all adapters
print(f"Setting adapters: {adapter_names} with weights {adapter_weights}")
pipe.set_adapters(adapter_names, adapter_weights=adapter_weights)
# Colectar trigger words de todos los LoRAs cargados
all_trigger_words = []
for _, image_idx, version_idx, _ in loaded_loras:
# PENDING: Could also fetch trigger words from version specific data if available
trigger = loras[image_idx].get("trigger_word", "")
if trigger:
all_trigger_words.append(trigger)
combined_trigger = " ".join(all_trigger_words)
# Aplicar trigger words a los prompts
final_prompts = []
for p in prompts:
if combined_trigger:
final_prompts.append(f"{combined_trigger} {p}")
else:
final_prompts.append(p)
prompts = final_prompts
if randomize_seed:
seed = random.randint(0, MAX_SEED)
multiplier = float(quality_multiplier.replace("x", ""))
width = int(width * multiplier)
height = int(height * multiplier)
# ✅ FIX: quantity viene como index 0..3 (por type="index"), convertimos a 1..4
real_quantity = int(quantity) + 1
try:
if (width * height > 1048 * 1048):
pipe.vae.enable_tiling()
print("VAE tiling enabled")
else:
pipe.vae.disable_tiling()
print("VAE tiling disabled")
except Exception as e:
print(f"Could not enable VAE tiling: {e}")
if (history is None):
history = []
gallery_images = []
for prompt in prompts:
current_seed = seed
if prompt_enhance:
prompt = rewrite(prompt)
# ✅ FIX: quantity ya no es el componente global; es un int real
for _ in range(real_quantity):
generator = torch.Generator(device="cuda").manual_seed(current_seed)
result = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=steps,
true_cfg_scale=cfg_scale,
width=width,
height=height,
num_images_per_prompt=1,
generator=generator,
)
img = result.images[0]
imgtuple = (img, str(current_seed))
gallery_images.append(imgtuple)
# history persistente (acumula)
history = [(img, str(current_seed))] + history
history = history[:24]
yield gallery_images, history, history, seed
current_seed += 100 # separación segura
#return images
# ... (El resto de las funciones como get_huggingface_safetensors, check_custom_model, etc., permanecen sin cambios) ...
def get_huggingface_safetensors(link):
split_link = link.split("/")
if len(split_link) != 2:
raise Exception("Invalid Hugging Face repository link format.")
print(f"Repository attempted: {split_link}")
model_card = ModelCard.load(link)
base_model = model_card.data.get("base_model")
print(f"Base model: {base_model}")
acceptable_models = {
"Qwen/Qwen-Image",
"Qwen/Qwen-Image-2512",
}
models_to_check = base_model if isinstance(base_model, list) else [base_model]
if not any(model in acceptable_models for model in models_to_check):
raise Exception("Not a Qwen-Image LoRA!")
image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
trigger_word = model_card.data.get("instance_prompt", "")
image_url = f"https://huggingface.co/{link}/resolve/main/{image_path}" if image_path else None
fs = HfFileSystem()
try:
list_of_files = fs.ls(link, detail=False)
safetensors_name = None
for file in list_of_files:
filename = file.split("/")[-1]
if filename.endswith(".safetensors"):
safetensors_name = filename
break
if not safetensors_name:
raise Exception("No valid *.safetensors file found in the repository.")
except Exception as e:
print(e)
raise Exception("You didn't include a valid Hugging Face repository with a *.safetensors LoRA")
return split_link[1], link, safetensors_name, trigger_word, image_url
def check_custom_model(link):
print(f"Checking a custom model on: {link}")
if link.endswith('.safetensors'):
if 'huggingface.co' in link:
parts = link.split('/')
try:
hf_index = parts.index('huggingface.co')
username = parts[hf_index + 1]
repo_name = parts[hf_index + 2]
repo = f"{username}/{repo_name}"
safetensors_name = parts[-1]
try:
model_card = ModelCard.load(repo)
trigger_word = model_card.data.get("instance_prompt", "")
image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
image_url = f"https://huggingface.co/{repo}/resolve/main/{image_path}" if image_path else None
except:
trigger_word = ""
image_url = None
return repo_name, repo, safetensors_name, trigger_word, image_url
except:
raise Exception("Invalid safetensors URL format")
if link.startswith("https://"):
if link.startswith("https://huggingface.co") or link.startswith("https://www.huggingface.co"):
link_split = link.split("huggingface.co/")
return get_huggingface_safetensors(link_split[1])
else:
return get_huggingface_safetensors(link)
def add_custom_lora(custom_lora, *args):
# args: radios (N) + scales (N) + selected_loras (1)
num_radios = NUM_LORAS
radio_vals = args[:num_radios]
scale_vals = args[num_radios:num_radios*2]
selected_loras_state = args[-1]
global loras
if custom_lora:
try:
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
print(f"Loaded custom LoRA: {repo}")
model_card_examples = ""
try:
model_card = ModelCard.load(repo)
widget_data = model_card.data.get("widget", [])
if widget_data and len(widget_data) > 0:
examples_html = '
{caption[:30]}{'...' if len(caption) > 30 else ''}
"+trigger_word+" as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}