Patrick Rathje
commited on
Commit
·
b89b569
0
Parent(s):
initial commit
Browse fileswip
wip
wip
do not use strict cors
wip
wip
wip build logs
wip
- .gitignore +1 -0
- Dockerfile +44 -0
- README.md +14 -0
- app.py +112 -0
- docker/vite.config.ts +17 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/.DS_Store
|
Dockerfile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Install Node to run Motion Canvas
|
| 4 |
+
WORKDIR /tmp/node
|
| 5 |
+
RUN apt-get update && apt-get install -y nodejs npm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# without v prefix!
|
| 9 |
+
ARG MC_VERSION="3.17.2"
|
| 10 |
+
ENV NODE_ENV=$MC_VERSION
|
| 11 |
+
|
| 12 |
+
WORKDIR /motion-canvas
|
| 13 |
+
|
| 14 |
+
RUN yes '' | npm init -y @motion-canvas@$MC_VERSION
|
| 15 |
+
|
| 16 |
+
WORKDIR /motion-canvas/my-animation
|
| 17 |
+
ENV MC_PROJECT_DIR=/motion-canvas/my-animation
|
| 18 |
+
|
| 19 |
+
# copy the vite.config.ts file to the project to get deterministic output filenames under dist/project.js
|
| 20 |
+
COPY docker/vite.config.ts ./vite.config.ts
|
| 21 |
+
|
| 22 |
+
# if we build here, we have to clean up the project every time before building a new one...
|
| 23 |
+
#RUN npm install
|
| 24 |
+
#RUN npm run build
|
| 25 |
+
|
| 26 |
+
# clone examples
|
| 27 |
+
ENV MC_EXAMPLE_DIR=/motion-canvas/examples
|
| 28 |
+
RUN git clone https://github.com/motion-canvas/examples /motion-canvas/examples
|
| 29 |
+
|
| 30 |
+
# docs/docs and docs/api should be interesting resources
|
| 31 |
+
ENV MC_DOCS_DIR=/motion-canvas/docs
|
| 32 |
+
RUN git clone https://github.com/motion-canvas/motion-canvas.github.io /motion-canvas/docs
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Install Gradio and copy local files
|
| 36 |
+
WORKDIR /app
|
| 37 |
+
RUN pip install --no-cache-dir gradio[mcp] gradio_motioncanvasplayer
|
| 38 |
+
COPY . .
|
| 39 |
+
|
| 40 |
+
RUN mkdir -p /app/public
|
| 41 |
+
EXPOSE 7860
|
| 42 |
+
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
| 43 |
+
|
| 44 |
+
CMD ["python", "app.py"]
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Run
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
```bash
|
| 5 |
+
docker build -t mc-gradio .
|
| 6 |
+
docker run -p 7860:7860 mc-gradio
|
| 7 |
+
```
|
| 8 |
+
|
| 9 |
+
The Motion Canvas Gradio app will be available under *http://localhost:7860*
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
## TODO: Multimodal ChatInterface?
|
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import uuid
|
| 6 |
+
import subprocess
|
| 7 |
+
from threading import Timer
|
| 8 |
+
|
| 9 |
+
from gradio_motioncanvasplayer import MotionCanvasPlayer
|
| 10 |
+
|
| 11 |
+
example_project_path = "https://prathje-gradio-motioncanvasplayer.hf.space/gradio_api/file=/home/user/app/public/project-3.17.2.js"
|
| 12 |
+
|
| 13 |
+
loading_project_path = ""
|
| 14 |
+
failed_project_path = ""
|
| 15 |
+
|
| 16 |
+
BUILD_TIMEOUT = 60
|
| 17 |
+
|
| 18 |
+
gr.set_static_paths(paths=[os.path.join(os.path.dirname(__file__), "public")])
|
| 19 |
+
|
| 20 |
+
def get_local_path(project_id):
|
| 21 |
+
return os.path.join(os.path.dirname(__file__), "public", "project-" + project_id + ".js")
|
| 22 |
+
|
| 23 |
+
def get_public_path(project_id):
|
| 24 |
+
return "/gradio_api/file=" + get_local_path(project_id)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def build_project(code):
|
| 28 |
+
# TODO: as soon as gradio supports states, we should keep the project_id for some time...
|
| 29 |
+
yield loading_project_path, "Preparing project...",
|
| 30 |
+
|
| 31 |
+
# generate a random uuid for the project
|
| 32 |
+
project_id = str(uuid.uuid4())
|
| 33 |
+
|
| 34 |
+
tmp_dir = os.path.join("/tmp/", project_id)
|
| 35 |
+
|
| 36 |
+
shutil.copytree(os.environ['MC_PROJECT_DIR'], tmp_dir, dirs_exist_ok=False)
|
| 37 |
+
acc_logs = ""
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
yield loading_project_path, "Building project...",
|
| 41 |
+
|
| 42 |
+
process = subprocess.Popen(
|
| 43 |
+
"npm install && npm run build",
|
| 44 |
+
stdout=subprocess.PIPE,
|
| 45 |
+
stderr=subprocess.PIPE,
|
| 46 |
+
text=True,
|
| 47 |
+
shell=True,
|
| 48 |
+
cwd=tmp_dir
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
timer = Timer(BUILD_TIMEOUT, process.kill)
|
| 52 |
+
timer.start()
|
| 53 |
+
|
| 54 |
+
while True:
|
| 55 |
+
line = process.stdout.readline()
|
| 56 |
+
if line:
|
| 57 |
+
acc_logs += line.rstrip() + "\n"
|
| 58 |
+
yield loading_project_path, acc_logs
|
| 59 |
+
elif process.poll() is not None:
|
| 60 |
+
break
|
| 61 |
+
timer.cancel()
|
| 62 |
+
|
| 63 |
+
# Check for errors
|
| 64 |
+
stderr_output = process.stderr.read()
|
| 65 |
+
if stderr_output:
|
| 66 |
+
acc_logs += "\n" + "Error building project: " + stderr_output
|
| 67 |
+
|
| 68 |
+
# check if the build was successful
|
| 69 |
+
if process.returncode != 0:
|
| 70 |
+
yield failed_project_path, acc_logs
|
| 71 |
+
else:
|
| 72 |
+
# copy dist/project.js to get_local_path(id)
|
| 73 |
+
shutil.copy(os.path.join(tmp_dir, "dist", "project.js"), get_local_path(project_id))
|
| 74 |
+
yield get_public_path(project_id), acc_logs
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
yield failed_project_path, acc_logs + "\n" + "Error building project: " + str(e)
|
| 78 |
+
|
| 79 |
+
finally:
|
| 80 |
+
# cleanup tmp dir
|
| 81 |
+
shutil.rmtree(tmp_dir)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# generate a random uuid for the project
|
| 86 |
+
# copy whole MC_PROJECT_DIR to a random tmp dir
|
| 87 |
+
# write code to scene.ts
|
| 88 |
+
# run npm run build
|
| 89 |
+
# return [logs, project_api_path]
|
| 90 |
+
# if successful copy MC_PROJECT_DIR/dist/project.js to get_local_path(id)
|
| 91 |
+
# always: log the output to logs
|
| 92 |
+
# always delete the tmp dir after the build is done
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
with gr.Blocks() as demo:
|
| 97 |
+
gr.Markdown("# Motion Canvas MCP Server")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column():
|
| 100 |
+
gr.Markdown("## TS Input for scene.ts")
|
| 101 |
+
code = gr.Code(language="typescript")
|
| 102 |
+
submit = gr.Button("Build")
|
| 103 |
+
logs = gr.Textbox(value="", label="Build Logs", interactive=False)
|
| 104 |
+
with gr.Column():
|
| 105 |
+
gr.Markdown("## Preview")
|
| 106 |
+
player = MotionCanvasPlayer(example_project_path, auto=True, quality=0.5, width=1920, height=1080, variables="{}")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
submit.click(build_project, inputs=[code], outputs=[player, logs], api_name="build_project")
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
demo.launch(mcp_server=True, strict_cors=False)
|
docker/vite.config.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {defineConfig} from 'vite';
|
| 2 |
+
import motionCanvas from '@motion-canvas/vite-plugin';
|
| 3 |
+
import ffmpeg from '@motion-canvas/ffmpeg';
|
| 4 |
+
|
| 5 |
+
export default defineConfig({
|
| 6 |
+
plugins: [
|
| 7 |
+
motionCanvas(),
|
| 8 |
+
ffmpeg(),
|
| 9 |
+
],
|
| 10 |
+
build: {
|
| 11 |
+
rollupOptions: {
|
| 12 |
+
output: {
|
| 13 |
+
entryFileNames: 'project.js',
|
| 14 |
+
},
|
| 15 |
+
},
|
| 16 |
+
},
|
| 17 |
+
});
|