File size: 1,969 Bytes
7bbb93a
bbcd536
 
 
9cd92c6
bbcd536
9cd92c6
bbcd536
 
 
 
7bbb93a
bbcd536
 
1ba4136
 
bbcd536
 
 
 
 
 
98d3da4
bbcd536
6260d09
98d3da4
 
 
bbcd536
 
6260d09
 
 
 
bbcd536
 
 
 
 
 
 
 
 
6260d09
bbcd536
 
 
 
 
6260d09
bbcd536
 
6260d09
bbcd536
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
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)