Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,14 +32,21 @@ import asyncio
|
|
| 32 |
###############################################################################
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# import spaces #
|
| 36 |
|
| 37 |
-
|
| 38 |
import imageio
|
| 39 |
import numpy as np
|
| 40 |
import torch
|
| 41 |
import rembg
|
| 42 |
-
from PIL import Image
|
| 43 |
from torchvision.transforms import v2
|
| 44 |
from pytorch_lightning import seed_everything
|
| 45 |
from omegaconf import OmegaConf
|
|
@@ -284,6 +291,86 @@ def make3d(images):
|
|
| 284 |
############# above part is for 3D generate #############
|
| 285 |
###############################################################################
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
css = """
|
| 288 |
#warning {background-color: #FFCCCB}
|
| 289 |
.chatbot {
|
|
@@ -1265,7 +1352,16 @@ def create_ui():
|
|
| 1265 |
# above part is for 3d generate.
|
| 1266 |
###############################################################################
|
| 1267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1269 |
def clear_tts_fields():
|
| 1270 |
return [gr.update(value=""), gr.update(value=""), None, None, gr.update(value=False), gr.update(value=True), None, None]
|
| 1271 |
|
|
|
|
| 32 |
###############################################################################
|
| 33 |
|
| 34 |
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
import uuid
|
| 38 |
+
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
|
| 39 |
+
from diffusers.utils import export_to_video
|
| 40 |
+
from safetensors.torch import load_file
|
| 41 |
+
|
| 42 |
+
|
| 43 |
# import spaces #
|
| 44 |
|
| 45 |
+
|
| 46 |
import imageio
|
| 47 |
import numpy as np
|
| 48 |
import torch
|
| 49 |
import rembg
|
|
|
|
| 50 |
from torchvision.transforms import v2
|
| 51 |
from pytorch_lightning import seed_everything
|
| 52 |
from omegaconf import OmegaConf
|
|
|
|
| 291 |
############# above part is for 3D generate #############
|
| 292 |
###############################################################################
|
| 293 |
|
| 294 |
+
###############################################################################
|
| 295 |
+
############# this part is for text to video #############
|
| 296 |
+
###############################################################################
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
MORE = """ ## TRY Other Models
|
| 302 |
+
### JARVIS: Your VOICE Assistant -> https://huggingface.co/spaces/KingNish/JARVIS
|
| 303 |
+
### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image
|
| 304 |
+
"""
|
| 305 |
+
|
| 306 |
+
# Constants
|
| 307 |
+
bases = {
|
| 308 |
+
"Cartoon": "frankjoshua/toonyou_beta6",
|
| 309 |
+
"Realistic": "emilianJR/epiCRealism",
|
| 310 |
+
"3d": "Lykon/DreamShaper",
|
| 311 |
+
"Anime": "Yntec/mistoonAnime2"
|
| 312 |
+
}
|
| 313 |
+
step_loaded = None
|
| 314 |
+
base_loaded = "Realistic"
|
| 315 |
+
motion_loaded = None
|
| 316 |
+
|
| 317 |
+
# Ensure model and scheduler are initialized in GPU-enabled function
|
| 318 |
+
if not torch.cuda.is_available():
|
| 319 |
+
raise NotImplementedError("No GPU detected!")
|
| 320 |
+
|
| 321 |
+
device = "cuda"
|
| 322 |
+
dtype = torch.float16
|
| 323 |
+
pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
|
| 324 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
| 325 |
+
|
| 326 |
+
# Safety checkers
|
| 327 |
+
from transformers import CLIPFeatureExtractor
|
| 328 |
+
|
| 329 |
+
feature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")
|
| 330 |
+
|
| 331 |
+
# Function
|
| 332 |
+
#@spaces.GPU(duration=60,queue=False)
|
| 333 |
+
def generate_image(prompt, base="Realistic", motion="", step=8, progress=gr.Progress()):
|
| 334 |
+
global step_loaded
|
| 335 |
+
global base_loaded
|
| 336 |
+
global motion_loaded
|
| 337 |
+
print(prompt, base, step)
|
| 338 |
+
|
| 339 |
+
if step_loaded != step:
|
| 340 |
+
repo = "ByteDance/AnimateDiff-Lightning"
|
| 341 |
+
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
| 342 |
+
pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
|
| 343 |
+
step_loaded = step
|
| 344 |
+
|
| 345 |
+
if base_loaded != base:
|
| 346 |
+
pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)
|
| 347 |
+
base_loaded = base
|
| 348 |
+
|
| 349 |
+
if motion_loaded != motion:
|
| 350 |
+
pipe.unload_lora_weights()
|
| 351 |
+
if motion != "":
|
| 352 |
+
pipe.load_lora_weights(motion, adapter_name="motion")
|
| 353 |
+
pipe.set_adapters(["motion"], [0.7])
|
| 354 |
+
motion_loaded = motion
|
| 355 |
+
|
| 356 |
+
progress((0, step))
|
| 357 |
+
def progress_callback(i, t, z):
|
| 358 |
+
progress((i+1, step))
|
| 359 |
+
|
| 360 |
+
output = pipe(prompt=prompt, guidance_scale=1.2, num_inference_steps=step, callback=progress_callback, callback_steps=1)
|
| 361 |
+
|
| 362 |
+
name = str(uuid.uuid4()).replace("-", "")
|
| 363 |
+
path = f"/tmp/{name}.mp4"
|
| 364 |
+
export_to_video(output.frames[0], path, fps=10)
|
| 365 |
+
return path
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
###############################################################################
|
| 370 |
+
############# above part is for text to video #############
|
| 371 |
+
###############################################################################
|
| 372 |
+
|
| 373 |
+
|
| 374 |
css = """
|
| 375 |
#warning {background-color: #FFCCCB}
|
| 376 |
.chatbot {
|
|
|
|
| 1352 |
# above part is for 3d generate.
|
| 1353 |
###############################################################################
|
| 1354 |
|
| 1355 |
+
###############################################################################
|
| 1356 |
+
############# this part is for text to video #############
|
| 1357 |
+
###############################################################################
|
| 1358 |
+
|
| 1359 |
|
| 1360 |
+
|
| 1361 |
+
|
| 1362 |
+
###############################################################################
|
| 1363 |
+
############# above part is for text to video #############
|
| 1364 |
+
###############################################################################
|
| 1365 |
def clear_tts_fields():
|
| 1366 |
return [gr.update(value=""), gr.update(value=""), None, None, gr.update(value=False), gr.update(value=True), None, None]
|
| 1367 |
|