bahi-bh commited on
Commit
dc78703
·
verified ·
1 Parent(s): a99adc3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ import yaml
5
+ import time
6
+
7
+ def run_vimax_task(prompt):
8
+ output_dir = "/tmp/outputs"
9
+ config_path = "configs/idea2video.yaml" # المسار الافتراضي في ViMax
10
+ runtime_config = "/tmp/runtime_config.yaml"
11
+
12
+ # 1. تحديث الـ Config ديناميكياً
13
+ try:
14
+ with open(config_path, 'r') as f:
15
+ config = yaml.safe_load(f)
16
+
17
+ config['prompt'] = prompt
18
+ config['output_dir'] = output_dir
19
+
20
+ with open(runtime_config, 'w') as f:
21
+ yaml.dump(config, f)
22
+ except Exception as e:
23
+ yield None, f"Configuration Error: {str(e)}"
24
+ return
25
+
26
+ # 2. تشغيل العمليات مع Streaming للـ Logs
27
+ cmd = ["python", "main_idea2video.py", "--config", runtime_config]
28
+
29
+ process = subprocess.Popen(
30
+ cmd,
31
+ stdout=subprocess.PIPE,
32
+ stderr=subprocess.STDOUT,
33
+ text=True,
34
+ bufsize=1
35
+ )
36
+
37
+ full_logs = ""
38
+ for line in process.stdout:
39
+ full_logs += line
40
+ # إرسال التحديثات حية للواجهة
41
+ yield None, full_logs
42
+
43
+ process.wait()
44
+
45
+ # 3. جلب الفيديو الناتج
46
+ files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.endswith(('.mp4', '.mkv'))]
47
+ if files:
48
+ latest_video = max(files, key=os.path.getctime)
49
+ yield latest_video, full_logs
50
+ else:
51
+ yield None, full_logs + "\n[!] لم يتم العثور على فيديو ناتج."
52
+
53
+ # إعداد واجهة Gradio مع Queue
54
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
55
+ gr.Markdown("# 🎬 ViMax on Hugging Face")
56
+
57
+ with gr.Row():
58
+ with gr.Column():
59
+ input_text = gr.Textbox(label="Idea Prompt", placeholder="Describe the video story...")
60
+ btn = gr.Button("Generate Video", variant="primary")
61
+
62
+ with gr.Column():
63
+ video_out = gr.Video(label="Final Output")
64
+ log_out = gr.Code(label="Execution Logs", language="shell", interactive=False)
65
+
66
+ btn.click(run_vimax_task, inputs=input_text, outputs=[video_out, log_out])
67
+
68
+ if __name__ == "__main__":
69
+ # تفعيل الطابور لضمان عدم انهيار الـ Space
70
+ demo.queue(max_size=5).launch(server_name="0.0.0.0", server_port=7860)