File size: 3,871 Bytes
922a0d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b390c67
922a0d6
b390c67
7cb600e
922a0d6
 
b390c67
 
 
922a0d6
 
 
 
b390c67
 
 
 
 
 
7cb600e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b390c67
7cb600e
 
 
 
 
922a0d6
7cb600e
 
 
922a0d6
 
 
 
 
 
 
 
 
 
 
b390c67
922a0d6
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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."}


from fastapi import FastAPI
from fastapi.responses import JSONResponse
import os
from manim import *
from pyuploadcare import Uploadcare
import shutil

app = FastAPI()

UPLOADCARE_PUBLIC_KEY = os.environ.get("key")
UPLOADCARE_SECRET_KEY = os.environ.get("secret")
uploadcare = Uploadcare(public_key=UPLOADCARE_PUBLIC_KEY, secret_key=UPLOADCARE_SECRET_KEY)

@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 saves as .mp4)
    video_path = os.path.join(output_dir, "media", "videos", "PointMovingOnShapes", "1080p60", "PointMovingOnShapes.mp4")

    if os.path.exists(video_path):
        try:
            # Upload the video file to Uploadcare
            uploaded_file = uploadcare.upload(video_path)
            
            # Get the CDN URL of the uploaded file
            file_url = uploaded_file.cdn_url

            # Return the URL as a JSON response
            return JSONResponse(content={"file_url": file_url})
        except Exception as e:
            return JSONResponse(content={"error": f"Upload failed: {str(e)}"}, status_code=500)
    else:
        return JSONResponse(content={"message": "Error: Animation rendering failed."}, status_code=500)