Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello, " + name + "!" * int(intensity)
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
)
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import subprocess
|
4 |
+
import uuid
|
5 |
|
6 |
+
MANIM_OUT_FOLDER= "/tmp"
|
|
|
7 |
|
8 |
+
# Function to run the manim command and return the output video file path
|
9 |
+
def run_manim(source_code, main_class):
|
10 |
+
# Generate a unique id
|
11 |
+
run_id = str(uuid.uuid4())
|
|
|
12 |
|
13 |
+
input_file_path = f"/tmp/{run_id}.py"
|
14 |
+
output_file_name = run_id + ".mp4"
|
15 |
+
output_file_path = os.path.join(MANIM_OUT_FOLDER, output_file_name)
|
16 |
+
|
17 |
+
# Save the source code to a temporary file
|
18 |
+
with open(input_file_path, "w") as f:
|
19 |
+
f.write(source_code)
|
20 |
+
|
21 |
+
# Run the manim command
|
22 |
+
command = f"manim render -ql -o {output_file_name} --media_dir {MANIM_OUT_FOLDER} {input_file_path} {main_class}"
|
23 |
+
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
24 |
+
print("Return code:", result.returncode)
|
25 |
+
print("Output:", result.stdout)
|
26 |
+
print("Error:", result.stderr)
|
27 |
+
|
28 |
+
# Return the output video file path
|
29 |
+
return output_file_path
|
30 |
+
|
31 |
+
# Gradio Interface
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
# Input components
|
34 |
+
source_code_input = gr.Code(label="Source Code", language="python")
|
35 |
+
main_class_input = gr.Textbox(label="Main Class")
|
36 |
+
|
37 |
+
# Output component
|
38 |
+
video_output = gr.Video(label="Output Video", streaming=False, autoplay=True)
|
39 |
+
|
40 |
+
# Button to trigger the manim command
|
41 |
+
run_button = gr.Button("Run Manim")
|
42 |
+
|
43 |
+
# Event listener to run the manim command and update the video output
|
44 |
+
run_button.click(fn=run_manim, inputs=[source_code_input, main_class_input], outputs=video_output)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
demo.launch(show_error=True)
|