Spaces:
Running
Running
main
Browse files
app.py
CHANGED
@@ -1,18 +1,53 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
import os
|
|
|
4 |
|
5 |
-
def run_ttv():
|
6 |
try:
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
if video_files:
|
12 |
-
|
13 |
-
|
14 |
-
return
|
15 |
-
|
|
|
16 |
except Exception as e:
|
17 |
return f"Có lỗi xảy ra: {str(e)}", None
|
18 |
|
@@ -21,13 +56,43 @@ with gr.Blocks() as demo:
|
|
21 |
gr.Markdown("# Ứng dụng Text-to-Video")
|
22 |
|
23 |
with gr.Row():
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
run_button.click(
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Chạy ứng dụng
|
33 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
import os
|
4 |
+
import time
|
5 |
|
6 |
+
def run_ttv(prompt, progress=gr.Progress()):
|
7 |
try:
|
8 |
+
# Kiểm tra file input.jpg
|
9 |
+
if not os.path.exists("input.jpg"):
|
10 |
+
return "Lỗi: Không tìm thấy file input.jpg", None
|
11 |
+
|
12 |
+
progress(0, desc="Đang khởi động mô hình...")
|
13 |
|
14 |
+
# Chạy file ttv.py với prompt được truyền vào
|
15 |
+
env = os.environ.copy()
|
16 |
+
env["PROMPT_TEXT"] = prompt # Truyền prompt qua biến môi trường
|
17 |
+
|
18 |
+
process = subprocess.Popen(
|
19 |
+
['python', 'ttv.py'],
|
20 |
+
env=env,
|
21 |
+
stdout=subprocess.PIPE,
|
22 |
+
stderr=subprocess.PIPE,
|
23 |
+
universal_newlines=True
|
24 |
+
)
|
25 |
+
|
26 |
+
# Theo dõi output và cập nhật progress
|
27 |
+
output_lines = []
|
28 |
+
while True:
|
29 |
+
line = process.stdout.readline()
|
30 |
+
if not line and process.poll() is not None:
|
31 |
+
break
|
32 |
+
if line:
|
33 |
+
output_lines.append(line.strip())
|
34 |
+
if "Starting..." in line:
|
35 |
+
progress(0.2, desc="Đang xử lý...")
|
36 |
+
elif "Loading model..." in line:
|
37 |
+
progress(0.4, desc="Đang tải mô hình...")
|
38 |
+
elif "Generating..." in line:
|
39 |
+
progress(0.6, desc="Đang tạo video...")
|
40 |
+
|
41 |
+
progress(0.8, desc="Đang hoàn thiện...")
|
42 |
+
|
43 |
+
# Kiểm tra và trả về video mới nhất
|
44 |
+
video_files = [f for f in os.listdir('.') if f.endswith(('.mp4', '.avi', '.mov'))]
|
45 |
if video_files:
|
46 |
+
latest_video = video_files[-1]
|
47 |
+
progress(1.0, desc="Hoàn thành!")
|
48 |
+
return "\n".join(output_lines), latest_video
|
49 |
+
|
50 |
+
return "\n".join(output_lines), None
|
51 |
except Exception as e:
|
52 |
return f"Có lỗi xảy ra: {str(e)}", None
|
53 |
|
|
|
56 |
gr.Markdown("# Ứng dụng Text-to-Video")
|
57 |
|
58 |
with gr.Row():
|
59 |
+
# Thêm input cho prompt
|
60 |
+
text_input = gr.Textbox(
|
61 |
+
label="Nhập mô tả cho video",
|
62 |
+
placeholder="Ví dụ: A little girl is riding a bicycle at high speed...",
|
63 |
+
lines=3
|
64 |
+
)
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
# Thêm preview cho input image
|
68 |
+
input_image = gr.Image(
|
69 |
+
label="Ảnh input.jpg",
|
70 |
+
value="input.jpg" if os.path.exists("input.jpg") else None,
|
71 |
+
interactive=False
|
72 |
+
)
|
73 |
+
video_output = gr.Video(label="Video đã tạo")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
run_button = gr.Button("Tạo Video", variant="primary")
|
77 |
+
output = gr.Textbox(label="Trạng thái")
|
78 |
+
|
79 |
+
# Thêm các thông tin hướng dẫn
|
80 |
+
gr.Markdown("""
|
81 |
+
### Hướng dẫn sử dụng:
|
82 |
+
1. Đặt ảnh nguồn với tên `input.jpg` vào thư mục chứa code
|
83 |
+
2. Nhập mô tả chi tiết cho video bạn muốn tạo
|
84 |
+
3. Nhấn "Tạo Video" và đợi quá trình xử lý hoàn tất
|
85 |
|
86 |
+
### Lưu ý:
|
87 |
+
- Quá trình tạo video có thể mất vài phút
|
88 |
+
- Đảm bảo máy tính có đủ RAM và GPU để xử lý
|
89 |
+
""")
|
90 |
|
91 |
+
run_button.click(
|
92 |
+
fn=run_ttv,
|
93 |
+
inputs=text_input,
|
94 |
+
outputs=[output, video_output]
|
95 |
+
)
|
96 |
|
97 |
# Chạy ứng dụng
|
98 |
if __name__ == "__main__":
|
input.jpg
ADDED
![]() |
Git LFS Details
|
ttv.py
CHANGED
@@ -1,9 +1,14 @@
|
|
1 |
import torch
|
2 |
from diffusers import CogVideoXImageToVideoPipeline
|
3 |
from diffusers.utils import export_to_video, load_image
|
|
|
4 |
|
5 |
print("Starting...")
|
6 |
-
|
|
|
|
|
|
|
|
|
7 |
image = load_image(image="input.jpg")
|
8 |
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
|
9 |
"THUDM/CogVideoX-5b-I2V",
|
@@ -14,6 +19,7 @@ pipe.enable_sequential_cpu_offload()
|
|
14 |
pipe.vae.enable_tiling()
|
15 |
pipe.vae.enable_slicing()
|
16 |
|
|
|
17 |
video = pipe(
|
18 |
prompt=prompt,
|
19 |
image=image,
|
@@ -24,4 +30,6 @@ video = pipe(
|
|
24 |
generator=torch.Generator(device="cuda").manual_seed(42),
|
25 |
).frames[0]
|
26 |
|
27 |
-
|
|
|
|
|
|
1 |
import torch
|
2 |
from diffusers import CogVideoXImageToVideoPipeline
|
3 |
from diffusers.utils import export_to_video, load_image
|
4 |
+
import os
|
5 |
|
6 |
print("Starting...")
|
7 |
+
|
8 |
+
# Lấy prompt từ biến môi trường hoặc sử dụng giá trị mặc định
|
9 |
+
prompt = os.getenv('PROMPT_TEXT', "A little girl is riding a bicycle at high speed. Focused, detailed, realistic.")
|
10 |
+
|
11 |
+
print("Loading model...")
|
12 |
image = load_image(image="input.jpg")
|
13 |
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
|
14 |
"THUDM/CogVideoX-5b-I2V",
|
|
|
19 |
pipe.vae.enable_tiling()
|
20 |
pipe.vae.enable_slicing()
|
21 |
|
22 |
+
print("Generating...")
|
23 |
video = pipe(
|
24 |
prompt=prompt,
|
25 |
image=image,
|
|
|
30 |
generator=torch.Generator(device="cuda").manual_seed(42),
|
31 |
).frames[0]
|
32 |
|
33 |
+
print("Saving video...")
|
34 |
+
export_to_video(video, "output.mp4", fps=8)
|
35 |
+
print("Done!")
|