File size: 2,328 Bytes
dc78703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import subprocess
import os
import yaml
import time

def run_vimax_task(prompt):
    output_dir = "/tmp/outputs"
    config_path = "configs/idea2video.yaml" # المسار الافتراضي في ViMax
    runtime_config = "/tmp/runtime_config.yaml"
    
    # 1. تحديث الـ Config ديناميكياً
    try:
        with open(config_path, 'r') as f:
            config = yaml.safe_load(f)
        
        config['prompt'] = prompt
        config['output_dir'] = output_dir
        
        with open(runtime_config, 'w') as f:
            yaml.dump(config, f)
    except Exception as e:
        yield None, f"Configuration Error: {str(e)}"
        return

    # 2. تشغيل العمليات مع Streaming للـ Logs
    cmd = ["python", "main_idea2video.py", "--config", runtime_config]
    
    process = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        bufsize=1
    )

    full_logs = ""
    for line in process.stdout:
        full_logs += line
        # إرسال التحديثات حية للواجهة
        yield None, full_logs

    process.wait()

    # 3. جلب الفيديو الناتج
    files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.endswith(('.mp4', '.mkv'))]
    if files:
        latest_video = max(files, key=os.path.getctime)
        yield latest_video, full_logs
    else:
        yield None, full_logs + "\n[!] لم يتم العثور على فيديو ناتج."

# إعداد واجهة Gradio مع Queue
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🎬 ViMax on Hugging Face")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(label="Idea Prompt", placeholder="Describe the video story...")
            btn = gr.Button("Generate Video", variant="primary")
        
        with gr.Column():
            video_out = gr.Video(label="Final Output")
            log_out = gr.Code(label="Execution Logs", language="shell", interactive=False)

    btn.click(run_vimax_task, inputs=input_text, outputs=[video_out, log_out])

if __name__ == "__main__":
    # تفعيل الطابور لضمان عدم انهيار الـ Space
    demo.queue(max_size=5).launch(server_name="0.0.0.0", server_port=7860)