import gradio as gr import os import subprocess import uuid MANIM_OUT_FOLDER= "/tmp" # Function to run the manim command and return the output video file path def run_manim(source_code, main_class): # Generate a unique id run_id = str(uuid.uuid4()) input_file_path = f"/tmp/{run_id}.py" output_file_name = run_id + ".mp4" #output_file_path = os.path.join(MANIM_OUT_FOLDER, output_file_name) output_file_path = f"/tmp/videos/{run_id}/480p15/{run_id}.mp4" # Save the source code to a temporary file with open(input_file_path, "w") as f: f.write(source_code) # Run the manim command print("Prepare to run", flush=True) command = f"manim render -ql -o {output_file_name} --media_dir {MANIM_OUT_FOLDER} {input_file_path} {main_class}" result = subprocess.run(command, shell=True, check=False, capture_output=True, text=True) print("Return code:", result.returncode, flush=True) print("Output:", result.stdout, flush=True) print("Error:", result.stderr, flush=True) # Return the output video file path if result.returncode == 0: return (output_file_path, "Success") else: return (None, result.stderr) # Gradio Interface with gr.Blocks() as demo: # Input components source_code_input = gr.Code(label="Source Code", language="python") main_class_input = gr.Textbox(label="Main Class") # Output component video_output = gr.Video(label="Output Video", streaming=False, autoplay=True) error_log_output = gr.TextArea(label="Error Log", show_copy_button=True) # Button to trigger the manim command run_button = gr.Button("Run Manim") # Event listener to run the manim command and update the video output run_button.click(fn=run_manim, inputs=[source_code_input, main_class_input], outputs=[video_output, error_log_output]) if __name__ == "__main__": demo.queue(max_size=50) demo.launch(show_error=True)