manim / app.py
tommy24's picture
Create app.py
b390c67 verified
raw
history blame
916 Bytes
from fastapi import FastAPI
from fastapi.responses import FileResponse
import os
import subprocess
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Welcome to the Manim API on HuggingFace!"}
@app.get("/generate")
def generate_animation():
# Define file paths
output_dir = "media"
output_file = os.path.join(output_dir, "ExampleScene.mp4")
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Run the Manim command
subprocess.run([
"manim",
"-pql", "example_scene.py", # 'l' for low quality (quick render)
"ExampleScene",
"--media_dir", output_dir
])
# Check if the file exists and return
if os.path.exists(output_file):
return FileResponse(output_file, media_type="video/mp4", filename="example_scene.mp4")
else:
return {"error": "Animation generation failed!"}