theminji commited on
Commit
2f815c6
·
verified ·
1 Parent(s): 03fa3c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -27,11 +27,12 @@ def index():
27
  if not prompt:
28
  return render_template("index.html")
29
 
30
- max_retries = 3
31
  attempt = 0
32
  last_error = None
33
  while attempt < max_retries:
34
  try:
 
35
  ai_response = client.models.generate_content(
36
  model="gemini-2.0-flash-lite-preview-02-05",
37
  contents=f"""You are 'Manimator', an expert Manim animator and coder.
@@ -41,28 +42,31 @@ Plan out in chain of thought what you are going to do first, then give the final
41
  Make sure to not use external images or resources other than default Manim, however you can use numpy or other default libraries.
42
  Keep the scene uncluttered and aesthetically pleasing.
43
  Make sure things are not overlapping unless explicitly stated otherwise.
44
- Make sure to keep things consistant with colors and also make sure to really plan out the scene so you get it right first try.
45
- It is crucial that the code works first try.
46
  You got this!! <3
47
  """
48
  )
49
 
 
50
  pattern = r"```python\s*(.*?)\s*```"
51
  match = re.search(pattern, ai_response.text, re.DOTALL)
52
  if not match:
53
  raise Exception("No python code block found in the AI response.")
54
  code = match.group(1)
55
 
 
56
  scene_match = re.search(r"class\s+(\w+)\(.*Scene.*\):", code)
57
  scene_name = scene_match.group(1) if scene_match else "MyScene"
58
 
 
59
  code_filename = f"generated_video_{uuid.uuid4().hex}.py"
60
  video_filename = f"output_video_{uuid.uuid4().hex}.mp4"
61
 
 
62
  code_filepath = os.path.join("/tmp", code_filename)
63
  with open(code_filepath, "w") as f:
64
  f.write(code)
65
 
 
66
  cmd = [
67
  "manim",
68
  "-qm",
@@ -73,23 +77,24 @@ You got this!! <3
73
  ]
74
  subprocess.run(cmd, check=True, capture_output=True, text=True)
75
 
 
76
  expected_dir = os.path.join(media_dir, "videos", code_filename.replace(".py", ""), "720p30")
77
  video_path_in_media = os.path.join(expected_dir, video_filename)
78
  if not os.path.exists(video_path_in_media):
79
  raise Exception(f"Manim did not produce the expected output file at {video_path_in_media}")
80
 
 
81
  tmp_video_path = os.path.join("/tmp", video_filename)
82
  shutil.move(video_path_in_media, tmp_video_path)
 
83
 
84
- # The code file is already at code_filepath in /tmp.
85
- tmp_code_path = code_filepath
86
-
87
  def remove_files():
88
  try:
89
  if os.path.exists(tmp_video_path):
90
  os.remove(tmp_video_path)
91
- if os.path.exists(tmp_code_path):
92
- os.remove(tmp_code_path)
93
  except Exception as e:
94
  app.logger.error("Error removing files: %s", e)
95
  Timer(600, remove_files).start()
@@ -98,6 +103,7 @@ You got this!! <3
98
  return render_template("result.html", video_url=video_url)
99
 
100
  except Exception as e:
 
101
  last_error = e
102
  attempt += 1
103
  time.sleep(1)
 
27
  if not prompt:
28
  return render_template("index.html")
29
 
30
+ max_retries = 2
31
  attempt = 0
32
  last_error = None
33
  while attempt < max_retries:
34
  try:
35
+ # Call the GenAI API to get the Manim code
36
  ai_response = client.models.generate_content(
37
  model="gemini-2.0-flash-lite-preview-02-05",
38
  contents=f"""You are 'Manimator', an expert Manim animator and coder.
 
42
  Make sure to not use external images or resources other than default Manim, however you can use numpy or other default libraries.
43
  Keep the scene uncluttered and aesthetically pleasing.
44
  Make sure things are not overlapping unless explicitly stated otherwise.
 
 
45
  You got this!! <3
46
  """
47
  )
48
 
49
+ # Extract the Python code block from the AI response
50
  pattern = r"```python\s*(.*?)\s*```"
51
  match = re.search(pattern, ai_response.text, re.DOTALL)
52
  if not match:
53
  raise Exception("No python code block found in the AI response.")
54
  code = match.group(1)
55
 
56
+ # Determine the scene class name from the generated code
57
  scene_match = re.search(r"class\s+(\w+)\(.*Scene.*\):", code)
58
  scene_name = scene_match.group(1) if scene_match else "MyScene"
59
 
60
+ # Generate randomized filenames for the generated code and video
61
  code_filename = f"generated_video_{uuid.uuid4().hex}.py"
62
  video_filename = f"output_video_{uuid.uuid4().hex}.mp4"
63
 
64
+ # Write the generated code file directly to /tmp
65
  code_filepath = os.path.join("/tmp", code_filename)
66
  with open(code_filepath, "w") as f:
67
  f.write(code)
68
 
69
+ # Prepare the Manim command with --media_dir flag
70
  cmd = [
71
  "manim",
72
  "-qm",
 
77
  ]
78
  subprocess.run(cmd, check=True, capture_output=True, text=True)
79
 
80
+ # Construct the expected output path from Manim.
81
  expected_dir = os.path.join(media_dir, "videos", code_filename.replace(".py", ""), "720p30")
82
  video_path_in_media = os.path.join(expected_dir, video_filename)
83
  if not os.path.exists(video_path_in_media):
84
  raise Exception(f"Manim did not produce the expected output file at {video_path_in_media}")
85
 
86
+ # Move the video file to /tmp (to serve it from there)
87
  tmp_video_path = os.path.join("/tmp", video_filename)
88
  shutil.move(video_path_in_media, tmp_video_path)
89
+ # The code file is already in /tmp (code_filepath)
90
 
91
+ # Schedule deletion of both files after 10 minutes (600 seconds)
 
 
92
  def remove_files():
93
  try:
94
  if os.path.exists(tmp_video_path):
95
  os.remove(tmp_video_path)
96
+ if os.path.exists(code_filepath):
97
+ os.remove(code_filepath)
98
  except Exception as e:
99
  app.logger.error("Error removing files: %s", e)
100
  Timer(600, remove_files).start()
 
103
  return render_template("result.html", video_url=video_url)
104
 
105
  except Exception as e:
106
+ app.logger.error("Attempt %d failed: %s", attempt + 1, e)
107
  last_error = e
108
  attempt += 1
109
  time.sleep(1)