Vmax / app.py
bahi-bh's picture
Create app.py
dc78703 verified
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)