parthraninga commited on
Commit
b9e56c5
·
verified ·
1 Parent(s): 9849f83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -5
app.py CHANGED
@@ -5,22 +5,47 @@ import shutil
5
  import tempfile
6
  import re
7
  import ast
 
 
 
8
 
9
  # --- Configuration ---
10
  BASE_DIR = "./media" # Use relative path in user home directory
11
 
12
  def ensure_permissions():
13
  """Ensure the base directory exists and is writable."""
14
- os.makedirs(BASE_DIR, exist_ok=True)
15
- # Try to create a test file to check permissions
16
- test_file = os.path.join(BASE_DIR, "test_write.txt")
17
  try:
 
 
 
 
 
18
  with open(test_file, 'w') as f:
19
  f.write("test")
20
  os.remove(test_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
- print(f"Warning: Permission issue with {BASE_DIR}: {e}")
23
- # Fallback to current directory
24
  global BASE_DIR
25
  BASE_DIR = "./media"
26
  os.makedirs(BASE_DIR, exist_ok=True)
@@ -43,6 +68,61 @@ def get_scene_class_name(code):
43
  match = re.search(pattern, code)
44
  return match.group(1) if match else None
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def render_manim_video(manim_code):
47
  """
48
  Runs the Manim command with dynamic code and returns the path to the rendered video.
@@ -266,4 +346,6 @@ with gr.Blocks(title="Manim Video Renderer", theme=gr.themes.Soft()) as demo:
266
 
267
  # --- Launch ---
268
  if __name__ == "__main__":
 
 
269
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
5
  import tempfile
6
  import re
7
  import ast
8
+ import sys
9
+ import time
10
+ from pathlib import Path
11
 
12
  # --- Configuration ---
13
  BASE_DIR = "./media" # Use relative path in user home directory
14
 
15
  def ensure_permissions():
16
  """Ensure the base directory exists and is writable."""
 
 
 
17
  try:
18
+ # Create directories with proper permissions
19
+ os.makedirs(BASE_DIR, exist_ok=True, mode=0o755)
20
+
21
+ # Try to create a test file to check permissions
22
+ test_file = os.path.join(BASE_DIR, "test_write.txt")
23
  with open(test_file, 'w') as f:
24
  f.write("test")
25
  os.remove(test_file)
26
+ print(f"✅ Directory {BASE_DIR} is writable")
27
+
28
+ except PermissionError as e:
29
+ print(f"❌ Permission denied for {BASE_DIR}: {e}")
30
+ # Try alternative directories
31
+ for alt_dir in ["./tmp_media", "/tmp/manim_render", "./output"]:
32
+ try:
33
+ global BASE_DIR
34
+ BASE_DIR = alt_dir
35
+ os.makedirs(BASE_DIR, exist_ok=True, mode=0o755)
36
+ test_file = os.path.join(BASE_DIR, "test_write.txt")
37
+ with open(test_file, 'w') as f:
38
+ f.write("test")
39
+ os.remove(test_file)
40
+ print(f"✅ Using alternative directory: {BASE_DIR}")
41
+ break
42
+ except:
43
+ continue
44
+ else:
45
+ raise gr.Error("Cannot create writable directory for video output")
46
  except Exception as e:
47
+ print(f"⚠️ Warning: Issue with directory setup: {e}")
48
+ # Last resort: current directory
49
  global BASE_DIR
50
  BASE_DIR = "./media"
51
  os.makedirs(BASE_DIR, exist_ok=True)
 
68
  match = re.search(pattern, code)
69
  return match.group(1) if match else None
70
 
71
+ def validate_manim_code(code):
72
+ """Validate Manim code for common issues."""
73
+ if not code.strip():
74
+ return False, "Code is empty."
75
+
76
+ # Check for Scene class
77
+ if not get_scene_class_name(code):
78
+ return False, "No Scene class found. Make sure your class inherits from Scene."
79
+
80
+ # Check for basic syntax
81
+ try:
82
+ ast.parse(code)
83
+ except SyntaxError as e:
84
+ return False, f"Syntax error: {e}"
85
+
86
+ # Check for required construct method
87
+ if 'def construct(' not in code:
88
+ return False, "Scene class must have a construct() method."
89
+
90
+ return True, "Code looks good!"
91
+
92
+ def check_manim_installation():
93
+ """Check if Manim is properly installed."""
94
+ try:
95
+ result = subprocess.run(
96
+ ["manim", "--version"],
97
+ capture_output=True,
98
+ text=True,
99
+ timeout=10
100
+ )
101
+ if result.returncode == 0:
102
+ return True, result.stdout.strip()
103
+ else:
104
+ return False, "Manim command failed"
105
+ except Exception as e:
106
+ return False, f"Manim not found: {e}"
107
+
108
+ def setup_environment():
109
+ """Set up the environment for Manim rendering."""
110
+ # Ensure permissions
111
+ ensure_permissions()
112
+
113
+ # Check Manim installation
114
+ manim_ok, manim_info = check_manim_installation()
115
+ if not manim_ok:
116
+ print(f"⚠️ Manim issue: {manim_info}")
117
+ else:
118
+ print(f"✅ {manim_info}")
119
+
120
+ # Set environment variables for better rendering
121
+ os.environ['MANIM_CONFIG_DIR'] = BASE_DIR
122
+ os.environ['MANIM_PLUGINS_DIR'] = os.path.join(BASE_DIR, 'plugins')
123
+
124
+ return manim_ok
125
+
126
  def render_manim_video(manim_code):
127
  """
128
  Runs the Manim command with dynamic code and returns the path to the rendered video.
 
346
 
347
  # --- Launch ---
348
  if __name__ == "__main__":
349
+ # Setup environment before launching
350
+ setup_environment()
351
  demo.launch(server_name="0.0.0.0", server_port=7860)