theminji commited on
Commit
212f1ce
·
verified ·
1 Parent(s): 1c0a656

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -17,7 +17,7 @@ if not API_KEY:
17
  raise ValueError("Missing GOOGLE_API_KEY environment variable.")
18
  client = genai.Client(api_key=API_KEY)
19
 
20
- # Define a dedicated media directory in /tmp
21
  media_dir = os.path.join("/tmp", "manim_media")
22
  os.makedirs(media_dir, exist_ok=True)
23
 
@@ -68,8 +68,9 @@ You got this!! <3
68
  code_filename = f"generated_video_{uuid.uuid4().hex}.py"
69
  video_filename = f"output_video_{uuid.uuid4().hex}.mp4"
70
 
71
- # Save the generated code to a file (in the current working directory)
72
- with open(code_filename, "w") as f:
 
73
  f.write(code)
74
 
75
  # Prepare the Manim command with --media_dir flag
@@ -78,7 +79,7 @@ You got this!! <3
78
  "-qm",
79
  "--media_dir", media_dir,
80
  "-o", video_filename,
81
- code_filename,
82
  scene_name
83
  ]
84
  print("Running Manim command:", " ".join(cmd))
@@ -88,31 +89,32 @@ You got this!! <3
88
  print("Manim stderr:", result.stderr)
89
  sys.stdout.flush()
90
 
91
- # Debug: list the media directory structure
92
  for root, dirs, files in os.walk(media_dir):
93
  print(root, dirs, files)
94
  sys.stdout.flush()
95
 
96
  # Construct the expected output path from Manim.
 
 
97
  expected_dir = os.path.join(media_dir, "videos", code_filename.replace(".py", ""), "720p30")
98
  video_path_in_media = os.path.join(expected_dir, video_filename)
99
  if not os.path.exists(video_path_in_media):
100
  raise Exception(f"Manim did not produce the expected output file at {video_path_in_media}")
101
 
102
- # Move both the video and the generated code file to /tmp
103
  tmp_video_path = os.path.join("/tmp", video_filename)
104
  shutil.move(video_path_in_media, tmp_video_path)
105
- tmp_code_path = os.path.join("/tmp", code_filename)
106
- shutil.move(code_filename, tmp_code_path)
107
 
108
  # Schedule deletion of both files after 10 minutes (600 seconds)
109
  def remove_files():
110
  try:
111
  if os.path.exists(tmp_video_path):
112
  os.remove(tmp_video_path)
113
- if os.path.exists(tmp_code_path):
114
- os.remove(tmp_code_path)
115
- print("Removed files:", tmp_video_path, tmp_code_path)
116
  sys.stdout.flush()
117
  except Exception as e:
118
  app.logger.error("Error removing files: %s", e)
@@ -134,6 +136,7 @@ You got this!! <3
134
 
135
  @app.route("/video/<filename>")
136
  def get_video(filename):
 
137
  return send_from_directory("/tmp", filename)
138
 
139
  if __name__ == "__main__":
 
17
  raise ValueError("Missing GOOGLE_API_KEY environment variable.")
18
  client = genai.Client(api_key=API_KEY)
19
 
20
+ # Define a dedicated media directory in /tmp for Manim output
21
  media_dir = os.path.join("/tmp", "manim_media")
22
  os.makedirs(media_dir, exist_ok=True)
23
 
 
68
  code_filename = f"generated_video_{uuid.uuid4().hex}.py"
69
  video_filename = f"output_video_{uuid.uuid4().hex}.mp4"
70
 
71
+ # Write the generated code file directly to /tmp
72
+ code_filepath = os.path.join("/tmp", code_filename)
73
+ with open(code_filepath, "w") as f:
74
  f.write(code)
75
 
76
  # Prepare the Manim command with --media_dir flag
 
79
  "-qm",
80
  "--media_dir", media_dir,
81
  "-o", video_filename,
82
+ code_filepath,
83
  scene_name
84
  ]
85
  print("Running Manim command:", " ".join(cmd))
 
89
  print("Manim stderr:", result.stderr)
90
  sys.stdout.flush()
91
 
92
+ # Debug: List the media directory structure
93
  for root, dirs, files in os.walk(media_dir):
94
  print(root, dirs, files)
95
  sys.stdout.flush()
96
 
97
  # Construct the expected output path from Manim.
98
+ # With --media_dir set, output should be in:
99
+ # {media_dir}/videos/<code_filename_without_.py>/720p30/<video_filename>
100
  expected_dir = os.path.join(media_dir, "videos", code_filename.replace(".py", ""), "720p30")
101
  video_path_in_media = os.path.join(expected_dir, video_filename)
102
  if not os.path.exists(video_path_in_media):
103
  raise Exception(f"Manim did not produce the expected output file at {video_path_in_media}")
104
 
105
+ # Move the video file to /tmp (it will be served from here)
106
  tmp_video_path = os.path.join("/tmp", video_filename)
107
  shutil.move(video_path_in_media, tmp_video_path)
108
+ # The code file is already in /tmp (at code_filepath)
 
109
 
110
  # Schedule deletion of both files after 10 minutes (600 seconds)
111
  def remove_files():
112
  try:
113
  if os.path.exists(tmp_video_path):
114
  os.remove(tmp_video_path)
115
+ if os.path.exists(code_filepath):
116
+ os.remove(code_filepath)
117
+ print("Removed files:", tmp_video_path, code_filepath)
118
  sys.stdout.flush()
119
  except Exception as e:
120
  app.logger.error("Error removing files: %s", e)
 
136
 
137
  @app.route("/video/<filename>")
138
  def get_video(filename):
139
+ # Serve the video file from /tmp
140
  return send_from_directory("/tmp", filename)
141
 
142
  if __name__ == "__main__":