Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, shutil
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from runner import AutomationRunner
|
| 4 |
+
|
| 5 |
+
# đảm bảo thư mục output luôn sạch
|
| 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 run_automation(prompts, avatar, headless, wait_time, mode):
|
| 13 |
+
clean_outputs()
|
| 14 |
+
# lưu prompt
|
| 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))
|
| 21 |
+
avatar_path = os.path.abspath(os.path.join("images", avatar.name))
|
| 22 |
+
|
| 23 |
+
logs = []
|
| 24 |
+
def log_cb(msg):
|
| 25 |
+
logs.append(msg)
|
| 26 |
+
|
| 27 |
+
runner = AutomationRunner(
|
| 28 |
+
headless=headless,
|
| 29 |
+
wait_time=wait_time,
|
| 30 |
+
avatar_image_path=avatar_path,
|
| 31 |
+
video_folder="outputs",
|
| 32 |
+
mode=mode,
|
| 33 |
+
log_callback=log_cb
|
| 34 |
+
)
|
| 35 |
+
runner.run()
|
| 36 |
+
|
| 37 |
+
# collect kết quả
|
| 38 |
+
videos = sorted([os.path.join("outputs",f) for f in os.listdir("outputs") if f.endswith(".mp4")])
|
| 39 |
+
return logs, videos
|
| 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 'Scene X: ...')", value="Scene 1: ")
|
| 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 |
+
video_files = gr.File(label="Video đầu ra", file_count="multiple")
|
| 54 |
+
run_btn.click(
|
| 55 |
+
run_automation,
|
| 56 |
+
inputs=[prompts, avatar, headless, wait_time, mode],
|
| 57 |
+
outputs=[log_output, video_files]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|