Spaces:
Running on Zero
Running on Zero
File size: 21,737 Bytes
4bb998d f0d9a3e 4bb998d f0d9a3e 4bb998d 63af8ef f0d9a3e 4bb998d c35c59d 4bb998d c35c59d 4bb998d fe203b8 4bb998d f0d9a3e 84e3f61 4bb998d d2b17be 4bb998d f0d9a3e 4bb998d d2b17be fc74578 d2b17be c35c59d 4bb998d c35c59d 4bb998d c35c59d d2b17be 4bb998d fc74578 4bb998d d2b17be 4bb998d c35c59d 4bb998d c35c59d 4bb998d f59054c 4bb998d f59054c 4bb998d f59054c 4bb998d f59054c 4bb998d f59054c 4bb998d 63af8ef f59054c f0d9a3e fe203b8 4bb998d fe203b8 4bb998d 63af8ef 4bb998d 63af8ef fe203b8 4bb998d fe203b8 4bb998d 63af8ef 4bb998d 63af8ef 4bb998d c35c59d f40c52e c35c59d 4bb998d f0d9a3e c35c59d 4bb998d c35c59d 4bb998d c35c59d fc74578 4bb998d c35c59d 4bb998d c35c59d 4bb998d f279b7b 4bb998d f279b7b c35c59d f0d9a3e 4bb998d c35c59d 4bb998d f0d9a3e fe203b8 f0d9a3e 4bb998d c35c59d f0d9a3e c35c59d f0d9a3e c35c59d f0d9a3e c35c59d 4bb998d c35c59d f279b7b 4bb998d c35c59d 4bb998d c35c59d 4bb998d c35c59d f279b7b 4bb998d f40c52e c35c59d 4bb998d fe203b8 4bb998d 63af8ef 4bb998d fe203b8 4bb998d fe203b8 63af8ef fe203b8 4bb998d fe203b8 4bb998d fe203b8 f0d9a3e e788617 f0d9a3e 4bb998d f40c52e 4bb998d f0d9a3e f40c52e 4bb998d f0d9a3e 4bb998d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | """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(
"""
<div class="seamless-hero">
<span class="seamless-badge">HF Inference · Flux LoRA</span>
<h1>Seamless Texture Studio</h1>
<p>Texturas repetíveis em alta resolução. Descreva o material; o app reforça <em>seamless/tileable</em> automaticamente. API e MCP em <code>/gradio_api/mcp/</code>.</p>
</div>
"""
)
with gr.Tabs():
with gr.Tab("Gerar", id="tab-generate"):
with gr.Row(equal_height=True):
with gr.Column(scale=5):
prompt_input = gr.Textbox(
label="Prompt",
placeholder="ex.: madeira clara com veios finos, desgaste suave",
lines=4,
info="Inglês costuma funcionar melhor com a maioria dos modelos.",
)
negative_prompt_input = gr.Textbox(
label="Prompt negativo",
placeholder="O que evitar (opcional)",
lines=2,
)
with gr.Row():
preset_dropdown = gr.Dropdown(
choices=["None"] + list_presets(),
value="None",
label="Preset",
info="Aplica prompt e parâmetros sugeridos para o material.",
)
seed_input = gr.Number(
label="Seed",
value=-1,
precision=0,
info="−1 = aleatório",
)
with gr.Accordion("Parâmetros avançados", open=False):
guidance_scale_slider = gr.Slider(
1.0, 20.0, value=7.5, step=0.5, label="Guidance scale"
)
num_steps_slider = gr.Slider(
10, 100, value=50, step=5, label="Passos de inferência"
)
with gr.Row():
width_slider = gr.Slider(256, 2048, value=1024, step=64, label="Largura")
height_slider = gr.Slider(256, 2048, value=1024, step=64, label="Altura")
cfg_scale_slider = gr.Slider(1.0, 20.0, value=7.5, step=0.5, label="CFG scale")
lora_strength_slider = gr.Slider(
0.0, 2.0, value=1.0, step=0.1, label="Força do LoRA"
)
with gr.Row():
apply_preset_btn = gr.Button("Aplicar preset", variant="secondary", size="lg")
generate_btn = gr.Button("Gerar textura", variant="primary", size="lg")
apply_preset_btn.click(
fn=apply_preset,
inputs=[
preset_dropdown,
prompt_input,
negative_prompt_input,
guidance_scale_slider,
num_steps_slider,
width_slider,
height_slider,
],
outputs=[
prompt_input,
negative_prompt_input,
guidance_scale_slider,
num_steps_slider,
width_slider,
height_slider,
],
api_name=False,
)
with gr.Column(scale=5):
image_output = gr.Image(
label="Pré-visualização",
type="pil",
height=420,
show_label=True,
)
status_output = gr.Textbox(label="Estado", interactive=False, lines=2)
with gr.Accordion("Metadados técnicos", open=False):
metadata_output = gr.JSON(label="Metadata")
generate_btn.click(
fn=generate_texture,
inputs=[
prompt_input,
negative_prompt_input,
preset_dropdown,
guidance_scale_slider,
num_steps_slider,
seed_input,
width_slider,
height_slider,
cfg_scale_slider,
lora_strength_slider,
],
outputs=[image_output, status_output, metadata_output],
api_name=False,
show_progress="full",
)
with gr.Tab("Galeria"):
gr.Markdown("Últimas gerações desta sessão (memória do processo).")
with gr.Row():
gallery_refresh_btn = gr.Button("Atualizar galeria", variant="secondary")
download_zip_btn = gr.Button("Baixar tudo (ZIP)", variant="primary")
gallery_display = gr.Gallery(
label="Texturas geradas",
columns=4,
rows=2,
height="auto",
object_fit="contain",
show_label=True,
)
zip_download = gr.File(label="Arquivo ZIP", visible=False)
gallery_refresh_btn.click(fn=get_gallery_images, outputs=gallery_display, api_name=False)
download_zip_btn.click(fn=download_gallery_zip, outputs=zip_download, api_name=False).then(
fn=lambda x: gr.update(visible=True) if x else gr.update(),
inputs=zip_download,
outputs=zip_download,
api_name=False,
)
with gr.Tab("Histórico"):
history_table = gr.Dataframe(
label="Últimas execuções",
headers=["Data/hora", "Prompt", "Seed", "Preset"],
interactive=False,
wrap=True,
)
history_refresh_btn = gr.Button("Atualizar tabela")
history_refresh_btn.click(fn=get_history_table, outputs=history_table, api_name=False)
with gr.Tab("MCP / API", visible=MCP_ENABLED):
gr.Markdown(
"""
### Model Context Protocol (MCP)
| | |
| --- | --- |
| **Endpoint** | `/gradio_api/mcp/` |
| **Ferramentas** | `generate_texture`, `get_presets`, `get_history` |
Integração útil para agentes e pipelines que chamam o Space remotamente.
"""
)
gr.HTML(
"""
<footer class="seamless-foot">
Geração via Hugging Face Inference · Modelo configurável por variável <code>MODEL_ID</code>
</footer>
"""
)
# Hidden: endpoints para MCP/Gradio API
with gr.Accordion("🔌 API/MCP (hidden)", open=False, visible=False):
api_prompt = gr.Textbox(label="prompt")
api_negative_prompt = gr.Textbox(label="negative_prompt", value="")
api_preset = gr.Dropdown(choices=["None"] + list_presets(), value="None", label="preset")
api_guidance = gr.Slider(1.0, 20.0, value=7.5, label="guidance_scale")
api_steps = gr.Slider(10, 100, value=50, step=5, label="num_inference_steps")
api_seed = gr.Number(value=-1, precision=0, label="seed")
api_width = gr.Slider(256, 2048, value=1024, step=64, label="width")
api_height = gr.Slider(256, 2048, value=1024, step=64, label="height")
api_cfg = gr.Slider(1.0, 20.0, value=7.5, step=0.5, label="cfg_scale")
api_lora = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="lora_strength")
# Output: imagem (servida automaticamente pelo Gradio) + metadata JSON
api_out_image = gr.Image(label="generated_image", type="pil")
api_out = gr.JSON(label="result")
gr.Button("generate_texture", visible=False).click(
fn=api_generate_texture,
inputs=[
api_prompt,
api_negative_prompt,
api_preset,
api_guidance,
api_steps,
api_seed,
api_width,
api_height,
api_cfg,
api_lora,
],
outputs=[api_out_image, api_out],
api_name="generate_texture",
)
api_presets_out = gr.JSON(label="presets")
gr.Button("get_presets", visible=False).click(
fn=api_get_presets,
inputs=[],
outputs=[api_presets_out],
api_name="get_presets",
)
api_history_limit = gr.Number(value=10, precision=0, label="limit")
api_history_out = gr.JSON(label="history")
gr.Button("get_history", visible=False).click(
fn=api_get_history,
inputs=[api_history_limit],
outputs=[api_history_out],
api_name="get_history",
)
if __name__ == "__main__":
# Spaces-friendly: queue + desabilitar SSR experimental
# Gradio 6: theme/css em launch(), não no construtor de Blocks
demo.queue(default_concurrency_limit=int(os.getenv("GRADIO_CONCURRENCY_LIMIT", "1")))
demo.launch(
theme=build_theme(),
css=SEAMLESS_CSS,
mcp_server=bool(MCP_ENABLED),
ssr_mode=False,
share=False,
)
|