"""HF Spaces-first Gradio app for Flux Seamless Texture LoRA. - Geração via HF Inference (huggingface_hub.InferenceClient) usando `ModelHandler` - UI moderna com controles avançados, presets, gallery e history - MCP via `mcp_server=True` + endpoints expostos com `api_name` """ import os import logging from datetime import datetime from pathlib import Path from typing import Any, Dict, List, Optional, Tuple, Union import gradio as gr import spaces from config.settings import MCP_ENABLED from ui_theme import build_theme from src.model_handler import ModelHandler from src.presets import list_presets, get_preset_prompt, get_preset_params from src.utils import validate_prompt, generate_seed from src.image_processor import create_zip, OUTPUT_DIR logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info("BOOT: HF Spaces-first app (InferenceClient) carregando…") model_handler = ModelHandler() # Prompt base: o usuário não precisa lembrar de pedir "seamless/tileable". # Mantemos em inglês por compatibilidade com a maioria dos modelos de imagem. BASE_TEXTURE_INSTRUCTIONS = ( "seamless, tileable, repeatable, repeating pattern, perfectly looping texture, " "no visible seams, no borders, no frame, no text, no watermark" ) # Estado simples em memória (suficiente para Spaces; persistência real via OUTPUT_DIR) gallery_state: List[Dict[str, Any]] = [] history_state: List[Dict[str, Any]] = [] def _extract_image(result: Any) -> Any: """Normaliza retornos: PIL, tuple/list/dict.""" if isinstance(result, tuple): return result[0] if isinstance(result, list) and result: return result[0] if isinstance(result, dict): return result.get("image") or (result.get("images", [None])[0] if result.get("images") else None) return result def _augment_prompt_for_seamless(prompt: str) -> str: """ Acrescenta instruções de textura tileable/seamless automaticamente. Se o usuário já menciona seamless/tileable/repeatable, não duplica. """ import re p = (prompt or "").strip() if not p: return p # Se já tem indicação de tile/seamless/repeat, não adiciona. if re.search(r"\b(seamless|tileable|tiling|repeatable|repeating|repeat)\b", p, flags=re.IGNORECASE): return p return f"{BASE_TEXTURE_INSTRUCTIONS}, {p}" def _merge_negative_prompt(preset_neg: str, user_neg: str) -> str: """Combina negative prompt do preset com o do usuário (sem sobrescrever).""" preset_neg = (preset_neg or "").strip() user_neg = (user_neg or "").strip() if not preset_neg: return user_neg if not user_neg: return preset_neg # Dedupe simples por substring (case-insensitive) if preset_neg.lower() in user_neg.lower(): return user_neg if user_neg.lower() in preset_neg.lower(): return preset_neg return f"{preset_neg}, {user_neg}" @spaces.GPU(duration=300) def generate_texture( prompt: str, negative_prompt: str, preset: str, guidance_scale: float, num_inference_steps: int, seed: float, width: int, height: int, cfg_scale: float, lora_strength: float, progress: gr.Progress = gr.Progress(), ) -> Tuple[Any, str, Dict[str, Any]]: """UI handler: gera imagem e atualiza gallery/history.""" try: progress(0.0, desc="Validando prompt…") # Valida o prompt do usuário (antes de anexar o base prompt). is_valid, error = validate_prompt(prompt, max_length=1000) if not is_valid: return None, f"Erro: {error}", {} if preset and preset != "None": preset_prompt = get_preset_prompt(preset) preset_params = get_preset_params(preset) if preset_prompt: prompt = f"{preset_prompt}, {prompt}" if prompt else preset_prompt if preset_params: guidance_scale = float(preset_params.get("guidance_scale", guidance_scale)) num_inference_steps = int(preset_params.get("num_inference_steps", num_inference_steps)) width = int(preset_params.get("width", width)) height = int(preset_params.get("height", height)) if "negative_prompt" in preset_params: negative_prompt = _merge_negative_prompt( str(preset_params.get("negative_prompt") or ""), negative_prompt, ) # Acrescenta instruções seamless/tileable automaticamente prompt = _augment_prompt_for_seamless(prompt) # Revalida após augment (evita estourar limite) is_valid, error = validate_prompt(prompt, max_length=1200) if not is_valid: # fallback: corta com segurança prompt = prompt[:1200] seed_int = int(seed) if seed is not None and seed >= 0 else generate_seed() progress(0.15, desc="Chamando API de inferência…") image, metadata = model_handler.generate( prompt=prompt, negative_prompt=negative_prompt, guidance_scale=float(guidance_scale), num_inference_steps=int(num_inference_steps), seed=seed_int, width=int(width), height=int(height), cfg_scale=float(cfg_scale), lora_strength=float(lora_strength), ) entry = { "timestamp": datetime.now().timestamp(), "prompt": prompt, "negative_prompt": negative_prompt, "params": { "guidance_scale": guidance_scale, "num_inference_steps": num_inference_steps, "seed": seed_int, "width": width, "height": height, "cfg_scale": cfg_scale, "lora_strength": lora_strength, "preset": preset, }, "image_path": metadata.get("image_path"), "image": image, } gallery_state.append(entry) history_state.append(entry) progress(1.0, desc="Concluído") return image, "Pronto — imagem na pré-visualização e na Galeria/Histórico.", metadata except Exception as e: logger.error("Falha ao gerar imagem", exc_info=True) return None, f"Erro: {str(e)}", {} def get_gallery_images() -> List[Tuple[Any, str]]: return [ (entry["image"], f"{entry['prompt'][:50]}...") for entry in reversed(gallery_state[-24:]) if entry.get("image") is not None ] def get_history_table() -> List[List[str]]: rows: List[List[str]] = [] for entry in reversed(history_state[-100:]): ts = datetime.fromtimestamp(entry["timestamp"]).strftime("%Y-%m-%d %H:%M:%S") p = entry["prompt"] p = (p[:60] + "...") if len(p) > 60 else p rows.append([ts, p, str(entry["params"].get("seed", "")), entry["params"].get("preset", "None")]) return rows def download_gallery_zip() -> Optional[Path]: try: image_paths = [ Path(entry["image_path"]) for entry in gallery_state if entry.get("image_path") and Path(entry["image_path"]).exists() ] if not image_paths: return None zip_path = OUTPUT_DIR / f"gallery_{int(datetime.now().timestamp())}.zip" create_zip(image_paths, zip_path) return zip_path except Exception as e: logger.error(f"Erro criando ZIP: {e}", exc_info=True) return None # Endpoints expostos via Gradio API / MCP (api_name) def _sanitize_for_json(obj: Any) -> Any: """Garante que todas as chaves de dicionários sejam strings (ORJSON requirement).""" if isinstance(obj, dict): return {str(k): _sanitize_for_json(v) for k, v in obj.items()} if isinstance(obj, (list, tuple)): return [_sanitize_for_json(v) for v in obj] # Converte tipos não-serializáveis para string if not isinstance(obj, (str, int, float, bool, type(None))): return str(obj) return obj def api_generate_texture( prompt: str, negative_prompt: str = "", preset: str = "None", guidance_scale: float = 7.5, num_inference_steps: int = 50, seed: int = -1, width: int = 1024, height: int = 1024, cfg_scale: float = 7.5, lora_strength: float = 1.0, ) -> Tuple[Any, Dict[str, Any]]: """ Gera uma textura seamless. Retorna: (imagem_gerada, metadata_json) - A imagem é servida automaticamente pelo Gradio para download. - O metadata contém informações sobre a geração. """ image, status, metadata = generate_texture( prompt=prompt, negative_prompt=negative_prompt, preset=preset, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, seed=seed, width=width, height=height, cfg_scale=cfg_scale, lora_strength=lora_strength, ) if image is None: return None, _sanitize_for_json({"success": False, "error": status}) return image, _sanitize_for_json({"success": True, "metadata": metadata}) def api_get_presets() -> Dict[str, Any]: from src.presets import TEXTURE_PRESETS return {"presets": list_presets(), "details": TEXTURE_PRESETS} def api_get_history(limit: int = 10) -> Dict[str, Any]: recent = history_state[-limit:] if len(history_state) > limit else history_state # Evitar enviar a imagem inteira via API safe = [] for e in recent: safe.append( { "timestamp": e["timestamp"], "prompt": e["prompt"], "negative_prompt": e.get("negative_prompt", ""), "params": _sanitize_for_json(e.get("params", {})), "image_path": e.get("image_path"), } ) return _sanitize_for_json({"total": len(history_state), "entries": safe}) SEAMLESS_CSS = """ .seamless-hero { padding: 1.35rem 1.5rem 1.25rem; border-radius: 14px; margin-bottom: 0.5rem; background: linear-gradient(115deg, rgba(245, 158, 11, 0.14) 0%, rgba(148, 163, 184, 0.12) 45%, rgba(241, 245, 249, 0.65) 100%); border: 1px solid rgba(148, 163, 184, 0.45); box-shadow: 0 12px 40px rgba(15, 23, 42, 0.06); } .seamless-hero h1 { margin: 0 0 0.35rem 0; font-size: 1.65rem; letter-spacing: -0.02em; line-height: 1.2; } .seamless-hero p { margin: 0; opacity: 0.88; font-size: 0.98rem; line-height: 1.45; } .seamless-badge { display: inline-block; font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.12em; color: var(--color-accent); margin-bottom: 0.5rem; } footer.seamless-foot { margin-top: 1.25rem; padding-top: 0.75rem; font-size: 0.85rem; opacity: 0.75; border-top: 1px solid rgba(148, 163, 184, 0.35); } """ def apply_preset( preset: str, prompt: str, negative_prompt: str, guidance_scale: float, num_steps: int, width: int, height: int, ): if not preset or preset == "None": return prompt, negative_prompt, guidance_scale, num_steps, width, height preset_prompt = get_preset_prompt(preset) preset_params = get_preset_params(preset) if preset_prompt: prompt = f"{preset_prompt}, {prompt}" if prompt else preset_prompt if preset_params: guidance_scale = float(preset_params.get("guidance_scale", guidance_scale)) num_steps = int(preset_params.get("num_inference_steps", num_steps)) width = int(preset_params.get("width", width)) height = int(preset_params.get("height", height)) if "negative_prompt" in preset_params: negative_prompt = _merge_negative_prompt( str(preset_params.get("negative_prompt") or ""), negative_prompt, ) return prompt, negative_prompt, guidance_scale, num_steps, width, height with gr.Blocks(title="Seamless Texture Studio") as demo: gr.HTML( """
Texturas repetíveis em alta resolução. Descreva o material; o app reforça seamless/tileable automaticamente. API e MCP em /gradio_api/mcp/.