File size: 916 Bytes
b390c67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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!"}