Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
-
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import random
|
| 7 |
import time
|
|
|
|
| 8 |
from omegaconf import OmegaConf
|
| 9 |
import torch
|
| 10 |
import torchvision
|
|
@@ -21,6 +21,19 @@ from funcs import (
|
|
| 21 |
save_videos
|
| 22 |
)
|
| 23 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def download_model():
|
| 26 |
REPO_ID = 'Doubiiu/DynamiCrafter_1024'
|
|
@@ -45,11 +58,57 @@ model.eval()
|
|
| 45 |
model = model.cuda()
|
| 46 |
|
| 47 |
# 번역 모델 로드
|
| 48 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
@spaces.GPU(duration=300, gpu_type="l40s")
|
| 51 |
-
def infer(image, prompt, steps=50, cfg_scale=7.5, eta=1.0, fs=3, seed=123):
|
| 52 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# 한글 입력 확인 및 번역
|
| 54 |
if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in prompt):
|
| 55 |
translated = translator(prompt, max_length=512)
|
|
@@ -59,8 +118,7 @@ def infer(image, prompt, steps=50, cfg_scale=7.5, eta=1.0, fs=3, seed=123):
|
|
| 59 |
save_fps = 8
|
| 60 |
seed_everything(seed)
|
| 61 |
transform = transforms.Compose([
|
| 62 |
-
transforms.Resize(
|
| 63 |
-
transforms.CenterCrop(resolution),
|
| 64 |
])
|
| 65 |
|
| 66 |
print('start:', prompt, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
|
|
@@ -70,15 +128,14 @@ def infer(image, prompt, steps=50, cfg_scale=7.5, eta=1.0, fs=3, seed=123):
|
|
| 70 |
|
| 71 |
batch_size = 1
|
| 72 |
channels = model.model.diffusion_model.out_channels
|
| 73 |
-
frames = model.temporal_length
|
| 74 |
h, w = resolution[0] // 8, resolution[1] // 8
|
| 75 |
noise_shape = [batch_size, channels, frames, h, w]
|
| 76 |
|
| 77 |
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 78 |
text_emb = model.get_learned_conditioning([prompt])
|
| 79 |
|
| 80 |
-
img_tensor =
|
| 81 |
-
img_tensor = (img_tensor
|
| 82 |
image_tensor_resized = transform(img_tensor)
|
| 83 |
videos = image_tensor_resized.unsqueeze(0)
|
| 84 |
|
|
@@ -110,11 +167,11 @@ def infer(image, prompt, steps=50, cfg_scale=7.5, eta=1.0, fs=3, seed=123):
|
|
| 110 |
torch.cuda.empty_cache()
|
| 111 |
|
| 112 |
i2v_examples = [
|
| 113 |
-
['
|
| 114 |
-
['
|
| 115 |
]
|
| 116 |
|
| 117 |
-
css = """#
|
| 118 |
|
| 119 |
with gr.Blocks(analytics_enabled=False, css=css) as dynamicrafter_iface:
|
| 120 |
|
|
@@ -122,8 +179,6 @@ with gr.Blocks(analytics_enabled=False, css=css) as dynamicrafter_iface:
|
|
| 122 |
with gr.Column():
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column():
|
| 125 |
-
with gr.Row():
|
| 126 |
-
i2v_input_image = gr.Image(label="Input Image",elem_id="input_img")
|
| 127 |
with gr.Row():
|
| 128 |
i2v_input_text = gr.Textbox(label='Prompts (한글 입력 가능)')
|
| 129 |
with gr.Row():
|
|
@@ -133,19 +188,21 @@ with gr.Blocks(analytics_enabled=False, css=css) as dynamicrafter_iface:
|
|
| 133 |
with gr.Row():
|
| 134 |
i2v_steps = gr.Slider(minimum=1, maximum=50, step=1, elem_id="i2v_steps", label="Sampling steps", value=50)
|
| 135 |
i2v_motion = gr.Slider(minimum=5, maximum=20, step=1, elem_id="i2v_motion", label="FPS", value=10)
|
|
|
|
| 136 |
i2v_end_btn = gr.Button("Generate")
|
| 137 |
with gr.Row():
|
| 138 |
i2v_output_video = gr.Video(label="Generated Video",elem_id="output_vid",autoplay=True,show_share_button=True)
|
| 139 |
|
| 140 |
gr.Examples(examples=i2v_examples,
|
| 141 |
-
inputs=[
|
| 142 |
outputs=[i2v_output_video],
|
| 143 |
fn = infer,
|
| 144 |
-
cache_examples=False
|
| 145 |
)
|
| 146 |
-
i2v_end_btn.click(inputs=[
|
| 147 |
outputs=[i2v_output_video],
|
| 148 |
fn = infer
|
| 149 |
)
|
| 150 |
|
| 151 |
-
dynamicrafter_iface.launch(server_port=
|
|
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import random
|
| 6 |
import time
|
| 7 |
+
import uuid
|
| 8 |
from omegaconf import OmegaConf
|
| 9 |
import torch
|
| 10 |
import torchvision
|
|
|
|
| 21 |
save_videos
|
| 22 |
)
|
| 23 |
from transformers import pipeline
|
| 24 |
+
from diffusers import StableDiffusionXLPipeline
|
| 25 |
+
#import spaces
|
| 26 |
+
import tensorflow as tf
|
| 27 |
+
print(tf.__version__)
|
| 28 |
+
|
| 29 |
+
print("GPU available:", len(tf.config.list_physical_devices('GPU')) > 0)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def is_tensor(x):
|
| 34 |
+
return tf.is_tensor(x)
|
| 35 |
+
|
| 36 |
+
os.environ['KERAS_BACKEND'] = 'tensorflow'
|
| 37 |
|
| 38 |
def download_model():
|
| 39 |
REPO_ID = 'Doubiiu/DynamiCrafter_1024'
|
|
|
|
| 58 |
model = model.cuda()
|
| 59 |
|
| 60 |
# 번역 모델 로드
|
| 61 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device=0 if torch.cuda.is_available() else -1)
|
| 62 |
+
|
| 63 |
+
# 이미지 생성 모델 로드
|
| 64 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 65 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 66 |
+
"SG161222/RealVisXL_V4.0",
|
| 67 |
+
torch_dtype=torch.float32,
|
| 68 |
+
use_safetensors=True,
|
| 69 |
+
add_watermarker=False
|
| 70 |
+
).to(device)
|
| 71 |
+
|
| 72 |
+
def generate_image(prompt: str):
|
| 73 |
+
# 한글 입력 감지 및 번역
|
| 74 |
+
if any('\uac00' <= char <= '\ud7a3' for char in prompt):
|
| 75 |
+
translated = translator(prompt, max_length=512)
|
| 76 |
+
prompt = translated[0]['translation_text']
|
| 77 |
+
|
| 78 |
+
# Hi-res와 3840x2160 스타일 적용
|
| 79 |
+
prompt = f"hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic"
|
| 80 |
+
|
| 81 |
+
# 고정된 설정값
|
| 82 |
+
negative_prompt = "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly, (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, disgusting, amputation"
|
| 83 |
+
width = 1024
|
| 84 |
+
height = 576
|
| 85 |
+
guidance_scale = 6
|
| 86 |
+
num_inference_steps = 100
|
| 87 |
+
seed = random.randint(0, 2**32 - 1)
|
| 88 |
+
generator = torch.Generator().manual_seed(seed)
|
| 89 |
+
|
| 90 |
+
image = pipe(
|
| 91 |
+
prompt=prompt,
|
| 92 |
+
negative_prompt=negative_prompt,
|
| 93 |
+
width=width,
|
| 94 |
+
height=height,
|
| 95 |
+
guidance_scale=guidance_scale,
|
| 96 |
+
num_inference_steps=num_inference_steps,
|
| 97 |
+
generator=generator,
|
| 98 |
+
).images[0]
|
| 99 |
+
|
| 100 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
| 101 |
+
image.save(unique_name)
|
| 102 |
+
return unique_name
|
| 103 |
+
|
| 104 |
+
# @spaces.GPU(duration=300, gpu_type="l40s")
|
| 105 |
+
def infer(prompt, steps=50, cfg_scale=7.5, eta=1.0, fs=3, seed=123, frames=64):
|
| 106 |
|
|
|
|
|
|
|
| 107 |
try:
|
| 108 |
+
# 이미지 생성
|
| 109 |
+
image_path = generate_image(prompt)
|
| 110 |
+
image = torchvision.io.read_image(image_path).float() / 255.0
|
| 111 |
+
|
| 112 |
# 한글 입력 확인 및 번역
|
| 113 |
if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in prompt):
|
| 114 |
translated = translator(prompt, max_length=512)
|
|
|
|
| 118 |
save_fps = 8
|
| 119 |
seed_everything(seed)
|
| 120 |
transform = transforms.Compose([
|
| 121 |
+
transforms.Resize(resolution, antialias=True),
|
|
|
|
| 122 |
])
|
| 123 |
|
| 124 |
print('start:', prompt, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
|
|
|
|
| 128 |
|
| 129 |
batch_size = 1
|
| 130 |
channels = model.model.diffusion_model.out_channels
|
|
|
|
| 131 |
h, w = resolution[0] // 8, resolution[1] // 8
|
| 132 |
noise_shape = [batch_size, channels, frames, h, w]
|
| 133 |
|
| 134 |
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 135 |
text_emb = model.get_learned_conditioning([prompt])
|
| 136 |
|
| 137 |
+
img_tensor = image.to(model.device)
|
| 138 |
+
img_tensor = (img_tensor - 0.5) * 2
|
| 139 |
image_tensor_resized = transform(img_tensor)
|
| 140 |
videos = image_tensor_resized.unsqueeze(0)
|
| 141 |
|
|
|
|
| 167 |
torch.cuda.empty_cache()
|
| 168 |
|
| 169 |
i2v_examples = [
|
| 170 |
+
['우주인 복장으로 기타를 치는 남자', 30, 7.5, 1.0, 6, 123, 64],
|
| 171 |
+
['time-lapse of a blooming flower with leaves and a stem', 30, 7.5, 1.0, 10, 123, 64],
|
| 172 |
]
|
| 173 |
|
| 174 |
+
css = """#output_vid {max-width: 1024px; max-height: 576px}"""
|
| 175 |
|
| 176 |
with gr.Blocks(analytics_enabled=False, css=css) as dynamicrafter_iface:
|
| 177 |
|
|
|
|
| 179 |
with gr.Column():
|
| 180 |
with gr.Row():
|
| 181 |
with gr.Column():
|
|
|
|
|
|
|
| 182 |
with gr.Row():
|
| 183 |
i2v_input_text = gr.Textbox(label='Prompts (한글 입력 가능)')
|
| 184 |
with gr.Row():
|
|
|
|
| 188 |
with gr.Row():
|
| 189 |
i2v_steps = gr.Slider(minimum=1, maximum=50, step=1, elem_id="i2v_steps", label="Sampling steps", value=50)
|
| 190 |
i2v_motion = gr.Slider(minimum=5, maximum=20, step=1, elem_id="i2v_motion", label="FPS", value=10)
|
| 191 |
+
i2v_frames = gr.Slider(minimum=16, maximum=128, step=16, elem_id="i2v_frames", label="Number of frames", value=64)
|
| 192 |
i2v_end_btn = gr.Button("Generate")
|
| 193 |
with gr.Row():
|
| 194 |
i2v_output_video = gr.Video(label="Generated Video",elem_id="output_vid",autoplay=True,show_share_button=True)
|
| 195 |
|
| 196 |
gr.Examples(examples=i2v_examples,
|
| 197 |
+
inputs=[i2v_input_text, i2v_steps, i2v_cfg_scale, i2v_eta, i2v_motion, i2v_seed, i2v_frames],
|
| 198 |
outputs=[i2v_output_video],
|
| 199 |
fn = infer,
|
| 200 |
+
cache_examples=False
|
| 201 |
)
|
| 202 |
+
i2v_end_btn.click(inputs=[i2v_input_text, i2v_steps, i2v_cfg_scale, i2v_eta, i2v_motion, i2v_seed, i2v_frames],
|
| 203 |
outputs=[i2v_output_video],
|
| 204 |
fn = infer
|
| 205 |
)
|
| 206 |
|
| 207 |
+
dynamicrafter_iface.launch(server_port=7930, server_name="0.0.0.0", share=True)
|
| 208 |
+
|