Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,354 +1,632 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
import logging
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
from pathlib import Path
|
| 5 |
import gradio as gr
|
| 6 |
-
import torch
|
| 7 |
-
import torchaudio
|
| 8 |
-
import os
|
| 9 |
-
import requests
|
| 10 |
-
from transformers import pipeline
|
| 11 |
-
import tempfile
|
| 12 |
import numpy as np
|
| 13 |
-
from
|
| 14 |
-
import
|
| 15 |
-
|
| 16 |
-
import
|
| 17 |
-
import
|
| 18 |
-
|
| 19 |
-
import
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
os.environ["TRANSFORMERS_ALLOW_UNSAFE_DESERIALIZATION"] = "1"
|
| 23 |
|
|
|
|
| 24 |
try:
|
| 25 |
import mmaudio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except ImportError:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate, load_video, make_video,
|
| 31 |
-
setup_eval_logging)
|
| 32 |
-
from mmaudio.model.flow_matching import FlowMatching
|
| 33 |
-
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
| 34 |
-
from mmaudio.model.sequence_config import SequenceConfig
|
| 35 |
-
from mmaudio.model.utils.features_utils import FeaturesUtils
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
logging.basicConfig(
|
| 39 |
-
level=logging.INFO,
|
| 40 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 41 |
-
)
|
| 42 |
-
log = logging.getLogger()
|
| 43 |
-
|
| 44 |
-
# CUDA μ€μ
|
| 45 |
-
if torch.cuda.is_available():
|
| 46 |
-
device = torch.device("cuda")
|
| 47 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
| 48 |
-
torch.backends.cudnn.allow_tf32 = True
|
| 49 |
-
torch.backends.cudnn.benchmark = True
|
| 50 |
-
else:
|
| 51 |
-
device = torch.device("cpu")
|
| 52 |
-
|
| 53 |
-
dtype = torch.bfloat16
|
| 54 |
-
|
| 55 |
-
# λͺ¨λΈ μ€μ
|
| 56 |
-
model: ModelConfig = all_model_cfg['large_44k_v2']
|
| 57 |
-
model.download_if_needed()
|
| 58 |
-
output_dir = Path('./output/gradio')
|
| 59 |
-
|
| 60 |
-
setup_eval_logging()
|
| 61 |
-
|
| 62 |
-
# λ²μκΈ° μ€μ - safetensors μ¬μ© μλ
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
try:
|
| 74 |
-
translator = pipeline("translation",
|
| 75 |
-
model="Helsinki-NLP/opus-mt-ko-en",
|
| 76 |
-
device="cpu")
|
| 77 |
-
except Exception as e2:
|
| 78 |
-
log.error(f"Failed to load translation model: {e2}")
|
| 79 |
-
translator = None
|
| 80 |
-
|
| 81 |
-
PIXABAY_API_KEY = "33492762-a28a596ec4f286f84cd328b17"
|
| 82 |
-
|
| 83 |
-
def cleanup_temp_files():
|
| 84 |
-
temp_dir = tempfile.gettempdir()
|
| 85 |
-
for file in os.listdir(temp_dir):
|
| 86 |
-
if file.endswith(('.mp4', '.flac')):
|
| 87 |
-
try:
|
| 88 |
-
os.remove(os.path.join(temp_dir, file))
|
| 89 |
-
except:
|
| 90 |
-
pass
|
| 91 |
-
|
| 92 |
-
atexit.register(cleanup_temp_files)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
enable_conditions=True,
|
| 105 |
-
mode=model.mode,
|
| 106 |
-
bigvgan_vocoder_ckpt=model.bigvgan_16k_path,
|
| 107 |
-
need_vae_encoder=False
|
| 108 |
-
).to(device, dtype).eval()
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
try:
|
| 117 |
-
|
| 118 |
-
if
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
except Exception as e:
|
| 128 |
-
logging.error(f"
|
| 129 |
-
return
|
| 130 |
|
| 131 |
-
|
| 132 |
-
@torch.
|
| 133 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
try:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
-
logging.error(f"Video
|
| 140 |
-
return
|
| 141 |
|
| 142 |
-
def
|
|
|
|
|
|
|
|
|
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
"per_page": 40
|
| 149 |
-
}
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
-
logging.error(f"
|
| 158 |
-
return
|
| 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 |
-
color:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
}
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
box-shadow: 0 4px 15px rgba(33,150,243,0.3);
|
| 278 |
}
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
}
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
| 289 |
}
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
background: rgba(30, 30, 30, 0.95);
|
| 293 |
-
padding: 15px;
|
| 294 |
-
border-radius: 10px;
|
| 295 |
-
border: 1px solid rgba(255, 255, 255, 0.05);
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
from gradio_client import Client, handle_file
|
| 5 |
+
import random
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import logging
|
| 9 |
+
import torch
|
| 10 |
+
from diffusers import AutoencoderKL, TCDScheduler
|
| 11 |
+
from diffusers.models.model_loading_utils import load_state_dict
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
|
| 14 |
+
# Spaces GPU
|
| 15 |
+
try:
|
| 16 |
+
import spaces
|
| 17 |
+
except:
|
| 18 |
+
# GPU λ°μ½λ μ΄ν°κ° μμ λλ₯Ό μν λλ―Έ λ°μ½λ μ΄ν°
|
| 19 |
+
class spaces:
|
| 20 |
+
@staticmethod
|
| 21 |
+
def GPU(duration=None):
|
| 22 |
+
def decorator(func):
|
| 23 |
+
return func
|
| 24 |
+
return decorator
|
| 25 |
+
|
| 26 |
+
# νκ²½ λ³μ μ€μ
|
| 27 |
os.environ["TRANSFORMERS_ALLOW_UNSAFE_DESERIALIZATION"] = "1"
|
| 28 |
|
| 29 |
+
# MMAudio κ΄λ ¨ μν¬νΈ
|
| 30 |
try:
|
| 31 |
import mmaudio
|
| 32 |
+
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate, load_video, make_video,
|
| 33 |
+
setup_eval_logging)
|
| 34 |
+
from mmaudio.model.flow_matching import FlowMatching
|
| 35 |
+
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
| 36 |
+
from mmaudio.model.sequence_config import SequenceConfig
|
| 37 |
+
from mmaudio.model.utils.features_utils import FeaturesUtils
|
| 38 |
+
MMAUDIO_AVAILABLE = True
|
| 39 |
except ImportError:
|
| 40 |
+
MMAUDIO_AVAILABLE = False
|
| 41 |
+
logging.warning("MMAudio not available. Sound generation will be disabled.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# ControlNet λͺ¨λΈ λ‘λ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
+
from controlnet_union import ControlNetModel_Union
|
| 46 |
+
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
| 47 |
+
|
| 48 |
+
# ControlNet μ€μ λ° λ‘λ
|
| 49 |
+
config_file = hf_hub_download(
|
| 50 |
+
"xinsir/controlnet-union-sdxl-1.0",
|
| 51 |
+
filename="config_promax.json",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
config = ControlNetModel_Union.load_config(config_file)
|
| 55 |
+
controlnet_model = ControlNetModel_Union.from_config(config)
|
| 56 |
+
|
| 57 |
+
model_file = hf_hub_download(
|
| 58 |
+
"xinsir/controlnet-union-sdxl-1.0",
|
| 59 |
+
filename="diffusion_pytorch_model_promax.safetensors",
|
| 60 |
+
)
|
| 61 |
+
state_dict = load_state_dict(model_file)
|
| 62 |
+
loaded_keys = list(state_dict.keys())
|
| 63 |
+
|
| 64 |
+
result = ControlNetModel_Union._load_pretrained_model(
|
| 65 |
+
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0", loaded_keys
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
model = result[0]
|
| 69 |
+
model = model.to(device="cuda", dtype=torch.float16)
|
| 70 |
+
|
| 71 |
+
# VAE λ‘λ
|
| 72 |
+
vae = AutoencoderKL.from_pretrained(
|
| 73 |
+
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
| 74 |
+
).to("cuda")
|
| 75 |
+
|
| 76 |
+
# νμ΄νλΌμΈ λ‘λ
|
| 77 |
+
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 78 |
+
"SG161222/RealVisXL_V5.0_Lightning",
|
| 79 |
+
torch_dtype=torch.float16,
|
| 80 |
+
vae=vae,
|
| 81 |
+
controlnet=model,
|
| 82 |
+
variant="fp16",
|
| 83 |
+
).to("cuda")
|
| 84 |
+
|
| 85 |
+
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 86 |
+
|
| 87 |
+
OUTPAINT_MODEL_LOADED = True
|
| 88 |
except Exception as e:
|
| 89 |
+
logging.error(f"Failed to load outpainting models: {str(e)}")
|
| 90 |
+
OUTPAINT_MODEL_LOADED = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# MMAudio λͺ¨λΈ μ€μ λ° λ‘λ
|
| 93 |
+
if MMAUDIO_AVAILABLE:
|
| 94 |
+
try:
|
| 95 |
+
# CUDA μ€μ
|
| 96 |
+
if torch.cuda.is_available():
|
| 97 |
+
device = torch.device("cuda")
|
| 98 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 99 |
+
torch.backends.cudnn.allow_tf32 = True
|
| 100 |
+
torch.backends.cudnn.benchmark = True
|
| 101 |
+
else:
|
| 102 |
+
device = torch.device("cpu")
|
| 103 |
+
|
| 104 |
+
dtype = torch.bfloat16
|
| 105 |
+
|
| 106 |
+
# λͺ¨λΈ μ€μ
|
| 107 |
+
model_cfg: ModelConfig = all_model_cfg['large_44k_v2']
|
| 108 |
+
model_cfg.download_if_needed()
|
| 109 |
+
|
| 110 |
+
setup_eval_logging()
|
| 111 |
+
|
| 112 |
+
# λͺ¨λΈ λ‘λ
|
| 113 |
+
def get_mmaudio_model():
|
| 114 |
+
with torch.cuda.device(device):
|
| 115 |
+
seq_cfg = model_cfg.seq_cfg
|
| 116 |
+
net: MMAudio = get_my_mmaudio(model_cfg.model_name).to(device, dtype).eval()
|
| 117 |
+
net.load_weights(torch.load(model_cfg.model_path, map_location=device, weights_only=True))
|
| 118 |
+
logging.info(f'Loaded MMAudio weights from {model_cfg.model_path}')
|
| 119 |
+
|
| 120 |
+
feature_utils = FeaturesUtils(
|
| 121 |
+
tod_vae_ckpt=model_cfg.vae_path,
|
| 122 |
+
synchformer_ckpt=model_cfg.synchformer_ckpt,
|
| 123 |
+
enable_conditions=True,
|
| 124 |
+
mode=model_cfg.mode,
|
| 125 |
+
bigvgan_vocoder_ckpt=model_cfg.bigvgan_16k_path,
|
| 126 |
+
need_vae_encoder=False
|
| 127 |
+
).to(device, dtype).eval()
|
| 128 |
+
|
| 129 |
+
return net, feature_utils, seq_cfg
|
| 130 |
+
|
| 131 |
+
mmaudio_net, mmaudio_feature_utils, mmaudio_seq_cfg = get_mmaudio_model()
|
| 132 |
+
MMAUDIO_LOADED = True
|
| 133 |
+
except Exception as e:
|
| 134 |
+
logging.error(f"Failed to load MMAudio models: {str(e)}")
|
| 135 |
+
MMAUDIO_LOADED = False
|
| 136 |
+
else:
|
| 137 |
+
MMAUDIO_LOADED = False
|
| 138 |
|
| 139 |
+
# API URLs
|
| 140 |
+
TEXT2IMG_API_URL = "http://211.233.58.201:7896"
|
| 141 |
+
VIDEO_API_URL = "http://211.233.58.201:7875"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
# λ‘κΉ
μ€μ
|
| 144 |
+
logging.basicConfig(level=logging.INFO)
|
| 145 |
+
|
| 146 |
+
# Image size presets
|
| 147 |
+
IMAGE_PRESETS = {
|
| 148 |
+
"컀μ€ν
": {"width": 1024, "height": 1024},
|
| 149 |
+
"1:1 μ μ¬κ°ν": {"width": 1024, "height": 1024},
|
| 150 |
+
"4:3 νμ€": {"width": 1024, "height": 768},
|
| 151 |
+
"16:9 μμ΄λμ€ν¬λ¦°": {"width": 1024, "height": 576},
|
| 152 |
+
"9:16 μΈλ‘ν": {"width": 576, "height": 1024},
|
| 153 |
+
"6:19 νΉμ μΈλ‘ν": {"width": 324, "height": 1024},
|
| 154 |
+
"Instagram μ μ¬κ°ν": {"width": 1080, "height": 1080},
|
| 155 |
+
"Instagram μ€ν 리": {"width": 1080, "height": 1920},
|
| 156 |
+
"Instagram κ°λ‘ν": {"width": 1080, "height": 566},
|
| 157 |
+
"Facebook 컀λ²": {"width": 820, "height": 312},
|
| 158 |
+
"Twitter ν€λ": {"width": 1500, "height": 500},
|
| 159 |
+
"YouTube μΈλ€μΌ": {"width": 1280, "height": 720},
|
| 160 |
+
"LinkedIn λ°°λ": {"width": 1584, "height": 396},
|
| 161 |
+
}
|
| 162 |
|
| 163 |
+
def update_dimensions(preset):
|
| 164 |
+
if preset in IMAGE_PRESETS:
|
| 165 |
+
return IMAGE_PRESETS[preset]["width"], IMAGE_PRESETS[preset]["height"]
|
| 166 |
+
return 1024, 1024
|
| 167 |
|
| 168 |
+
def generate_text_to_image(prompt, width, height, guidance, inference_steps, seed):
|
| 169 |
+
if not prompt:
|
| 170 |
+
return None, "ν둬ννΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ"
|
| 171 |
+
|
| 172 |
try:
|
| 173 |
+
client = Client(TEXT2IMG_API_URL)
|
| 174 |
+
if seed == -1:
|
| 175 |
+
seed = random.randint(0, 9999999)
|
| 176 |
|
| 177 |
+
result = client.predict(
|
| 178 |
+
prompt=prompt,
|
| 179 |
+
width=int(width),
|
| 180 |
+
height=int(height),
|
| 181 |
+
guidance=float(guidance),
|
| 182 |
+
inference_steps=int(inference_steps),
|
| 183 |
+
seed=int(seed),
|
| 184 |
+
do_img2img=False,
|
| 185 |
+
init_image=None,
|
| 186 |
+
image2image_strength=0.8,
|
| 187 |
+
resize_img=True,
|
| 188 |
+
api_name="/generate_image"
|
| 189 |
+
)
|
| 190 |
+
return result[0], f"μ¬μ©λ μλ: {result[1]}"
|
| 191 |
except Exception as e:
|
| 192 |
+
logging.error(f"Image generation error: {str(e)}")
|
| 193 |
+
return None, f"μ€λ₯: {str(e)}"
|
| 194 |
|
| 195 |
+
@spaces.GPU(duration=60)
|
| 196 |
+
@torch.inference_mode()
|
| 197 |
+
def video_to_audio(video_path, prompt, negative_prompt="music", seed=0, num_steps=25, cfg_strength=4.5, duration=8.0):
|
| 198 |
+
"""λΉλμ€μ μ¬μ΄λλ₯Ό μΆκ°νλ ν¨μ"""
|
| 199 |
+
if not MMAUDIO_LOADED:
|
| 200 |
+
logging.error("MMAudio model not loaded")
|
| 201 |
+
return video_path
|
| 202 |
+
|
| 203 |
try:
|
| 204 |
+
rng = torch.Generator(device=device)
|
| 205 |
+
rng.manual_seed(seed)
|
| 206 |
+
fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
|
| 207 |
+
|
| 208 |
+
# λΉλμ€ λ‘λ
|
| 209 |
+
clip_frames, sync_frames, actual_duration = load_video(video_path, duration)
|
| 210 |
+
clip_frames = clip_frames.unsqueeze(0)
|
| 211 |
+
sync_frames = sync_frames.unsqueeze(0)
|
| 212 |
+
mmaudio_seq_cfg.duration = actual_duration
|
| 213 |
+
mmaudio_net.update_seq_lengths(mmaudio_seq_cfg.latent_seq_len, mmaudio_seq_cfg.clip_seq_len, mmaudio_seq_cfg.sync_seq_len)
|
| 214 |
+
|
| 215 |
+
# μ€λμ€ μμ±
|
| 216 |
+
audios = generate(clip_frames,
|
| 217 |
+
sync_frames, [prompt],
|
| 218 |
+
negative_text=[negative_prompt],
|
| 219 |
+
feature_utils=mmaudio_feature_utils,
|
| 220 |
+
net=mmaudio_net,
|
| 221 |
+
fm=fm,
|
| 222 |
+
rng=rng,
|
| 223 |
+
cfg_strength=cfg_strength)
|
| 224 |
+
audio = audios.float().cpu()[0]
|
| 225 |
+
|
| 226 |
+
# λΉλμ€μ μ€λμ€ κ²°ν©
|
| 227 |
+
video_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
| 228 |
+
make_video(video_path,
|
| 229 |
+
video_save_path,
|
| 230 |
+
audio,
|
| 231 |
+
sampling_rate=mmaudio_seq_cfg.sampling_rate,
|
| 232 |
+
duration_sec=mmaudio_seq_cfg.duration)
|
| 233 |
+
|
| 234 |
+
return video_save_path
|
| 235 |
except Exception as e:
|
| 236 |
+
logging.error(f"Video to audio error: {str(e)}")
|
| 237 |
+
return video_path
|
| 238 |
|
| 239 |
+
def generate_video_from_image(image, prompt="", length=4.0, sound_generation="μ¬μ΄λ μμ", sound_prompt="", sound_negative_prompt="music"):
|
| 240 |
+
if image is None:
|
| 241 |
+
return None
|
| 242 |
+
|
| 243 |
try:
|
| 244 |
+
# μ΄λ―Έμ§ μ μ₯
|
| 245 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as fp:
|
| 246 |
+
temp_path = fp.name
|
| 247 |
+
Image.fromarray(image).save(temp_path)
|
|
|
|
|
|
|
| 248 |
|
| 249 |
+
# λΉλμ€ μμ± API νΈμΆ
|
| 250 |
+
client = Client(VIDEO_API_URL)
|
| 251 |
+
result = client.predict(
|
| 252 |
+
input_image=handle_file(temp_path),
|
| 253 |
+
prompt=prompt if prompt else "Generate natural motion",
|
| 254 |
+
n_prompt="",
|
| 255 |
+
seed=random.randint(0, 9999999),
|
| 256 |
+
use_teacache=True,
|
| 257 |
+
video_length=float(length),
|
| 258 |
+
api_name="/process"
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
os.unlink(temp_path)
|
| 262 |
+
|
| 263 |
+
if result and len(result) > 0:
|
| 264 |
+
video_dict = result[0]
|
| 265 |
+
video_path = video_dict.get("video") if isinstance(video_dict, dict) else None
|
| 266 |
+
|
| 267 |
+
# μ¬μ΄λ μμ± μ΅μ
μ΄ μ νλ κ²½μ°
|
| 268 |
+
if video_path and sound_generation == "μ¬μ΄λ μμ±" and MMAUDIO_LOADED:
|
| 269 |
+
# μ¬μ΄λ ν둬ννΈκ° λΉμ΄μμΌλ©΄ κΈ°λ³Έκ° μ¬μ©
|
| 270 |
+
if not sound_prompt:
|
| 271 |
+
sound_prompt = prompt if prompt else "ambient sound"
|
| 272 |
+
|
| 273 |
+
# λΉλμ€μ μ¬μ΄λ μΆκ°
|
| 274 |
+
video_with_sound = video_to_audio(
|
| 275 |
+
video_path,
|
| 276 |
+
sound_prompt,
|
| 277 |
+
sound_negative_prompt,
|
| 278 |
+
duration=length
|
| 279 |
+
)
|
| 280 |
+
return video_with_sound
|
| 281 |
+
|
| 282 |
+
return video_path
|
| 283 |
+
|
| 284 |
except Exception as e:
|
| 285 |
+
logging.error(f"Video generation error: {str(e)}")
|
| 286 |
+
return None
|
| 287 |
+
|
| 288 |
+
def prepare_image_and_mask(image, width, height, overlap_percentage, alignment):
|
| 289 |
+
"""μ΄λ―Έμ§μ λ§μ€ν¬λ₯Ό μ€λΉνλ ν¨μ"""
|
| 290 |
+
if image is None:
|
| 291 |
+
return None, None
|
| 292 |
+
|
| 293 |
+
# PIL μ΄λ―Έμ§λ‘ λ³ν
|
| 294 |
+
if isinstance(image, np.ndarray):
|
| 295 |
+
image = Image.fromarray(image).convert('RGB')
|
| 296 |
+
|
| 297 |
+
target_size = (width, height)
|
| 298 |
+
|
| 299 |
+
# μ΄λ―Έμ§λ₯Ό νκ² ν¬κΈ°μ λ§κ² μ‘°μ
|
| 300 |
+
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
| 301 |
+
new_width = int(image.width * scale_factor)
|
| 302 |
+
new_height = int(image.height * scale_factor)
|
| 303 |
+
|
| 304 |
+
# μ΄λ―Έμ§ 리μ¬μ΄μ¦
|
| 305 |
+
source = image.resize((new_width, new_height), Image.LANCZOS)
|
| 306 |
+
|
| 307 |
+
# μ€λ²λ© κ³μ°
|
| 308 |
+
overlap_x = int(new_width * (overlap_percentage / 100))
|
| 309 |
+
overlap_y = int(new_height * (overlap_percentage / 100))
|
| 310 |
+
overlap_x = max(overlap_x, 1)
|
| 311 |
+
overlap_y = max(overlap_y, 1)
|
| 312 |
+
|
| 313 |
+
# μ λ ¬μ λ°λ₯Έ λ§μ§ κ³μ°
|
| 314 |
+
if alignment == "κ°μ΄λ°":
|
| 315 |
+
margin_x = (target_size[0] - new_width) // 2
|
| 316 |
+
margin_y = (target_size[1] - new_height) // 2
|
| 317 |
+
elif alignment == "μΌμͺ½":
|
| 318 |
+
margin_x = 0
|
| 319 |
+
margin_y = (target_size[1] - new_height) // 2
|
| 320 |
+
elif alignment == "μ€λ₯Έμͺ½":
|
| 321 |
+
margin_x = target_size[0] - new_width
|
| 322 |
+
margin_y = (target_size[1] - new_height) // 2
|
| 323 |
+
elif alignment == "μ":
|
| 324 |
+
margin_x = (target_size[0] - new_width) // 2
|
| 325 |
+
margin_y = 0
|
| 326 |
+
elif alignment == "μλ":
|
| 327 |
+
margin_x = (target_size[0] - new_width) // 2
|
| 328 |
+
margin_y = target_size[1] - new_height
|
| 329 |
+
|
| 330 |
+
# λ°°κ²½ μ΄λ―Έμ§ μμ±
|
| 331 |
+
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 332 |
+
background.paste(source, (margin_x, margin_y))
|
| 333 |
+
|
| 334 |
+
# λ§μ€ν¬ μμ±
|
| 335 |
+
mask = Image.new('L', target_size, 255)
|
| 336 |
+
mask_draw = ImageDraw.Draw(mask)
|
| 337 |
+
|
| 338 |
+
# λ§μ€ν¬ μμ 그리기
|
| 339 |
+
white_gaps_patch = 2
|
| 340 |
+
|
| 341 |
+
left_overlap = margin_x + overlap_x if alignment != "μΌμͺ½" else margin_x
|
| 342 |
+
right_overlap = margin_x + new_width - overlap_x if alignment != "μ€λ₯Έμͺ½" else margin_x + new_width
|
| 343 |
+
top_overlap = margin_y + overlap_y if alignment != "μ" else margin_y
|
| 344 |
+
bottom_overlap = margin_y + new_height - overlap_y if alignment != "μλ" else margin_y + new_height
|
| 345 |
+
|
| 346 |
+
mask_draw.rectangle([
|
| 347 |
+
(left_overlap, top_overlap),
|
| 348 |
+
(right_overlap, bottom_overlap)
|
| 349 |
+
], fill=0)
|
| 350 |
+
|
| 351 |
+
return background, mask
|
| 352 |
+
|
| 353 |
+
@spaces.GPU(duration=24)
|
| 354 |
+
def outpaint_image(image, prompt, width, height, overlap_percentage, alignment, num_steps=8):
|
| 355 |
+
"""μ΄λ―Έμ§ μμνμΈν
μ€ν"""
|
| 356 |
+
if image is None:
|
| 357 |
+
return None
|
| 358 |
+
|
| 359 |
+
if not OUTPAINT_MODEL_LOADED:
|
| 360 |
+
return Image.new('RGB', (width, height), (200, 200, 200))
|
| 361 |
+
|
| 362 |
+
try:
|
| 363 |
+
# μ΄λ―Έμ§μ λ§μ€ν¬ μ€λΉ
|
| 364 |
+
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, alignment)
|
| 365 |
+
if background is None:
|
| 366 |
+
return None
|
| 367 |
+
|
| 368 |
+
# cnet_image μμ± (λ§μ€ν¬ μμμ κ²μμμΌλ‘)
|
| 369 |
+
cnet_image = background.copy()
|
| 370 |
+
cnet_image.paste(0, (0, 0), mask)
|
| 371 |
+
|
| 372 |
+
# ν둬ννΈ μ€λΉ
|
| 373 |
+
final_prompt = f"{prompt}, high quality, 4k" if prompt else "high quality, 4k"
|
| 374 |
+
|
| 375 |
+
# GPUμμ μ€ν
|
| 376 |
+
with torch.autocast(device_type="cuda", dtype=torch.float16):
|
| 377 |
+
(
|
| 378 |
+
prompt_embeds,
|
| 379 |
+
negative_prompt_embeds,
|
| 380 |
+
pooled_prompt_embeds,
|
| 381 |
+
negative_pooled_prompt_embeds,
|
| 382 |
+
) = pipe.encode_prompt(final_prompt, "cuda", True)
|
| 383 |
+
|
| 384 |
+
# μμ± νλ‘μΈμ€
|
| 385 |
+
for generated_image in pipe(
|
| 386 |
+
prompt_embeds=prompt_embeds,
|
| 387 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
| 388 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 389 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
| 390 |
+
image=cnet_image,
|
| 391 |
+
num_inference_steps=num_steps
|
| 392 |
+
):
|
| 393 |
+
# μ€κ° κ²°κ³Ό (νμμ μ¬μ©)
|
| 394 |
+
pass
|
| 395 |
+
|
| 396 |
+
# μ΅μ’
μ΄λ―Έμ§
|
| 397 |
+
final_image = generated_image
|
| 398 |
+
|
| 399 |
+
# RGBAλ‘ λ³ννκ³ λ§μ€ν¬ μ μ©
|
| 400 |
+
final_image = final_image.convert("RGBA")
|
| 401 |
+
cnet_image.paste(final_image, (0, 0), mask)
|
| 402 |
+
|
| 403 |
+
return cnet_image
|
| 404 |
+
|
| 405 |
+
except Exception as e:
|
| 406 |
+
logging.error(f"Outpainting error: {str(e)}")
|
| 407 |
+
return background if 'background' in locals() else None
|
| 408 |
|
| 409 |
+
# CSS
|
| 410 |
+
css = """
|
| 411 |
+
:root {
|
| 412 |
+
--primary-color: #f8c3cd;
|
| 413 |
+
--secondary-color: #b3e5fc;
|
| 414 |
+
--background-color: #f5f5f7;
|
| 415 |
+
--card-background: #ffffff;
|
| 416 |
+
--text-color: #424242;
|
| 417 |
+
--accent-color: #ffb6c1;
|
| 418 |
+
--success-color: #c8e6c9;
|
| 419 |
+
--warning-color: #fff9c4;
|
| 420 |
+
--shadow-color: rgba(0, 0, 0, 0.1);
|
| 421 |
+
--border-radius: 12px;
|
| 422 |
}
|
| 423 |
+
.gradio-container {
|
| 424 |
+
max-width: 1200px !important;
|
| 425 |
+
margin: 0 auto !important;
|
|
|
|
| 426 |
}
|
| 427 |
+
.panel-box {
|
| 428 |
+
border-radius: var(--border-radius) !important;
|
| 429 |
+
box-shadow: 0 8px 16px var(--shadow-color) !important;
|
| 430 |
+
background-color: var(--card-background) !important;
|
| 431 |
+
padding: 20px !important;
|
| 432 |
+
margin-bottom: 20px !important;
|
| 433 |
}
|
| 434 |
+
#generate-btn, #video-btn, #outpaint-btn {
|
| 435 |
+
background: linear-gradient(135deg, #ff9a9e, #fad0c4) !important;
|
| 436 |
+
font-size: 1.1rem !important;
|
| 437 |
+
padding: 12px 24px !important;
|
| 438 |
+
margin-top: 10px !important;
|
| 439 |
+
width: 100% !important;
|
| 440 |
}
|
| 441 |
+
.tabitem {
|
| 442 |
+
min-height: 700px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
}
|
| 444 |
"""
|
| 445 |
|
| 446 |
+
# Gradio Interface
|
| 447 |
+
demo = gr.Blocks(css=css, title="AI μ΄λ―Έμ§ & λΉλμ€ μμ±κΈ°")
|
| 448 |
+
|
| 449 |
+
with demo:
|
| 450 |
+
gr.Markdown("# π¨ Ginigen μ€νλμ€")
|
| 451 |
+
|
| 452 |
+
with gr.Tabs() as tabs:
|
| 453 |
+
# 첫 λ²μ§Έ ν: ν
μ€νΈ to μ΄λ―Έμ§
|
| 454 |
+
with gr.Tab("ν
μ€νΈβμ΄λ―Έμ§βλΉλμ€", elem_classes="tabitem"):
|
| 455 |
+
with gr.Row(equal_height=True):
|
| 456 |
+
# μ
λ ₯ 컬λΌ
|
| 457 |
+
with gr.Column(scale=1):
|
| 458 |
+
with gr.Group(elem_classes="panel-box"):
|
| 459 |
+
gr.Markdown("### π μ΄λ―Έμ§ μμ± μ€μ ")
|
| 460 |
+
|
| 461 |
+
prompt = gr.Textbox(
|
| 462 |
+
label="ν둬ννΈ(νκΈ/μμ΄ κ°λ₯)",
|
| 463 |
+
placeholder="μμ±νκ³ μΆμ μ΄λ―Έμ§λ₯Ό μ€λͺ
νμΈμ...",
|
| 464 |
+
lines=3
|
| 465 |
+
)
|
| 466 |
+
|
| 467 |
+
size_preset = gr.Dropdown(
|
| 468 |
+
choices=list(IMAGE_PRESETS.keys()),
|
| 469 |
+
value="1:1 μ μ¬κ°ν",
|
| 470 |
+
label="ν¬κΈ° ν리μ
"
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
with gr.Row():
|
| 474 |
+
width = gr.Slider(256, 2048, 1024, step=64, label="λλΉ")
|
| 475 |
+
height = gr.Slider(256, 2048, 1024, step=64, label="λμ΄")
|
| 476 |
+
|
| 477 |
+
with gr.Row():
|
| 478 |
+
guidance = gr.Slider(1.0, 20.0, 3.5, step=0.1, label="κ°μ΄λμ€")
|
| 479 |
+
steps = gr.Slider(1, 50, 30, step=1, label="μ€ν
")
|
| 480 |
+
|
| 481 |
+
seed = gr.Number(label="μλ (-1=λλ€)", value=-1)
|
| 482 |
+
|
| 483 |
+
generate_btn = gr.Button("π¨ μ΄λ―Έμ§ μμ±", variant="primary", elem_id="generate-btn")
|
| 484 |
+
|
| 485 |
+
with gr.Group(elem_classes="panel-box"):
|
| 486 |
+
gr.Markdown("### π¬ λΉλμ€ μμ± μ€μ ")
|
| 487 |
+
|
| 488 |
+
video_prompt = gr.Textbox(
|
| 489 |
+
label="(μ ν) λΉλμ€ ν둬ννΈ(μμ΄λ‘ μ
λ ₯)",
|
| 490 |
+
placeholder="λΉλμ€μ μμ§μμ μ€λͺ
νμΈμ... (λΉμλλ©΄ κΈ°λ³Έ μμ§μ μ μ©)",
|
| 491 |
+
lines=2
|
| 492 |
+
)
|
| 493 |
+
|
| 494 |
+
video_length = gr.Slider(
|
| 495 |
+
minimum=1,
|
| 496 |
+
maximum=60,
|
| 497 |
+
value=4,
|
| 498 |
+
step=0.5,
|
| 499 |
+
label="λΉλμ€ κΈΈμ΄ (μ΄)",
|
| 500 |
+
info="1μ΄μμ 60μ΄κΉμ§ μ ν κ°λ₯ν©λλ€"
|
| 501 |
+
)
|
| 502 |
+
|
| 503 |
+
# μ¬μ΄λ μμ± μ΅μ
μΆκ°
|
| 504 |
+
sound_generation = gr.Radio(
|
| 505 |
+
choices=["μ¬μ΄λ μμ", "μ¬μ΄λ μμ±"],
|
| 506 |
+
value="μ¬μ΄λ μμ",
|
| 507 |
+
label="μ¬μ΄λ μ΅μ
",
|
| 508 |
+
info="λΉλμ€μ μ¬μ΄λλ₯Ό μΆκ°ν μ§ μ ννμΈμ"
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
# μ¬μ΄λ κ΄λ ¨ μ
λ ₯ νλ (μ‘°κ±΄λΆ νμ)
|
| 512 |
+
with gr.Column(visible=False) as sound_options:
|
| 513 |
+
sound_prompt = gr.Textbox(
|
| 514 |
+
label="μ¬μ΄λ ν둬ννΈ (μ ν)",
|
| 515 |
+
placeholder="μμ±ν μ¬μ΄λλ₯Ό μ€λͺ
νμΈμ... (λΉμλλ©΄ λΉλμ€ ν둬ννΈ μ¬μ©)",
|
| 516 |
+
lines=2
|
| 517 |
+
)
|
| 518 |
+
sound_negative_prompt = gr.Textbox(
|
| 519 |
+
label="μ¬μ΄λ λ€κ±°ν°λΈ ν둬ννΈ",
|
| 520 |
+
value="music",
|
| 521 |
+
lines=1
|
| 522 |
+
)
|
| 523 |
+
|
| 524 |
+
video_btn = gr.Button("π¬ λΉλμ€λ‘ λ³ν", variant="secondary", elem_id="video-btn")
|
| 525 |
+
|
| 526 |
+
# μΆλ ₯ 컬λΌ
|
| 527 |
+
with gr.Column(scale=1):
|
| 528 |
+
with gr.Group(elem_classes="panel-box"):
|
| 529 |
+
gr.Markdown("### πΌοΈ μμ± κ²°κ³Ό")
|
| 530 |
+
|
| 531 |
+
output_image = gr.Image(label="μμ±λ μ΄λ―Έμ§", type="numpy")
|
| 532 |
+
output_seed = gr.Textbox(label="μλ μ 보")
|
| 533 |
+
output_video = gr.Video(label="μμ±λ λΉλμ€")
|
| 534 |
|
| 535 |
+
# λ λ²μ§Έ ν: μ΄λ―Έμ§ μμνμΈν
|
| 536 |
+
with gr.Tab("μ΄λ―Έμ§ λΉμ¨ λ³κ²½/μμ±", elem_classes="tabitem"):
|
| 537 |
+
with gr.Row(equal_height=True):
|
| 538 |
+
# μ
λ ₯ 컬λΌ
|
| 539 |
+
with gr.Column(scale=1):
|
| 540 |
+
with gr.Group(elem_classes="panel-box"):
|
| 541 |
+
gr.Markdown("### πΌοΈ μ΄λ―Έμ§ μ
λ‘λ")
|
| 542 |
+
|
| 543 |
+
input_image = gr.Image(
|
| 544 |
+
label="μλ³Έ μ΄λ―Έμ§",
|
| 545 |
+
type="numpy"
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
outpaint_prompt = gr.Textbox(
|
| 549 |
+
label="ν둬ννΈ (μ ν)",
|
| 550 |
+
placeholder="νμ₯ν μμμ λν μ€λͺ
...",
|
| 551 |
+
lines=2
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
with gr.Group(elem_classes="panel-box"):
|
| 555 |
+
gr.Markdown("### βοΈ μμνμΈν
μ€μ ")
|
| 556 |
+
|
| 557 |
+
outpaint_size_preset = gr.Dropdown(
|
| 558 |
+
choices=list(IMAGE_PRESETS.keys()),
|
| 559 |
+
value="16:9 μμ΄λμ€ν¬λ¦°",
|
| 560 |
+
label="λͺ©ν ν¬κΈ° ν리μ
"
|
| 561 |
+
)
|
| 562 |
+
|
| 563 |
+
with gr.Row():
|
| 564 |
+
outpaint_width = gr.Slider(256, 2048, 1280, step=64, label="λͺ©ν λλΉ")
|
| 565 |
+
outpaint_height = gr.Slider(256, 2048, 720, step=64, label="λͺ©ν λμ΄")
|
| 566 |
+
|
| 567 |
+
alignment = gr.Dropdown(
|
| 568 |
+
choices=["κ°μ΄λ°", "μΌμͺ½", "μ€λ₯Έμͺ½", "μ", "μλ"],
|
| 569 |
+
value="κ°μ΄λ°",
|
| 570 |
+
label="μ λ ¬"
|
| 571 |
+
)
|
| 572 |
+
|
| 573 |
+
overlap_percentage = gr.Slider(
|
| 574 |
+
minimum=1,
|
| 575 |
+
maximum=50,
|
| 576 |
+
value=10,
|
| 577 |
+
step=1,
|
| 578 |
+
label="λ§μ€ν¬ μ€λ²λ© (%)"
|
| 579 |
+
)
|
| 580 |
+
|
| 581 |
+
outpaint_steps = gr.Slider(
|
| 582 |
+
minimum=4,
|
| 583 |
+
maximum=12,
|
| 584 |
+
value=8,
|
| 585 |
+
step=1,
|
| 586 |
+
label="μΆλ‘ μ€ν
"
|
| 587 |
+
)
|
| 588 |
+
|
| 589 |
+
outpaint_btn = gr.Button("π¨ μμνμΈν
μ€ν", variant="primary", elem_id="outpaint-btn")
|
| 590 |
+
|
| 591 |
+
# μΆλ ₯ 컬λΌ
|
| 592 |
+
with gr.Column(scale=1):
|
| 593 |
+
with gr.Group(elem_classes="panel-box"):
|
| 594 |
+
gr.Markdown("### πΌοΈ κ²°κ³Ό")
|
| 595 |
+
|
| 596 |
+
outpaint_result = gr.Image(label="μμνμΈν
κ²°κ³Ό")
|
| 597 |
+
|
| 598 |
+
# μ΄λ²€νΈ μ°κ²° - 첫 λ²μ§Έ ν
|
| 599 |
+
size_preset.change(update_dimensions, [size_preset], [width, height])
|
| 600 |
+
|
| 601 |
+
generate_btn.click(
|
| 602 |
+
generate_text_to_image,
|
| 603 |
+
[prompt, width, height, guidance, steps, seed],
|
| 604 |
+
[output_image, output_seed]
|
| 605 |
+
)
|
| 606 |
+
|
| 607 |
+
# μ¬μ΄λ μ΅μ
νμ/μ¨κΉ
|
| 608 |
+
def toggle_sound_options(choice):
|
| 609 |
+
return gr.update(visible=(choice == "μ¬μ΄λ μμ±"))
|
| 610 |
+
|
| 611 |
+
sound_generation.change(
|
| 612 |
+
toggle_sound_options,
|
| 613 |
+
[sound_generation],
|
| 614 |
+
[sound_options]
|
| 615 |
+
)
|
| 616 |
+
|
| 617 |
+
video_btn.click(
|
| 618 |
+
generate_video_from_image,
|
| 619 |
+
[output_image, video_prompt, video_length, sound_generation, sound_prompt, sound_negative_prompt],
|
| 620 |
+
[output_video]
|
| 621 |
+
)
|
| 622 |
+
|
| 623 |
+
# μ΄λ²€νΈ μ°κ²° - λ λ²μ§Έ ν
|
| 624 |
+
outpaint_size_preset.change(update_dimensions, [outpaint_size_preset], [outpaint_width, outpaint_height])
|
| 625 |
+
|
| 626 |
+
outpaint_btn.click(
|
| 627 |
+
outpaint_image,
|
| 628 |
+
[input_image, outpaint_prompt, outpaint_width, outpaint_height, overlap_percentage, alignment, outpaint_steps],
|
| 629 |
+
[outpaint_result]
|
| 630 |
+
)
|
| 631 |
+
|
| 632 |
+
demo.launch()
|