|
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(): |
|
|
|
output_dir = "media" |
|
output_file = os.path.join(output_dir, "ExampleScene.mp4") |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
subprocess.run([ |
|
"manim", |
|
"-pql", "example_scene.py", |
|
"ExampleScene", |
|
"--media_dir", output_dir |
|
]) |
|
|
|
|
|
if os.path.exists(output_file): |
|
return FileResponse(output_file, media_type="video/mp4", filename="example_scene.mp4") |
|
else: |
|
return {"error": "Animation generation failed!"} |
|
|