File size: 3,156 Bytes
31a3b89
 
855e230
 
 
31a3b89
855e230
31a3b89
 
 
 
 
 
 
 
 
 
 
 
 
 
855e230
 
 
31a3b89
855e230
 
 
 
 
 
 
 
 
deeab35
855e230
 
 
 
 
 
 
 
 
 
31a3b89
 
 
 
 
855e230
 
 
 
31a3b89
deeab35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
855e230
31a3b89
deeab35
 
 
 
 
 
 
 
 
855e230
 
31a3b89
 
855e230
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import shutil
import gradio as gr
from runner import AutomationRunner

COOKIES_FILE = "cookies.json"

def clean_workspace():
    for d in ("outputs", "images"):
        if os.path.exists(d):
            shutil.rmtree(d)
        os.makedirs(d, exist_ok=True)

def run_automation(prompts, avatar, cookies_file, headless, wait_time, mode):
    clean_workspace()

    # 1) Lưu cookies.json nếu user upload
    if cookies_file is not None:
        shutil.copy(cookies_file.name, COOKIES_FILE)

    # 2) Ghi prompt vào input.txt
    with open("input.txt", "w", encoding="utf-8") as f:
        f.write(prompts)

    # 3) Lưu ảnh nhân vật nếu có
    avatar_path = None
    if avatar is not None:
        avatar.save(os.path.join("images", avatar.name))
        avatar_path = os.path.abspath(os.path.join("images", avatar.name))

    logs = []
    def log_cb(msg):
        logs.append(msg)

    # 4) Chạy automation
    runner = AutomationRunner(
        headless=headless,
        wait_time=wait_time,
        avatar_image_path=avatar_path,
        video_folder="outputs",
        mode=mode,
        log_callback=log_cb
    )
    runner.run()

    # 5) Thu thập video đầu ra
    videos = sorted(
        [os.path.join("outputs", f) for f in os.listdir("outputs") if f.endswith(".mp4")]
    )
    return "\n".join(logs), videos

with gr.Blocks() as demo:
    gr.Markdown("## HaiLuo AI Video Automation")
    with gr.Row():
        with gr.Column(scale=2):
            prompts = gr.Textbox(
                lines=8,
                label="Nhập các Scene (mỗi dòng `Scene X: ...`)",
                value="Scene 1: "
            )
            avatar = gr.File(
                label="Ảnh nhân vật (tuỳ chọn)",
                file_count="single",        # sửa thành "single"
                file_types=["image"]        # hoặc cụ thể hơn ["png","jpg","jpeg"]
            )
            cookies_file = gr.File(
                label="Upload cookies.json (bắt buộc lần đầu)",
                file_count="single",        # sửa thành "single"
                file_types=[".json"]
            )
            mode = gr.Radio(
                ["subject", "text"],
                label="Chế độ",
                value="subject"
            )
            headless = gr.Checkbox(
                label="Chạy headless",
                value=True
            )
            wait_time = gr.Slider(
                60, 1800,
                value=600,
                step=30,
                label="Thời gian chờ (giây)"
            )
            run_btn = gr.Button("🚀 Chạy Automation")
        with gr.Column(scale=1):
            log_output = gr.Textbox(
                lines=15,
                label="Logs",
                interactive=False
            )
            video_out = gr.File(
                label="Download Videos",
                file_count="multiple"
            )
    run_btn.click(
        run_automation,
        inputs=[prompts, avatar, cookies_file, headless, wait_time, mode],
        outputs=[log_output, video_out]
    )

if __name__ == "__main__":
    demo.launch()