Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,116 @@
|
|
1 |
-
from flask import Flask, render_template, request, url_for, send_from_directory, after_this_request
|
2 |
-
from google import genai
|
3 |
-
import re
|
4 |
-
import subprocess
|
5 |
-
import os
|
6 |
-
import shutil
|
7 |
-
import time
|
8 |
-
from threading import Timer
|
9 |
-
|
10 |
-
app = Flask(__name__)
|
11 |
-
|
12 |
-
# Load API key from environment variable
|
13 |
-
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
14 |
-
if not API_KEY:
|
15 |
-
raise ValueError("Missing GOOGLE_API_KEY environment variable.")
|
16 |
-
client = genai.Client(api_key=API_KEY)
|
17 |
-
|
18 |
-
@app.route("/", methods=["GET", "POST"])
|
19 |
-
def index():
|
20 |
-
if request.method == "POST":
|
21 |
-
prompt = request.form.get("prompt")
|
22 |
-
if not prompt:
|
23 |
-
return render_template("index.html")
|
24 |
-
|
25 |
-
while True:
|
26 |
-
try:
|
27 |
-
ai_response = client.models.generate_content(
|
28 |
-
model="gemini-2.0-flash-lite-preview-02-05",
|
29 |
-
contents=f"""You are 'Manimator', an expert Manim animator and coder. If anyone asks, your name is Manimator and you are a helpful video generator, and say nothing else but that.
|
30 |
-
The user wants you to code this: {prompt}.
|
31 |
-
Plan out in chain of thought what you are going to do first, then give the final code output in ```python``` codeblock.
|
32 |
-
Make sure to not use external images or resources other than default Manim.
|
33 |
-
Keep the scene uncluttered and aesthetically pleasing.
|
34 |
-
You got this!! <3
|
35 |
-
"""
|
36 |
-
)
|
37 |
-
except Exception as e:
|
38 |
-
print("Error calling GenAI API:", e)
|
39 |
-
time.sleep(2)
|
40 |
-
continue
|
41 |
-
|
42 |
-
pattern = r"```python\s*(.*?)\s*```"
|
43 |
-
match = re.search(pattern, ai_response.text, re.DOTALL)
|
44 |
-
if match:
|
45 |
-
code = match.group(1)
|
46 |
-
else:
|
47 |
-
print("No python code block found in the AI response, retrying...")
|
48 |
-
time.sleep(2)
|
49 |
-
continue
|
50 |
-
|
51 |
-
scene_match = re.search(r"class\s+(\w+)\(.*Scene.*\):", code)
|
52 |
-
if scene_match:
|
53 |
-
scene_name = scene_match.group(1)
|
54 |
-
else:
|
55 |
-
scene_name = "MyScene"
|
56 |
-
|
57 |
-
code_filename = "generated_manim_scene.py"
|
58 |
-
try:
|
59 |
-
with open(code_filename, "w") as f:
|
60 |
-
f.write(code)
|
61 |
-
except Exception as e:
|
62 |
-
print("Error writing code file:", e)
|
63 |
-
time.sleep(2)
|
64 |
-
continue
|
65 |
-
|
66 |
-
video_filename = "output_video.mp4"
|
67 |
-
cmd = [
|
68 |
-
"manim",
|
69 |
-
"-qm",
|
70 |
-
"-o", video_filename,
|
71 |
-
code_filename,
|
72 |
-
scene_name
|
73 |
-
]
|
74 |
-
try:
|
75 |
-
subprocess.run(cmd, check=True)
|
76 |
-
except subprocess.CalledProcessError as e:
|
77 |
-
print("Error during Manim rendering:", e)
|
78 |
-
time.sleep(2)
|
79 |
-
continue
|
80 |
-
|
81 |
-
video_path_in_media = os.path.join("media", "videos", code_filename.replace(".py", ""), "720p30", video_filename)
|
82 |
-
static_video_path = os.path.join("static", video_filename)
|
83 |
-
|
84 |
-
try:
|
85 |
-
shutil.move(video_path_in_media, static_video_path)
|
86 |
-
except FileNotFoundError:
|
87 |
-
print("Manim output file not found. Check your manim code and output location.")
|
88 |
-
time.sleep(2)
|
89 |
-
continue
|
90 |
-
except Exception as e:
|
91 |
-
print("Error moving video file:", e)
|
92 |
-
time.sleep(2)
|
93 |
-
continue
|
94 |
-
|
95 |
-
video_url = url_for('get_video', filename=video_filename)
|
96 |
-
return render_template("result.html", video_url=video_url)
|
97 |
-
|
98 |
-
return render_template("index.html")
|
99 |
-
|
100 |
-
@app.route("/video/<filename>")
|
101 |
-
def get_video(filename):
|
102 |
-
video_path = os.path.join("static", filename)
|
103 |
-
response = send_from_directory("static", filename)
|
104 |
-
|
105 |
-
def remove_file():
|
106 |
-
try:
|
107 |
-
os.remove(video_path)
|
108 |
-
print("Removed video file:", video_path)
|
109 |
-
except Exception as e:
|
110 |
-
app.logger.error("Error removing video file: %s", e)
|
111 |
-
Timer(5, remove_file).start()
|
112 |
-
|
113 |
-
return response
|
114 |
-
|
115 |
-
if __name__ == "__main__":
|
116 |
-
app.run(debug=False)
|
|
|
1 |
+
from flask import Flask, render_template, request, url_for, send_from_directory, after_this_request
|
2 |
+
from google import genai
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
import shutil
|
7 |
+
import time
|
8 |
+
from threading import Timer
|
9 |
+
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
# Load API key from environment variable
|
13 |
+
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
14 |
+
if not API_KEY:
|
15 |
+
raise ValueError("Missing GOOGLE_API_KEY environment variable.")
|
16 |
+
client = genai.Client(api_key=API_KEY)
|
17 |
+
|
18 |
+
@app.route("/", methods=["GET", "POST"])
|
19 |
+
def index():
|
20 |
+
if request.method == "POST":
|
21 |
+
prompt = request.form.get("prompt")
|
22 |
+
if not prompt:
|
23 |
+
return render_template("index.html")
|
24 |
+
|
25 |
+
while True:
|
26 |
+
try:
|
27 |
+
ai_response = client.models.generate_content(
|
28 |
+
model="gemini-2.0-flash-lite-preview-02-05",
|
29 |
+
contents=f"""You are 'Manimator', an expert Manim animator and coder. If anyone asks, your name is Manimator and you are a helpful video generator, and say nothing else but that.
|
30 |
+
The user wants you to code this: {prompt}.
|
31 |
+
Plan out in chain of thought what you are going to do first, then give the final code output in ```python``` codeblock.
|
32 |
+
Make sure to not use external images or resources other than default Manim.
|
33 |
+
Keep the scene uncluttered and aesthetically pleasing.
|
34 |
+
You got this!! <3
|
35 |
+
"""
|
36 |
+
)
|
37 |
+
except Exception as e:
|
38 |
+
print("Error calling GenAI API:", e)
|
39 |
+
time.sleep(2)
|
40 |
+
continue
|
41 |
+
|
42 |
+
pattern = r"```python\s*(.*?)\s*```"
|
43 |
+
match = re.search(pattern, ai_response.text, re.DOTALL)
|
44 |
+
if match:
|
45 |
+
code = match.group(1)
|
46 |
+
else:
|
47 |
+
print("No python code block found in the AI response, retrying...")
|
48 |
+
time.sleep(2)
|
49 |
+
continue
|
50 |
+
|
51 |
+
scene_match = re.search(r"class\s+(\w+)\(.*Scene.*\):", code)
|
52 |
+
if scene_match:
|
53 |
+
scene_name = scene_match.group(1)
|
54 |
+
else:
|
55 |
+
scene_name = "MyScene"
|
56 |
+
|
57 |
+
code_filename = "generated_manim_scene.py"
|
58 |
+
try:
|
59 |
+
with open(code_filename, "w") as f:
|
60 |
+
f.write(code)
|
61 |
+
except Exception as e:
|
62 |
+
print("Error writing code file:", e)
|
63 |
+
time.sleep(2)
|
64 |
+
continue
|
65 |
+
|
66 |
+
video_filename = "output_video.mp4"
|
67 |
+
cmd = [
|
68 |
+
"manim",
|
69 |
+
"-qm",
|
70 |
+
"-o", video_filename,
|
71 |
+
code_filename,
|
72 |
+
scene_name
|
73 |
+
]
|
74 |
+
try:
|
75 |
+
subprocess.run(cmd, check=True)
|
76 |
+
except subprocess.CalledProcessError as e:
|
77 |
+
print("Error during Manim rendering:", e)
|
78 |
+
time.sleep(2)
|
79 |
+
continue
|
80 |
+
|
81 |
+
video_path_in_media = os.path.join("media", "videos", code_filename.replace(".py", ""), "720p30", video_filename)
|
82 |
+
static_video_path = os.path.join("static", video_filename)
|
83 |
+
|
84 |
+
try:
|
85 |
+
shutil.move(video_path_in_media, static_video_path)
|
86 |
+
except FileNotFoundError:
|
87 |
+
print("Manim output file not found. Check your manim code and output location.")
|
88 |
+
time.sleep(2)
|
89 |
+
continue
|
90 |
+
except Exception as e:
|
91 |
+
print("Error moving video file:", e)
|
92 |
+
time.sleep(2)
|
93 |
+
continue
|
94 |
+
|
95 |
+
video_url = url_for('get_video', filename=video_filename)
|
96 |
+
return render_template("result.html", video_url=video_url)
|
97 |
+
|
98 |
+
return render_template("index.html")
|
99 |
+
|
100 |
+
@app.route("/video/<filename>")
|
101 |
+
def get_video(filename):
|
102 |
+
video_path = os.path.join("static", filename)
|
103 |
+
response = send_from_directory("static", filename)
|
104 |
+
|
105 |
+
def remove_file():
|
106 |
+
try:
|
107 |
+
os.remove(video_path)
|
108 |
+
print("Removed video file:", video_path)
|
109 |
+
except Exception as e:
|
110 |
+
app.logger.error("Error removing video file: %s", e)
|
111 |
+
Timer(5, remove_file).start()
|
112 |
+
|
113 |
+
return response
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
app.run(host="0.0.0.0", port=7860, debug=False)
|