import gradio as gr import requests import io import random import os from PIL import Image # Configuración global API_TOKEN = os.getenv("HF_READ_TOKEN") HEADERS = {"Authorization": f"Bearer {API_TOKEN}"} TIMEOUT = 120 # Opciones de modelos LORA LORA_OPTIONS = { "Aitana": "RedSparkie/aitaoca2", "Alice": "RedSparkie/wonderland", "Andrea": "RedSparkie/bote", "Halle": "RedSparkie/quema", "Lluna": "RedSparkie/luna", "Lola": "RedSparkie/lola", "Maxi": "RedSparkie/maximo", "Pruckute": "RedSparkie/pruckute", "Rivers": "RedSparkie/larios4" } DEFAULT_MODEL = "black-forest-labs/FLUX.1-dev" # Función principal def query(lora_name, prompt, steps, cfg_scale, randomize_seed, seed, width, height): if not prompt: return None lora_id = LORA_OPTIONS.get(lora_name, DEFAULT_MODEL) api_url = f"https://api-inference.huggingface.co/models/{lora_id.strip()}" if randomize_seed: seed = random.randint(1, 4294967296) # Payload payload = { "inputs": f"{prompt}, ultra detail, ultra elaboration, ultra quality, perfect.", "steps": steps, "cfg_scale": cfg_scale, "seed": seed, "width": width, "height": height } # Petición con reintentos for attempt in range(3): # Máximo de 3 intentos response = requests.post(api_url, headers=HEADERS, json=payload, timeout=TIMEOUT) if response.status_code == 200: try: image = Image.open(io.BytesIO(response.content)) return image, str(seed) except Exception as e: raise gr.Error(f"Error al procesar la imagen: {e}") elif response.status_code == 503: if attempt < 2: # Si no es el último intento time.sleep(5) else: raise gr.Error("El modelo está cargándose, por favor intenta más tarde.") else: raise gr.Error(f"Error inesperado: {response.status_code} - {response.text}") return None # Interfaz with gr.Blocks() as app: gr.Markdown("