File size: 1,644 Bytes
dbba191
f23ec20
 
1a34a56
 
b9e56c5
f23ec20
dbba191
1a34a56
 
dbba191
1a34a56
 
 
f4f1e6a
1a34a56
f23ec20
f4f1e6a
1a34a56
f23ec20
 
 
 
f4f1e6a
f23ec20
1a34a56
dbba191
 
 
 
 
f4f1e6a
1a34a56
 
 
 
f23ec20
dbba191
1a34a56
dbba191
 
1a34a56
f23ec20
 
dbba191
1a34a56
dbba191
1a34a56
f23ec20
 
dbba191
f23ec20
dbba191
 
1a34a56
f23ec20
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
# --- Configuration ---
MANIM_SCRIPT_FILE = "proof.py"
MANIM_SCENE_NAME = "PythagoreanTheorem"
OUTPUT_VIDEO_PATH = f"media/videos/proof/480p15/{MANIM_SCENE_NAME}.mp4"


def render_manim_video():
    """
    This function runs the Manim command to render the video.
    It cleans up old media files and returns the path to the new video.
    """
    # 1. Clean up previous renders to avoid errors
    if os.path.exists("media"):
        shutil.rmtree("media")

    # 2. Ensure output directory exists
    os.makedirs(os.path.dirname(OUTPUT_VIDEO_PATH), exist_ok=True)

    # 3. Construct and run the Manim command
    command = [
        "manim",
        "-ql",
    ]

    try:
        # Execute the command
        process = subprocess.run(
            command,
            check=True,
        )
        print("Manim STDOUT:", process.stdout)

        # 4. Return the path to the generated video
        if os.path.exists(OUTPUT_VIDEO_PATH):


            return OUTPUT_VIDEO_PATH
        else:
            raise gr.Error("Video file was not created. Check the Manim logs.")

    except subprocess.CalledProcessError as e:
        # If the command fails, raise an error with the logs
        print("Manim STDERR:", e.stderr)
        raise gr.Error(f"Manim failed to render. Error: {e.stderr}")
    except Exception as e:
        raise gr.Error(f"An unexpected error occurred: {str(e)}")

# --- Create the Gradio Interface ---
with gr.Blocks() as demo:
    gr.Markdown("# Interactive Manim: Pythagorean Theorem Proof")
    gr.Markdown(
        outputs=[output_video]
    )

# --- Launch the App ---
demo.launch(server_name="0.0.0.0", share=True)