Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,8 +12,8 @@ import logging
|
|
12 |
logging.basicConfig(level=logging.INFO)
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
-
# Tạo thư mục lưu video
|
16 |
-
output_dir = "
|
17 |
os.makedirs(output_dir, exist_ok=True)
|
18 |
|
19 |
# Constants
|
@@ -62,11 +62,15 @@ def generate_image(prompt, base="Realistic", motion="", step=1, progress=gr.Prog
|
|
62 |
|
63 |
# Tải motion LoRA (tùy chọn)
|
64 |
if motion_loaded != motion:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
|
71 |
progress((0, step))
|
72 |
def progress_callback(i, t, z):
|
@@ -79,16 +83,18 @@ def generate_image(prompt, base="Realistic", motion="", step=1, progress=gr.Prog
|
|
79 |
guidance_scale=1.2,
|
80 |
num_inference_steps=step,
|
81 |
callback=progress_callback,
|
82 |
-
callback_steps=1
|
|
|
|
|
83 |
)
|
84 |
|
85 |
-
# Xuất video
|
86 |
name = str(uuid.uuid4()).replace("-", "")
|
87 |
path = os.path.join(output_dir, f"{name}.mp4")
|
88 |
logger.info(f"Saving video to {path}")
|
89 |
-
export_to_video(output.frames[0], path, fps=10)
|
90 |
|
91 |
-
# Kiểm tra
|
92 |
if not os.path.exists(path):
|
93 |
raise FileNotFoundError(f"Video file {path} was not created.")
|
94 |
|
@@ -145,7 +151,7 @@ with gr.Blocks(css=css) as demo:
|
|
145 |
("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
|
146 |
("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
|
147 |
],
|
148 |
-
value="
|
149 |
interactive=True,
|
150 |
elem_id="gr-input"
|
151 |
)
|
@@ -158,7 +164,7 @@ with gr.Blocks(css=css) as demo:
|
|
158 |
)
|
159 |
submit = gr.Button("Generate Video", variant="primary", elem_id="gr-button")
|
160 |
with gr.Column(elem_id="output"):
|
161 |
-
video = gr.Video(label="AnimateDiff-Lightning", autoplay=True, height=
|
162 |
|
163 |
prompt.submit(fn=generate_image, inputs=[prompt, select_base, select_motion, select_step], outputs=video)
|
164 |
submit.click(fn=generate_image, inputs=[prompt, select_base, select_motion, select_step], outputs=video)
|
@@ -168,11 +174,11 @@ with gr.Blocks(css=css) as demo:
|
|
168 |
["Focus: Eiffel Tower (Animate: Clouds moving)"],
|
169 |
["Focus: Trees In forest (Animate: Lion running)"],
|
170 |
["Focus: Astronaut in Space"],
|
171 |
-
["Focus: Group of Birds in sky (Animate:
|
172 |
-
["Focus:
|
173 |
["Focus: Panda in Forest (Animate: Drinking Tea)"],
|
174 |
["Focus: Kids Playing (Season: Winter)"],
|
175 |
-
["Focus: Cars in Street (Season: Rain, Daytime) (Shot from Distance)
|
176 |
],
|
177 |
fn=generate_image,
|
178 |
inputs=[prompt],
|
|
|
12 |
logging.basicConfig(level=logging.INFO)
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
# Tạo thư mục lưu video trong không gian làm việc của Spaces
|
16 |
+
output_dir = "/home/user/app/outputs"
|
17 |
os.makedirs(output_dir, exist_ok=True)
|
18 |
|
19 |
# Constants
|
|
|
62 |
|
63 |
# Tải motion LoRA (tùy chọn)
|
64 |
if motion_loaded != motion:
|
65 |
+
try:
|
66 |
+
pipe.unload_lora_weights()
|
67 |
+
if motion != "":
|
68 |
+
pipe.load_lora_weights(motion, adapter_name="motion")
|
69 |
+
pipe.set_adapters(["motion"], [0.7])
|
70 |
+
motion_loaded = motion
|
71 |
+
except Exception as e:
|
72 |
+
logger.warning(f"Failed to load LoRA weights: {str(e)}. Proceeding without LoRA.")
|
73 |
+
motion_loaded = ""
|
74 |
|
75 |
progress((0, step))
|
76 |
def progress_callback(i, t, z):
|
|
|
83 |
guidance_scale=1.2,
|
84 |
num_inference_steps=step,
|
85 |
callback=progress_callback,
|
86 |
+
callback_steps=1,
|
87 |
+
width=256, # Giảm độ phân giải để tăng tốc
|
88 |
+
height=256
|
89 |
)
|
90 |
|
91 |
+
# Xuất video với codec H.264
|
92 |
name = str(uuid.uuid4()).replace("-", "")
|
93 |
path = os.path.join(output_dir, f"{name}.mp4")
|
94 |
logger.info(f"Saving video to {path}")
|
95 |
+
export_to_video(output.frames[0], path, fps=10, codec="h264")
|
96 |
|
97 |
+
# Kiểm tra file video
|
98 |
if not os.path.exists(path):
|
99 |
raise FileNotFoundError(f"Video file {path} was not created.")
|
100 |
|
|
|
151 |
("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
|
152 |
("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
|
153 |
],
|
154 |
+
value="",
|
155 |
interactive=True,
|
156 |
elem_id="gr-input"
|
157 |
)
|
|
|
164 |
)
|
165 |
submit = gr.Button("Generate Video", variant="primary", elem_id="gr-button")
|
166 |
with gr.Column(elem_id="output"):
|
167 |
+
video = gr.Video(label="AnimateDiff-Lightning", autoplay=True, height=256, width=256, elem_id="gr-video")
|
168 |
|
169 |
prompt.submit(fn=generate_image, inputs=[prompt, select_base, select_motion, select_step], outputs=video)
|
170 |
submit.click(fn=generate_image, inputs=[prompt, select_base, select_motion, select_step], outputs=video)
|
|
|
174 |
["Focus: Eiffel Tower (Animate: Clouds moving)"],
|
175 |
["Focus: Trees In forest (Animate: Lion running)"],
|
176 |
["Focus: Astronaut in Space"],
|
177 |
+
["Focus: Group of Birds in sky (Animate: Birds Moving) (Shot From distance)"],
|
178 |
+
["Focus: Statue of liberty (Shot from Drone)"],
|
179 |
["Focus: Panda in Forest (Animate: Drinking Tea)"],
|
180 |
["Focus: Kids Playing (Season: Winter)"],
|
181 |
+
["Focus: Cars in Street (Season: Rain, Daytime) (Shot from Distance)"]
|
182 |
],
|
183 |
fn=generate_image,
|
184 |
inputs=[prompt],
|