Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
-
from fastapi.responses import
|
3 |
import os
|
4 |
-
import subprocess
|
5 |
from manim import *
|
|
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
|
|
|
|
|
|
|
|
9 |
@app.get("/")
|
10 |
def read_root():
|
11 |
return {"message": "Welcome to the Manim API on HuggingFace!"}
|
@@ -39,11 +95,21 @@ def generate_animation():
|
|
39 |
scene = PointMovingOnShapes()
|
40 |
scene.render(directory=output_dir)
|
41 |
|
42 |
-
# Path to the rendered video file (
|
43 |
video_path = os.path.join(output_dir, "media", "videos", "PointMovingOnShapes", "1080p60", "PointMovingOnShapes.mp4")
|
44 |
|
45 |
-
# Return the video file as a response
|
46 |
if os.path.exists(video_path):
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
else:
|
49 |
-
return {"message": "Error: Animation rendering failed."}
|
|
|
|
1 |
+
# from fastapi import FastAPI
|
2 |
+
# from fastapi.responses import FileResponse
|
3 |
+
# import os
|
4 |
+
# import subprocess
|
5 |
+
# from manim import *
|
6 |
+
|
7 |
+
# app = FastAPI()
|
8 |
+
|
9 |
+
# @app.get("/")
|
10 |
+
# def read_root():
|
11 |
+
# return {"message": "Welcome to the Manim API on HuggingFace!"}
|
12 |
+
|
13 |
+
# @app.get("/generate")
|
14 |
+
# def generate_animation():
|
15 |
+
# class PointMovingOnShapes(Scene):
|
16 |
+
# def construct(self):
|
17 |
+
# # Define the circle and dot
|
18 |
+
# circle = Circle(radius=1, color=BLUE)
|
19 |
+
# dot = Dot()
|
20 |
+
# dot2 = dot.copy().shift(RIGHT)
|
21 |
+
# self.add(dot)
|
22 |
+
|
23 |
+
# # Create a line
|
24 |
+
# line = Line([3, 0, 0], [5, 0, 0])
|
25 |
+
# self.add(line)
|
26 |
+
|
27 |
+
# # Animate the circle, dot, and line
|
28 |
+
# self.play(GrowFromCenter(circle))
|
29 |
+
# self.play(Transform(dot, dot2))
|
30 |
+
# self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)
|
31 |
+
# self.play(Rotating(dot, about_point=[2, 0, 0]), run_time=1.5)
|
32 |
+
# self.wait()
|
33 |
+
|
34 |
+
# # Directory to save rendered video
|
35 |
+
# output_dir = "/tmp/manim_output"
|
36 |
+
# os.makedirs(output_dir, exist_ok=True)
|
37 |
+
|
38 |
+
# # Rendering the scene to a video file
|
39 |
+
# scene = PointMovingOnShapes()
|
40 |
+
# scene.render(directory=output_dir)
|
41 |
+
|
42 |
+
# # Path to the rendered video file (manim by default saves as .mp4)
|
43 |
+
# video_path = os.path.join(output_dir, "media", "videos", "PointMovingOnShapes", "1080p60", "PointMovingOnShapes.mp4")
|
44 |
+
|
45 |
+
# # Return the video file as a response
|
46 |
+
# if os.path.exists(video_path):
|
47 |
+
# return FileResponse(video_path)
|
48 |
+
# else:
|
49 |
+
# return {"message": "Error: Animation rendering failed."}
|
50 |
+
|
51 |
+
|
52 |
from fastapi import FastAPI
|
53 |
+
from fastapi.responses import JSONResponse
|
54 |
import os
|
|
|
55 |
from manim import *
|
56 |
+
from pyuploadcare import Uploadcare
|
57 |
+
import shutil
|
58 |
|
59 |
app = FastAPI()
|
60 |
|
61 |
+
UPLOADCARE_PUBLIC_KEY = os.environ.get("key")
|
62 |
+
UPLOADCARE_SECRET_KEY = os.environ.get("secret")
|
63 |
+
uploadcare = Uploadcare(public_key=UPLOADCARE_PUBLIC_KEY, secret_key=UPLOADCARE_SECRET_KEY)
|
64 |
+
|
65 |
@app.get("/")
|
66 |
def read_root():
|
67 |
return {"message": "Welcome to the Manim API on HuggingFace!"}
|
|
|
95 |
scene = PointMovingOnShapes()
|
96 |
scene.render(directory=output_dir)
|
97 |
|
98 |
+
# Path to the rendered video file (Manim saves as .mp4)
|
99 |
video_path = os.path.join(output_dir, "media", "videos", "PointMovingOnShapes", "1080p60", "PointMovingOnShapes.mp4")
|
100 |
|
|
|
101 |
if os.path.exists(video_path):
|
102 |
+
try:
|
103 |
+
# Upload the video file to Uploadcare
|
104 |
+
uploaded_file = uploadcare.upload(video_path)
|
105 |
+
|
106 |
+
# Get the CDN URL of the uploaded file
|
107 |
+
file_url = uploaded_file.cdn_url
|
108 |
+
|
109 |
+
# Return the URL as a JSON response
|
110 |
+
return JSONResponse(content={"file_url": file_url})
|
111 |
+
except Exception as e:
|
112 |
+
return JSONResponse(content={"error": f"Upload failed: {str(e)}"}, status_code=500)
|
113 |
else:
|
114 |
+
return JSONResponse(content={"message": "Error: Animation rendering failed."}, status_code=500)
|
115 |
+
|