| 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() |
|
|
| |
| if cookies_file is not None: |
| shutil.copy(cookies_file.name, COOKIES_FILE) |
|
|
| |
| with open("input.txt", "w", encoding="utf-8") as f: |
| f.write(prompts) |
|
|
| |
| 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) |
|
|
| |
| runner = AutomationRunner( |
| headless=headless, |
| wait_time=wait_time, |
| avatar_image_path=avatar_path, |
| video_folder="outputs", |
| mode=mode, |
| log_callback=log_cb |
| ) |
| runner.run() |
|
|
| |
| 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", |
| file_types=["image"] |
| ) |
| cookies_file = gr.File( |
| label="Upload cookies.json (bắt buộc lần đầu)", |
| file_count="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() |
|
|