Spaces:
Configuration error
Configuration error
Commit ·
3b0ee09
1
Parent(s): bd8d357
Add memory optimization: low quality rendering + automatic cleanup
Browse files- backend/runner.py +71 -8
backend/runner.py
CHANGED
|
@@ -1,40 +1,103 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Get the directory of the current script (backend/)
|
| 6 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
MEDIA_DIR = os.path.join(BASE_DIR, "media")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
async def render_scene(code: str):
|
|
|
|
|
|
|
|
|
|
| 10 |
# Create a unique ID for this run
|
| 11 |
run_id = str(uuid.uuid4())
|
| 12 |
filename = f"scene_{run_id}.py"
|
| 13 |
|
| 14 |
# Save code to file in the backend directory
|
| 15 |
filepath = os.path.join(BASE_DIR, filename)
|
| 16 |
-
with open(filepath, "w") as f:
|
| 17 |
f.write(code)
|
| 18 |
|
| 19 |
-
# Run Manim
|
| 20 |
-
#
|
| 21 |
|
| 22 |
-
cmd = ["manim", "-
|
| 23 |
|
| 24 |
try:
|
| 25 |
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
| 26 |
-
print("Manim
|
| 27 |
except subprocess.CalledProcessError as e:
|
| 28 |
-
print("Manim Error:", e.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
raise Exception(f"Manim failed: {e.stderr}")
|
| 30 |
|
| 31 |
# Construct expected output path
|
| 32 |
-
#
|
| 33 |
|
| 34 |
video_folder = filename.replace(".py", "")
|
| 35 |
-
video_path = os.path.join(MEDIA_DIR, "videos", video_folder, "
|
| 36 |
|
| 37 |
if not os.path.exists(video_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
raise Exception(f"Video file not found at {video_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
return video_path
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
| 4 |
+
import shutil
|
| 5 |
+
import glob
|
| 6 |
+
from pathlib import Path
|
| 7 |
|
| 8 |
# Get the directory of the current script (backend/)
|
| 9 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
MEDIA_DIR = os.path.join(BASE_DIR, "media")
|
| 11 |
|
| 12 |
+
def cleanup_old_files():
|
| 13 |
+
"""Clean up old scene files and media to free memory"""
|
| 14 |
+
try:
|
| 15 |
+
# Remove old scene files (keep only last 2)
|
| 16 |
+
scene_files = sorted(glob.glob(os.path.join(BASE_DIR, "scene_*.py")), key=os.path.getmtime)
|
| 17 |
+
for old_file in scene_files[:-2]:
|
| 18 |
+
try:
|
| 19 |
+
os.remove(old_file)
|
| 20 |
+
print(f"🗑️ Cleaned up old scene file: {old_file}")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"⚠️ Could not remove {old_file}: {e}")
|
| 23 |
+
|
| 24 |
+
# Remove old video folders (keep only last 3)
|
| 25 |
+
videos_dir = os.path.join(MEDIA_DIR, "videos")
|
| 26 |
+
if os.path.exists(videos_dir):
|
| 27 |
+
video_folders = sorted(
|
| 28 |
+
[f for f in Path(videos_dir).iterdir() if f.is_dir()],
|
| 29 |
+
key=os.path.getmtime
|
| 30 |
+
)
|
| 31 |
+
for old_folder in video_folders[:-3]:
|
| 32 |
+
try:
|
| 33 |
+
shutil.rmtree(old_folder)
|
| 34 |
+
print(f"🗑️ Cleaned up old video folder: {old_folder}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"⚠️ Could not remove {old_folder}: {e}")
|
| 37 |
+
|
| 38 |
+
# Remove old audio files (keep only last 5)
|
| 39 |
+
audio_files = sorted(glob.glob(os.path.join(BASE_DIR, "step_*_narration.mp3")), key=os.path.getmtime)
|
| 40 |
+
for old_audio in audio_files[:-5]:
|
| 41 |
+
try:
|
| 42 |
+
os.remove(old_audio)
|
| 43 |
+
print(f"🗑️ Cleaned up old audio file: {old_audio}")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"⚠️ Could not remove {old_audio}: {e}")
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"⚠️ Cleanup error: {e}")
|
| 49 |
+
|
| 50 |
async def render_scene(code: str):
|
| 51 |
+
# Clean up old files before rendering
|
| 52 |
+
cleanup_old_files()
|
| 53 |
+
|
| 54 |
# Create a unique ID for this run
|
| 55 |
run_id = str(uuid.uuid4())
|
| 56 |
filename = f"scene_{run_id}.py"
|
| 57 |
|
| 58 |
# Save code to file in the backend directory
|
| 59 |
filepath = os.path.join(BASE_DIR, filename)
|
| 60 |
+
with open(filepath, "w", encoding='utf-8') as f:
|
| 61 |
f.write(code)
|
| 62 |
|
| 63 |
+
# Run Manim with LOW quality (-ql) for better memory usage
|
| 64 |
+
# -ql = 480p15, much lighter than -qm (720p30)
|
| 65 |
|
| 66 |
+
cmd = ["manim", "-ql", "--media_dir", MEDIA_DIR, filepath, "GenScene"]
|
| 67 |
|
| 68 |
try:
|
| 69 |
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
| 70 |
+
print("✓ Manim render completed")
|
| 71 |
except subprocess.CalledProcessError as e:
|
| 72 |
+
print("❌ Manim Error:", e.stderr)
|
| 73 |
+
# Clean up failed scene file
|
| 74 |
+
try:
|
| 75 |
+
os.remove(filepath)
|
| 76 |
+
except:
|
| 77 |
+
pass
|
| 78 |
raise Exception(f"Manim failed: {e.stderr}")
|
| 79 |
|
| 80 |
# Construct expected output path
|
| 81 |
+
# Low quality: {MEDIA_DIR}/videos/{filename_without_extension}/480p15/GenScene.mp4
|
| 82 |
|
| 83 |
video_folder = filename.replace(".py", "")
|
| 84 |
+
video_path = os.path.join(MEDIA_DIR, "videos", video_folder, "480p15", "GenScene.mp4")
|
| 85 |
|
| 86 |
if not os.path.exists(video_path):
|
| 87 |
+
# Clean up scene file
|
| 88 |
+
try:
|
| 89 |
+
os.remove(filepath)
|
| 90 |
+
except:
|
| 91 |
+
pass
|
| 92 |
raise Exception(f"Video file not found at {video_path}")
|
| 93 |
+
|
| 94 |
+
print(f"✓ Video created: {video_path}")
|
| 95 |
+
|
| 96 |
+
# Clean up the scene file immediately after successful render
|
| 97 |
+
try:
|
| 98 |
+
os.remove(filepath)
|
| 99 |
+
print(f"🗑️ Cleaned up scene file: {filepath}")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"⚠️ Could not remove scene file: {e}")
|
| 102 |
|
| 103 |
return video_path
|