Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,30 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from runner import AutomationRunner
|
| 4 |
|
| 5 |
-
|
| 6 |
-
def clean_outputs():
|
| 7 |
-
if os.path.exists("outputs"):
|
| 8 |
-
shutil.rmtree("outputs")
|
| 9 |
-
os.makedirs("outputs", exist_ok=True)
|
| 10 |
-
os.makedirs("images", exist_ok=True)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
with open("input.txt", "w", encoding="utf-8") as f:
|
| 16 |
f.write(prompts)
|
| 17 |
|
|
|
|
| 18 |
avatar_path = None
|
| 19 |
if avatar is not None:
|
| 20 |
avatar.save(os.path.join("images", avatar.name))
|
|
@@ -24,6 +34,7 @@ def run_automation(prompts, avatar, headless, wait_time, mode):
|
|
| 24 |
def log_cb(msg):
|
| 25 |
logs.append(msg)
|
| 26 |
|
|
|
|
| 27 |
runner = AutomationRunner(
|
| 28 |
headless=headless,
|
| 29 |
wait_time=wait_time,
|
|
@@ -34,27 +45,30 @@ def run_automation(prompts, avatar, headless, wait_time, mode):
|
|
| 34 |
)
|
| 35 |
runner.run()
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
videos = sorted(
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("## HaiLuo AI Video Automation")
|
| 43 |
with gr.Row():
|
| 44 |
-
with gr.Column():
|
| 45 |
-
prompts = gr.Textbox(lines=8, label="Nhập các Scene (mỗi dòng
|
| 46 |
-
avatar = gr.File(label="Ảnh nhân vật (tuỳ chọn)")
|
|
|
|
| 47 |
mode = gr.Radio(["subject","text"], label="Chế độ", value="subject")
|
| 48 |
headless = gr.Checkbox(label="Chạy headless", value=True)
|
| 49 |
wait_time = gr.Slider(60, 1800, value=600, step=30, label="Thời gian chờ (giây)")
|
| 50 |
run_btn = gr.Button("🚀 Chạy Automation")
|
| 51 |
-
with gr.Column():
|
| 52 |
log_output = gr.Textbox(lines=15, label="Logs", interactive=False)
|
| 53 |
-
|
| 54 |
run_btn.click(
|
| 55 |
run_automation,
|
| 56 |
-
inputs=[prompts, avatar, headless, wait_time, mode],
|
| 57 |
-
outputs=[log_output,
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
import gradio as gr
|
| 5 |
from runner import AutomationRunner
|
| 6 |
|
| 7 |
+
COOKIES_FILE = "cookies.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def clean_workspace():
|
| 10 |
+
for d in ("outputs", "images"):
|
| 11 |
+
if os.path.exists(d):
|
| 12 |
+
shutil.rmtree(d)
|
| 13 |
+
os.makedirs(d, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
def run_automation(prompts, avatar, cookies_file, headless, wait_time, mode):
|
| 16 |
+
clean_workspace()
|
| 17 |
+
|
| 18 |
+
# 1) Lưu cookies.json nếu user upload
|
| 19 |
+
if cookies_file is not None:
|
| 20 |
+
# cookies_file.name là đường dẫn tạm tới file upload
|
| 21 |
+
shutil.copy(cookies_file.name, COOKIES_FILE)
|
| 22 |
+
|
| 23 |
+
# 2) Ghi prompt vào input.txt
|
| 24 |
with open("input.txt", "w", encoding="utf-8") as f:
|
| 25 |
f.write(prompts)
|
| 26 |
|
| 27 |
+
# 3) Lưu ảnh nhân vật nếu có
|
| 28 |
avatar_path = None
|
| 29 |
if avatar is not None:
|
| 30 |
avatar.save(os.path.join("images", avatar.name))
|
|
|
|
| 34 |
def log_cb(msg):
|
| 35 |
logs.append(msg)
|
| 36 |
|
| 37 |
+
# 4) Run automation
|
| 38 |
runner = AutomationRunner(
|
| 39 |
headless=headless,
|
| 40 |
wait_time=wait_time,
|
|
|
|
| 45 |
)
|
| 46 |
runner.run()
|
| 47 |
|
| 48 |
+
# 5) Thu thập video đầu ra
|
| 49 |
+
videos = sorted(
|
| 50 |
+
[os.path.join("outputs", f) for f in os.listdir("outputs") if f.endswith(".mp4")]
|
| 51 |
+
)
|
| 52 |
+
return "\n".join(logs), videos
|
| 53 |
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("## HaiLuo AI Video Automation")
|
| 56 |
with gr.Row():
|
| 57 |
+
with gr.Column(scale=2):
|
| 58 |
+
prompts = gr.Textbox(lines=8, label="Nhập các Scene (mỗi dòng `Scene X: ...`)", value="Scene 1: ")
|
| 59 |
+
avatar = gr.File(label="Ảnh nhân vật (tuỳ chọn)", file_count=1, file_types=["image"])
|
| 60 |
+
cookies_file = gr.File(label="Upload cookies.json (bắt buộc lần đầu)", file_count=1, file_types=[".json"])
|
| 61 |
mode = gr.Radio(["subject","text"], label="Chế độ", value="subject")
|
| 62 |
headless = gr.Checkbox(label="Chạy headless", value=True)
|
| 63 |
wait_time = gr.Slider(60, 1800, value=600, step=30, label="Thời gian chờ (giây)")
|
| 64 |
run_btn = gr.Button("🚀 Chạy Automation")
|
| 65 |
+
with gr.Column(scale=1):
|
| 66 |
log_output = gr.Textbox(lines=15, label="Logs", interactive=False)
|
| 67 |
+
video_out = gr.File(label="Download Videos", file_count="multiple")
|
| 68 |
run_btn.click(
|
| 69 |
run_automation,
|
| 70 |
+
inputs=[prompts, avatar, cookies_file, headless, wait_time, mode],
|
| 71 |
+
outputs=[log_output, video_out]
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|