Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,964 Bytes
fe211d6 8a2d3f3 f1b22c3 4ebde26 8a2d3f3 fe211d6 de4e465 fe211d6 23dafae fe211d6 23dafae 8a2d3f3 23dafae 8f51460 8a2d3f3 4ebde26 23dafae 8a2d3f3 23dafae 8f51460 8a2d3f3 cbc09c4 8a2d3f3 4ebde26 8a2d3f3 4ebde26 41a9246 23dafae 8f51460 4ebde26 cbc09c4 4ebde26 de4e465 4ebde26 23dafae 4ebde26 23dafae 8a2d3f3 23dafae ae048dd 389aa97 ae048dd 4ebde26 ae048dd 8a2d3f3 23dafae 8a2d3f3 4ebde26 23dafae 8a2d3f3 4ebde26 f1b22c3 23dafae 4ebde26 f1b22c3 41a9246 8a2d3f3 4ebde26 f1b22c3 8a2d3f3 8f51460 8a2d3f3 23dafae 8f51460 fe211d6 23dafae 8a2d3f3 8f51460 23dafae 8a2d3f3 23dafae 4ebde26 41a9246 23dafae 4ebde26 41a9246 8a2d3f3 23dafae 8a2d3f3 8f51460 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 41a9246 f1b22c3 de4e465 23dafae 8a2d3f3 8f51460 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8f51460 f1b22c3 8a2d3f3 8f51460 8a2d3f3 4ebde26 8f51460 8a2d3f3 f1b22c3 8a2d3f3 de4e465 8a2d3f3 f1b22c3 4ebde26 f1b22c3 41a9246 8a2d3f3 23dafae 8a2d3f3 23dafae 4ebde26 8a2d3f3 f1b22c3 23dafae 4ebde26 23dafae 8a2d3f3 23dafae cbc09c4 4ebde26 cbc09c4 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 23dafae cbc09c4 23dafae cbc09c4 8a2d3f3 23dafae 8a2d3f3 23dafae 8a2d3f3 cbc09c4 23dafae 8a2d3f3 cbc09c4 |
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 |
import torch
from diffusers import (
StableDiffusionXLImg2ImgPipeline,
StableDiffusionInpaintPipeline,
DDIMScheduler,
PNDMScheduler,
EulerDiscreteScheduler,
DPMSolverMultistepScheduler
)
from PIL import Image, ImageFilter, ImageEnhance
import numpy as np
import cv2
class InteriorDesignerPro:
def __init__(self):
self.device = torch.device("cuda")
self.model_name = "RealVisXL V4.0"
# Проверка GPU
gpu_name = torch.cuda.get_device_name(0)
self.is_powerful_gpu = any(gpu in gpu_name for gpu in ['A100', 'H100', 'RTX 4090', 'RTX 3090', 'T4'])
# Основная модель - RealVisXL V4
print(f"Loading {self.model_name} on {gpu_name}...")
self.pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"SG161222/RealVisXL_V4.0",
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16"
).to(self.device)
# БЕЗ ЭТИХ СТРОК! Они замедляют H200!
# self.pipe.enable_model_cpu_offload()
# self.pipe.enable_vae_slicing()
# Inpainting модель
try:
self.inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-inpainting",
torch_dtype=torch.float16,
safety_checker=None
).to(self.device)
print("Inpainting model loaded")
except:
print("Warning: Using fallback for inpainting")
self.inpaint_pipe = None
@torch.inference_mode()
def apply_style_pro(self, image, style_name, room_type, strength=0.75, quality="balanced"):
"""Применение стиля к изображению"""
from design_styles import DESIGN_STYLES
style = DESIGN_STYLES.get(style_name, DESIGN_STYLES["Современный минимализм"])
# Строка 56-57 должна быть:
if image.width > 768 or image.height > 768:
image.thumbnail((768, 768), Image.Resampling.LANCZOS)
# Оптимальные настройки для H200:
quality_settings = {
"fast": {"steps": 15, "guidance": 7.0},
"balanced": {"steps": 20, "guidance": 8.0},
"ultra": {"steps": 30, "guidance": 9.0}
}
settings = quality_settings.get(quality, quality_settings["balanced"])
# Промпт для SDXL
room_specific = style.get("room_specific", {}).get(room_type, "")
full_prompt = f"{style['prompt']}, {room_specific}, {room_type} interior design, professional photo, high quality, 8k, photorealistic"
# Генерация с SDXL
result = self.pipe(
prompt=full_prompt,
prompt_2=full_prompt,
negative_prompt=style.get("negative", "low quality, blurry"),
negative_prompt_2=style.get("negative", "low quality, blurry"),
image=image,
strength=strength,
num_inference_steps=settings["steps"],
guidance_scale=settings["guidance"],
original_size=(768, 768),
target_size=(768, 768)
).images[0]
return result
def create_variations(self, image, num_variations=4):
"""Создание вариаций дизайна"""
variations = []
base_seed = torch.randint(0, 1000000, (1,)).item()
for i in range(num_variations):
torch.manual_seed(base_seed + i)
var = self.pipe(
prompt="interior design variation, same style, different details",
prompt_2="interior design variation, same style, different details",
image=image,
strength=0.4 + (i * 0.05),
num_inference_steps=20,
guidance_scale=7.5
).images[0]
variations.append(var)
return variations
def create_hdr_lighting(self, image, intensity=0.3):
"""Улучшение освещения в стиле HDR"""
# Конвертируем в numpy
img_array = np.array(image)
# Применяем CLAHE для улучшения контраста
lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
l_clahe = clahe.apply(l)
enhanced_lab = cv2.merge([l_clahe, a, b])
enhanced_rgb = cv2.cvtColor(enhanced_lab, cv2.COLOR_LAB2RGB)
# Смешиваем с оригиналом
result = cv2.addWeighted(img_array, 1-intensity, enhanced_rgb, intensity, 0)
return Image.fromarray(result)
def enhance_details(self, image):
"""Улучшение деталей изображения"""
# Увеличиваем резкость
enhancer = ImageEnhance.Sharpness(image)
sharp = enhancer.enhance(1.5)
# Немного увеличиваем контраст
enhancer = ImageEnhance.Contrast(sharp)
contrast = enhancer.enhance(1.1)
return contrast
def change_element(self, image, element, value, strength=0.7):
"""Изменение отдельного элемента интерьера"""
from design_styles import ROOM_ELEMENTS
element_info = ROOM_ELEMENTS.get(element, {})
prompt_add = element_info.get("prompt_add", element.lower())
prompt = f"interior with {value} {prompt_add}, professional photo"
negative = f"old {element}, damaged, ugly"
result = self.pipe(
prompt=prompt,
prompt_2=prompt,
negative_prompt=negative,
negative_prompt_2=negative,
image=image,
strength=strength,
num_inference_steps=30,
guidance_scale=8.0
).images[0]
return result
def create_style_comparison(self, image, styles, quality="fast"):
"""Создание сравнения стилей"""
results = []
# Настройки для быстрой генерации
steps = 15 if quality == "fast" else 25
for style in styles:
styled = self.apply_style_pro(
image,
style,
"living room",
strength=0.75,
quality=quality
)
results.append((style, styled))
return results
# Динамическое добавление метода для сетки
def _create_comparison_grid(self, results):
"""Создание сетки из результатов"""
if not results:
return None
images = [img for _, img in results]
titles = [title for title, _ in results]
# Определяем размер сетки
n = len(images)
cols = min(3, n)
rows = (n + cols - 1) // cols
# Размер одного изображения
img_width, img_height = images[0].size
grid_width = img_width * cols
grid_height = img_height * rows
# Создаем сетку
grid = Image.new('RGB', (grid_width, grid_height), 'white')
for idx, (img, title) in enumerate(zip(images, titles)):
row = idx // cols
col = idx % cols
x = col * img_width
y = row * img_height
grid.paste(img, (x, y))
return grid
# Добавляем метод к классу
InteriorDesignerPro._create_comparison_grid = _create_comparison_grid
class ObjectRemover:
"""Класс для удаления объектов"""
def __init__(self, inpaint_pipe):
self.pipe = inpaint_pipe
self.device = torch.device("cuda")
def remove_objects(self, image, mask):
"""Удаление объектов с изображения"""
if self.pipe is None:
# Fallback на простое заполнение
return self.simple_inpaint(image, mask)
# Используем inpainting pipeline
result = self.pipe(
prompt="empty room interior, clean wall, seamless texture",
negative_prompt="furniture, objects, people, clutter",
image=image,
mask_image=mask,
strength=0.99,
num_inference_steps=50,
guidance_scale=7.5
).images[0]
return result
def simple_inpaint(self, image, mask):
"""Простое заполнение через OpenCV"""
img_array = np.array(image)
mask_array = np.array(mask.convert('L'))
# Инпейнтинг через OpenCV
result = cv2.inpaint(img_array, mask_array, 3, cv2.INPAINT_TELEA)
return Image.fromarray(result)
def generate_mask_from_text(self, image, text_description, precision=0.3):
"""Генерация маски на основе текстового описания"""
# Простая маска в центре (заглушка)
width, height = image.size
mask = Image.new('L', (width, height), 0)
# Создаем маску в центре
center_x, center_y = width // 2, height // 2
radius = int(min(width, height) * precision)
# Рисуем круг
from PIL import ImageDraw
draw = ImageDraw.Draw(mask)
draw.ellipse([center_x - radius, center_y - radius,
center_x + radius, center_y + radius], fill=255)
return mask
|