parthraninga commited on
Commit
1a34a56
·
verified ·
1 Parent(s): eb5564e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,56 +1,56 @@
1
  # --- Configuration ---
2
  MANIM_SCRIPT_FILE = "proof.py"
3
  MANIM_SCENE_NAME = "PythagoreanTheorem"
4
- BASE_DIR = "/tmp/media"
5
- OUTPUT_VIDEO_PATH = f"{BASE_DIR}/videos/proof/480p15/{MANIM_SCENE_NAME}.mp4"
6
 
7
  def render_manim_video():
8
  """
9
- Runs the Manim command and returns the path to the rendered video.
10
-
11
  """
12
- # 1. Clean up previous renders
13
- if os.path.exists(BASE_DIR):
14
- shutil.rmtree(BASE_DIR, ignore_errors=True)
15
 
16
- # 2. Make sure the output path exists
17
  os.makedirs(os.path.dirname(OUTPUT_VIDEO_PATH), exist_ok=True)
18
 
19
- # 3. Construct the Manim command
20
  command = [
21
  "manim",
22
  "-ql",
23
  ]
24
 
25
  try:
26
- # Run the command
27
  process = subprocess.run(
28
  command,
29
  check=True,
30
  )
31
  print("Manim STDOUT:", process.stdout)
32
 
33
- # 4. Move output video from default location to BASE_DIR
34
- default_output = f"media/videos/{MANIM_SCRIPT_FILE.replace('.py', '')}/480p15/{MANIM_SCENE_NAME}.mp4"
35
- if os.path.exists(default_output):
36
- shutil.move(default_output, OUTPUT_VIDEO_PATH)
37
  return OUTPUT_VIDEO_PATH
38
  else:
39
- raise gr.Error("Video was not created. Check Manim output.")
40
 
41
  except subprocess.CalledProcessError as e:
42
-
43
  print("Manim STDERR:", e.stderr)
44
  raise gr.Error(f"Manim failed to render. Error: {e.stderr}")
45
  except Exception as e:
46
- raise gr.Error(f"Unexpected error: {str(e)}")
47
 
48
- # --- Gradio UI ---
49
  with gr.Blocks() as demo:
50
  gr.Markdown("# Interactive Manim: Pythagorean Theorem Proof")
51
  gr.Markdown(
52
  outputs=[output_video]
53
  )
54
 
55
- # --- Launch ---
56
  demo.launch(server_name="0.0.0.0", share=True)
 
1
  # --- Configuration ---
2
  MANIM_SCRIPT_FILE = "proof.py"
3
  MANIM_SCENE_NAME = "PythagoreanTheorem"
4
+ OUTPUT_VIDEO_PATH = f"media/videos/proof/480p15/{MANIM_SCENE_NAME}.mp4"
5
+
6
 
7
  def render_manim_video():
8
  """
9
+ This function runs the Manim command to render the video.
10
+ It cleans up old media files and returns the path to the new video.
11
  """
12
+ # 1. Clean up previous renders to avoid errors
13
+ if os.path.exists("media"):
14
+ shutil.rmtree("media")
15
 
16
+ # 2. Ensure output directory exists
17
  os.makedirs(os.path.dirname(OUTPUT_VIDEO_PATH), exist_ok=True)
18
 
19
+ # 3. Construct and run the Manim command
20
  command = [
21
  "manim",
22
  "-ql",
23
  ]
24
 
25
  try:
26
+ # Execute the command
27
  process = subprocess.run(
28
  command,
29
  check=True,
30
  )
31
  print("Manim STDOUT:", process.stdout)
32
 
33
+ # 4. Return the path to the generated video
34
+ if os.path.exists(OUTPUT_VIDEO_PATH):
35
+
36
+
37
  return OUTPUT_VIDEO_PATH
38
  else:
39
+ raise gr.Error("Video file was not created. Check the Manim logs.")
40
 
41
  except subprocess.CalledProcessError as e:
42
+ # If the command fails, raise an error with the logs
43
  print("Manim STDERR:", e.stderr)
44
  raise gr.Error(f"Manim failed to render. Error: {e.stderr}")
45
  except Exception as e:
46
+ raise gr.Error(f"An unexpected error occurred: {str(e)}")
47
 
48
+ # --- Create the Gradio Interface ---
49
  with gr.Blocks() as demo:
50
  gr.Markdown("# Interactive Manim: Pythagorean Theorem Proof")
51
  gr.Markdown(
52
  outputs=[output_video]
53
  )
54
 
55
+ # --- Launch the App ---
56
  demo.launch(server_name="0.0.0.0", share=True)