Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,73 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import subprocess
|
| 3 |
-
import os
|
| 4 |
-
import shutil
|
| 5 |
-
|
| 6 |
-
# --- Configuration ---
|
| 7 |
-
MANIM_SCRIPT_FILE = "proof.py"
|
| 8 |
-
MANIM_SCENE_NAME = "PythagoreanTheorem"
|
| 9 |
-
OUTPUT_VIDEO_PATH = f"media/videos/proof/480p15/{MANIM_SCENE_NAME}.mp4"
|
| 10 |
-
|
| 11 |
-
def render_manim_video():
|
| 12 |
-
"""
|
| 13 |
-
This function runs the Manim command to render the video.
|
| 14 |
-
It cleans up old media files and returns the path to the new video.
|
| 15 |
-
"""
|
| 16 |
-
# 1. Clean up previous renders to avoid errors
|
| 17 |
-
if os.path.exists("media"):
|
| 18 |
-
shutil.rmtree("media")
|
| 19 |
-
|
| 20 |
-
# 2. Construct and run the Manim command
|
| 21 |
-
# Using -ql for "low quality" to ensure faster rendering on CPU
|
| 22 |
-
command = [
|
| 23 |
-
"manim",
|
| 24 |
-
"-ql",
|
| 25 |
-
MANIM_SCRIPT_FILE,
|
| 26 |
-
MANIM_SCENE_NAME
|
| 27 |
-
]
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
# Execute the command
|
| 31 |
-
process = subprocess.run(
|
| 32 |
-
command,
|
| 33 |
-
check=True,
|
| 34 |
-
capture_output=True,
|
| 35 |
-
text=True
|
| 36 |
-
)
|
| 37 |
-
print("Manim STDOUT:", process.stdout)
|
| 38 |
-
|
| 39 |
-
# 3. Return the path to the generated video
|
| 40 |
-
if os.path.exists(OUTPUT_VIDEO_PATH):
|
| 41 |
-
return OUTPUT_VIDEO_PATH
|
| 42 |
-
else:
|
| 43 |
-
raise gr.Error("Video file was not created. Check the Manim logs.")
|
| 44 |
-
|
| 45 |
-
except subprocess.CalledProcessError as e:
|
| 46 |
-
# If the command fails, raise an error with the logs
|
| 47 |
-
print("Manim STDERR:", e.stderr)
|
| 48 |
-
raise gr.Error(f"Manim failed to render. Error: {e.stderr}")
|
| 49 |
-
except Exception as e:
|
| 50 |
-
raise gr.Error(f"An unexpected error occurred: {str(e)}")
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# --- Create the Gradio Interface ---
|
| 54 |
-
with gr.Blocks() as demo:
|
| 55 |
-
gr.Markdown("# Interactive Manim: Pythagorean Theorem Proof")
|
| 56 |
-
gr.Markdown(
|
| 57 |
-
"Click the button below to render a Manim animation that visually proves "
|
| 58 |
-
"$a^2 + b^2 = c^2$. The render may take a minute."
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
with gr.Row():
|
| 62 |
-
render_button = gr.Button("Render Video", variant="primary")
|
| 63 |
-
|
| 64 |
-
output_video = gr.Video(label="Rendered Animation", interactive=False)
|
| 65 |
-
|
| 66 |
-
render_button.click(
|
| 67 |
-
fn=render_manim_video,
|
| 68 |
-
inputs=[],
|
| 69 |
-
outputs=[output_video]
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
# --- Launch the App ---
|
| 73 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
# --- Configuration ---
|
| 7 |
+
MANIM_SCRIPT_FILE = "proof.py"
|
| 8 |
+
MANIM_SCENE_NAME = "PythagoreanTheorem"
|
| 9 |
+
OUTPUT_VIDEO_PATH = f"media/videos/proof/480p15/{MANIM_SCENE_NAME}.mp4"
|
| 10 |
+
|
| 11 |
+
def render_manim_video():
|
| 12 |
+
"""
|
| 13 |
+
This function runs the Manim command to render the video.
|
| 14 |
+
It cleans up old media files and returns the path to the new video.
|
| 15 |
+
"""
|
| 16 |
+
# 1. Clean up previous renders to avoid errors
|
| 17 |
+
if os.path.exists("media"):
|
| 18 |
+
shutil.rmtree("media")
|
| 19 |
+
|
| 20 |
+
# 2. Construct and run the Manim command
|
| 21 |
+
# Using -ql for "low quality" to ensure faster rendering on CPU
|
| 22 |
+
command = [
|
| 23 |
+
"manim",
|
| 24 |
+
"-ql",
|
| 25 |
+
MANIM_SCRIPT_FILE,
|
| 26 |
+
MANIM_SCENE_NAME
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Execute the command
|
| 31 |
+
process = subprocess.run(
|
| 32 |
+
command,
|
| 33 |
+
check=True,
|
| 34 |
+
capture_output=True,
|
| 35 |
+
text=True
|
| 36 |
+
)
|
| 37 |
+
print("Manim STDOUT:", process.stdout)
|
| 38 |
+
|
| 39 |
+
# 3. Return the path to the generated video
|
| 40 |
+
if os.path.exists(OUTPUT_VIDEO_PATH):
|
| 41 |
+
return OUTPUT_VIDEO_PATH
|
| 42 |
+
else:
|
| 43 |
+
raise gr.Error("Video file was not created. Check the Manim logs.")
|
| 44 |
+
|
| 45 |
+
except subprocess.CalledProcessError as e:
|
| 46 |
+
# If the command fails, raise an error with the logs
|
| 47 |
+
print("Manim STDERR:", e.stderr)
|
| 48 |
+
raise gr.Error(f"Manim failed to render. Error: {e.stderr}")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
raise gr.Error(f"An unexpected error occurred: {str(e)}")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# --- Create the Gradio Interface ---
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# Interactive Manim: Pythagorean Theorem Proof")
|
| 56 |
+
gr.Markdown(
|
| 57 |
+
"Click the button below to render a Manim animation that visually proves "
|
| 58 |
+
"$a^2 + b^2 = c^2$. The render may take a minute."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
render_button = gr.Button("Render Video", variant="primary")
|
| 63 |
+
|
| 64 |
+
output_video = gr.Video(label="Rendered Animation", interactive=False)
|
| 65 |
+
|
| 66 |
+
render_button.click(
|
| 67 |
+
fn=render_manim_video,
|
| 68 |
+
inputs=[],
|
| 69 |
+
outputs=[output_video]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# --- Launch the App ---
|
| 73 |
+
demo.launch(server_name="0.0.0.0", share=True)
|