File size: 4,779 Bytes
d160dc6 98889c8 1eb87a5 a7111d1 98889c8 d160dc6 1665fe1 1eb87a5 d160dc6 a7111d1 d160dc6 a7111d1 d160dc6 a7111d1 46bb495 d160dc6 3920f5c 1eb87a5 3920f5c 46bb495 d160dc6 46bb495 d160dc6 46bb495 3920f5c d160dc6 a02c6d7 d160dc6 1eb87a5 d85fde4 d160dc6 d85fde4 46bb495 3920f5c d160dc6 a7111d1 3920f5c d160dc6 d85fde4 d160dc6 d85fde4 d160dc6 a7111d1 3920f5c 46bb495 3920f5c a7111d1 0652978 3920f5c d160dc6 0652978 d160dc6 3920f5c d160dc6 3920f5c d85fde4 3920f5c d160dc6 d85fde4 3920f5c d160dc6 3920f5c d85fde4 a7111d1 3920f5c d160dc6 3920f5c d160dc6 d85fde4 d160dc6 1665fe1 d160dc6 1665fe1 d160dc6 d85fde4 d160dc6 98889c8 d160dc6 |
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 |
import logging
import pickle
import warnings
import gradio as gr
import jax
import jax.numpy as jnp
import numpy as np
import torch
from PIL import Image
from diffusers import StableDiffusionXLImg2ImgPipeline
from huggingface_hub import hf_hub_download
from transformers import DPTImageProcessor, DPTForDepthEstimation
from model import build_thera
from utils import make_grid
# Configuração de logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("processing.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Configurações
warnings.filterwarnings("ignore")
JAX_DEVICE = jax.devices("cpu")[0]
TORCH_DEVICE = "cpu"
def load_thera_model(repo_id, filename):
try:
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
with open(model_path, 'rb') as fh:
check = pickle.load(fh)
variables = check['model']
backbone, size = check['backbone'], check['size']
return build_thera(3, backbone, size), variables
except Exception as e:
logger.error(f"Erro ao carregar Thera: {str(e)}")
raise
logger.info("Carregando modelos...")
model_edsr, variables_edsr = load_thera_model("prs-eth/thera-edsr-pro", "model.pkl")
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float32
).to(TORCH_DEVICE)
pipe.load_lora_weights("KappaNeuro/bas-relief", weight_name="BAS-RELIEF.safetensors")
feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(TORCH_DEVICE)
def adjust_size(size):
return max(8, (size // 8) * 8)
def full_pipeline(image, prompt, scale_factor=2.0, progress=gr.Progress()):
try:
progress(0.1, desc="Iniciando...")
image = image.convert("RGB")
source = np.array(image) / 255.0
# Ajuste de dimensões
target_shape = (
adjust_size(int(image.height * scale_factor)),
adjust_size(int(image.width * scale_factor))
)
logger.info(f"Transformação: {image.size} → {target_shape}")
# Gerar grid
coords = make_grid(target_shape)
logger.debug(f"Coords shape: {coords.shape}")
# Super-resolução
progress(0.3, desc="Processando super-resolução...")
source_jax = jax.device_put(source[np.newaxis, ...], JAX_DEVICE)
t = jnp.array([1.0 / (scale_factor ** 2)], dtype=jnp.float32)
upscaled = model_edsr.apply(
variables_edsr,
source_jax,
t,
target_shape
)
upscaled_pil = Image.fromarray((np.array(upscaled[0]) * 255).astype(np.uint8))
# Bas-Relief
progress(0.6, desc="Gerando relevo...")
bas_relief = pipe(
prompt=f"BAS-RELIEF {prompt}, ultra detailed engraving, 16K resolution",
image=upscaled_pil,
strength=0.7,
num_inference_steps=25
).images[0]
# Depth Map
progress(0.8, desc="Calculando profundidade...")
inputs = feature_extractor(bas_relief, return_tensors="pt").to(TORCH_DEVICE)
with torch.no_grad():
depth = depth_model(**inputs).predicted_depth
depth_map = torch.nn.functional.interpolate(
depth.unsqueeze(1),
size=bas_relief.size[::-1],
mode="bicubic"
).squeeze().cpu().numpy()
depth_normalized = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
depth_pil = Image.fromarray((depth_normalized * 255).astype(np.uint8))
return upscaled_pil, bas_relief, depth_pil
except Exception as e:
logger.error(f"ERRO: {str(e)}", exc_info=True)
raise gr.Error(f"Erro no processamento: {str(e)}")
# Interface
with gr.Blocks(title="SuperRes + BasRelief") as app:
gr.Markdown("## 🖼️ Super Resolução + 🗿 Bas-Relief + 🗺️ Mapa de Profundidade")
with gr.Row():
with gr.Column():
img_input = gr.Image(type="pil", label="Entrada")
prompt = gr.Textbox("Escultura detalhada em mármore, alto relevo", label="Descrição")
scale = gr.Slider(1.0, 4.0, value=2.0, label="Escala")
btn = gr.Button("Processar ▶️")
with gr.Column():
img_upscaled = gr.Image(label="Super Resolução")
img_basrelief = gr.Image(label="Bas-Relief")
img_depth = gr.Image(label="Profundidade")
btn.click(full_pipeline, [img_input, prompt, scale], [img_upscaled, img_basrelief, img_depth])
if __name__ == "__main__":
app.launch() |