Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ def run_manim_pre_check(code_str: str) -> (bool, str):
|
|
| 17 |
Returns (True, "Success") if the code is valid.
|
| 18 |
Returns (False, "Error Log") if any Python error (like NameError) occurs.
|
| 19 |
"""
|
| 20 |
-
print("
|
| 21 |
|
| 22 |
# Write the code to a temporary file for the pre-check
|
| 23 |
with open("scene_pre_check.py", "w", encoding="utf-8") as f:
|
|
@@ -39,26 +39,26 @@ def run_manim_pre_check(code_str: str) -> (bool, str):
|
|
| 39 |
process = subprocess.run(cmd, capture_output=True, timeout=30, check=False)
|
| 40 |
|
| 41 |
if process.returncode == 0:
|
| 42 |
-
print("
|
| 43 |
return True, "Pre-check successful."
|
| 44 |
else:
|
| 45 |
# An error occurred, capture the logs
|
| 46 |
stderr_log = process.stderr.decode('utf-8', 'ignore')
|
| 47 |
-
print(f"
|
| 48 |
# Format a clean error message for the user
|
| 49 |
-
error_message = f"
|
| 50 |
return False, error_message
|
| 51 |
|
| 52 |
except subprocess.TimeoutExpired:
|
| 53 |
-
print("
|
| 54 |
-
return False, "
|
| 55 |
|
| 56 |
def cleanup_media_directory():
|
| 57 |
"""Wipes the media directory to prevent caching issues."""
|
| 58 |
media_dir = 'media'
|
| 59 |
if os.path.exists(media_dir):
|
| 60 |
try: shutil.rmtree(media_dir)
|
| 61 |
-
except OSError as e: print(f"
|
| 62 |
|
| 63 |
def make_even(n):
|
| 64 |
n = int(n)
|
|
@@ -75,7 +75,7 @@ def get_resolution_flags(orientation, quality):
|
|
| 75 |
|
| 76 |
def scale_animation_times(code, factor):
|
| 77 |
"""Scales run_time and wait() calls."""
|
| 78 |
-
print(f"
|
| 79 |
MIN_RUN_TIME = 0.1
|
| 80 |
def scale(m, is_wait):
|
| 81 |
val = float(m.group(2)) * factor
|
|
@@ -86,7 +86,7 @@ def scale_animation_times(code, factor):
|
|
| 86 |
|
| 87 |
def run_manim(code_str, orientation, quality, timeout):
|
| 88 |
timeout_sec = float(timeout) if timeout and float(timeout) > 0 else None
|
| 89 |
-
print(f"
|
| 90 |
|
| 91 |
with open("scene.py", "w", encoding="utf-8") as f: f.write(code_str)
|
| 92 |
|
|
@@ -97,7 +97,7 @@ def run_manim(code_str, orientation, quality, timeout):
|
|
| 97 |
frame_rate_flags = ["--frame_rate", "15"] if quality == "Preview (360p)" else []
|
| 98 |
cmd = ["manim", "--resolution", res_str, *frame_rate_flags, "--disable_caching",
|
| 99 |
"--progress_bar", "none", "scene.py", "GenScene", "-o", output_filename]
|
| 100 |
-
print(f"
|
| 101 |
|
| 102 |
try:
|
| 103 |
process = subprocess.run(cmd, capture_output=True, timeout=timeout_sec, check=False)
|
|
@@ -105,22 +105,22 @@ def run_manim(code_str, orientation, quality, timeout):
|
|
| 105 |
full_logs = f"--- MANIM STDOUT ---\n{stdout_log}\n\n--- MANIM STDERR ---\n{stderr_log}"
|
| 106 |
|
| 107 |
if process.returncode != 0:
|
| 108 |
-
print(f"
|
| 109 |
-
return None, f"
|
| 110 |
except subprocess.TimeoutExpired as e:
|
| 111 |
-
print(f"
|
| 112 |
stdout_log, stderr_log = (e.stdout.decode('utf-8', 'ignore') if e.stdout else ""), (e.stderr.decode('utf-8', 'ignore') if e.stderr else "")
|
| 113 |
-
return None, f"
|
| 114 |
|
| 115 |
media_video_base = os.path.join("media", "videos", "scene")
|
| 116 |
if os.path.exists(media_video_base):
|
| 117 |
for root, _, files in os.walk(media_video_base):
|
| 118 |
if output_filename in files:
|
| 119 |
found_video_path = os.path.join(root, output_filename)
|
| 120 |
-
print(f"
|
| 121 |
-
return found_video_path, f"
|
| 122 |
|
| 123 |
-
print(f"
|
| 124 |
return None, f"Video file not created despite success code. Check logs:\n{full_logs}", False
|
| 125 |
|
| 126 |
# ---------------------------------------------------------
|
|
|
|
| 17 |
Returns (True, "Success") if the code is valid.
|
| 18 |
Returns (False, "Error Log") if any Python error (like NameError) occurs.
|
| 19 |
"""
|
| 20 |
+
print("🕵️ Running fast pre-check with 'manim -s'...", flush=True)
|
| 21 |
|
| 22 |
# Write the code to a temporary file for the pre-check
|
| 23 |
with open("scene_pre_check.py", "w", encoding="utf-8") as f:
|
|
|
|
| 39 |
process = subprocess.run(cmd, capture_output=True, timeout=30, check=False)
|
| 40 |
|
| 41 |
if process.returncode == 0:
|
| 42 |
+
print("✅ Pre-check passed. Code is valid.", flush=True)
|
| 43 |
return True, "Pre-check successful."
|
| 44 |
else:
|
| 45 |
# An error occurred, capture the logs
|
| 46 |
stderr_log = process.stderr.decode('utf-8', 'ignore')
|
| 47 |
+
print(f"❌ Pre-check failed.\n{stderr_log}", flush=True)
|
| 48 |
# Format a clean error message for the user
|
| 49 |
+
error_message = f"⚠️ ERROR: Your code failed the pre-check.\n\n--- ERROR LOG ---\n{stderr_log}"
|
| 50 |
return False, error_message
|
| 51 |
|
| 52 |
except subprocess.TimeoutExpired:
|
| 53 |
+
print("⌛ Pre-check timed out.", flush=True)
|
| 54 |
+
return False, "❌ ERROR: The pre-check timed out. The code may contain an infinite loop."
|
| 55 |
|
| 56 |
def cleanup_media_directory():
|
| 57 |
"""Wipes the media directory to prevent caching issues."""
|
| 58 |
media_dir = 'media'
|
| 59 |
if os.path.exists(media_dir):
|
| 60 |
try: shutil.rmtree(media_dir)
|
| 61 |
+
except OSError as e: print(f"⚠️ Warning during cleanup: {e}", flush=True)
|
| 62 |
|
| 63 |
def make_even(n):
|
| 64 |
n = int(n)
|
|
|
|
| 75 |
|
| 76 |
def scale_animation_times(code, factor):
|
| 77 |
"""Scales run_time and wait() calls."""
|
| 78 |
+
print(f"⚡ Scaling animation times by a factor of {factor} for preview.", flush=True)
|
| 79 |
MIN_RUN_TIME = 0.1
|
| 80 |
def scale(m, is_wait):
|
| 81 |
val = float(m.group(2)) * factor
|
|
|
|
| 86 |
|
| 87 |
def run_manim(code_str, orientation, quality, timeout):
|
| 88 |
timeout_sec = float(timeout) if timeout and float(timeout) > 0 else None
|
| 89 |
+
print(f"🎬 Starting Full Render: {orientation} @ {quality} (Timeout: {timeout_sec}s)...", flush=True)
|
| 90 |
|
| 91 |
with open("scene.py", "w", encoding="utf-8") as f: f.write(code_str)
|
| 92 |
|
|
|
|
| 97 |
frame_rate_flags = ["--frame_rate", "15"] if quality == "Preview (360p)" else []
|
| 98 |
cmd = ["manim", "--resolution", res_str, *frame_rate_flags, "--disable_caching",
|
| 99 |
"--progress_bar", "none", "scene.py", "GenScene", "-o", output_filename]
|
| 100 |
+
print(f"⚙️ Running command: {' '.join(cmd)}", flush=True)
|
| 101 |
|
| 102 |
try:
|
| 103 |
process = subprocess.run(cmd, capture_output=True, timeout=timeout_sec, check=False)
|
|
|
|
| 105 |
full_logs = f"--- MANIM STDOUT ---\n{stdout_log}\n\n--- MANIM STDERR ---\n{stderr_log}"
|
| 106 |
|
| 107 |
if process.returncode != 0:
|
| 108 |
+
print(f"❌ Render Failed.\n{stderr_log}", flush=True)
|
| 109 |
+
return None, f"⚠️ ERROR: Manim failed to render.\n{full_logs}", False
|
| 110 |
except subprocess.TimeoutExpired as e:
|
| 111 |
+
print(f"⌛ Render timed out after {timeout_sec} seconds.", flush=True)
|
| 112 |
stdout_log, stderr_log = (e.stdout.decode('utf-8', 'ignore') if e.stdout else ""), (e.stderr.decode('utf-8', 'ignore') if e.stderr else "")
|
| 113 |
+
return None, f"❌ ERROR: Timed out.\n--- MANIM STDOUT ---\n{stdout_log}\n\n--- MANIM STDERR ---\n{stderr_log}", False
|
| 114 |
|
| 115 |
media_video_base = os.path.join("media", "videos", "scene")
|
| 116 |
if os.path.exists(media_video_base):
|
| 117 |
for root, _, files in os.walk(media_video_base):
|
| 118 |
if output_filename in files:
|
| 119 |
found_video_path = os.path.join(root, output_filename)
|
| 120 |
+
print(f"✅ Video Render Success: {found_video_path}", flush=True)
|
| 121 |
+
return found_video_path, f"✅ Rendering Successful\n\n{full_logs}", True
|
| 122 |
|
| 123 |
+
print(f"❌ Final output file '{output_filename}' not found.", flush=True)
|
| 124 |
return None, f"Video file not created despite success code. Check logs:\n{full_logs}", False
|
| 125 |
|
| 126 |
# ---------------------------------------------------------
|