manim / app.py
tommy24's picture
Update app.py
7cb600e verified
raw
history blame
1.58 kB
from fastapi import FastAPI
from fastapi.responses import FileResponse
import os
import subprocess
from manim import *
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Welcome to the Manim API on HuggingFace!"}
@app.get("/generate")
def generate_animation():
class PointMovingOnShapes(Scene):
def construct(self):
# Define the circle and dot
circle = Circle(radius=1, color=BLUE)
dot = Dot()
dot2 = dot.copy().shift(RIGHT)
self.add(dot)
# Create a line
line = Line([3, 0, 0], [5, 0, 0])
self.add(line)
# Animate the circle, dot, and line
self.play(GrowFromCenter(circle))
self.play(Transform(dot, dot2))
self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)
self.play(Rotating(dot, about_point=[2, 0, 0]), run_time=1.5)
self.wait()
# Directory to save rendered video
output_dir = "/tmp/manim_output"
os.makedirs(output_dir, exist_ok=True)
# Rendering the scene to a video file
scene = PointMovingOnShapes()
scene.render(directory=output_dir)
# Path to the rendered video file (manim by default saves as .mp4)
video_path = os.path.join(output_dir, "media", "videos", "PointMovingOnShapes", "1080p60", "PointMovingOnShapes.mp4")
# Return the video file as a response
if os.path.exists(video_path):
return FileResponse(video_path)
else:
return {"message": "Error: Animation rendering failed."}