Spaces:
Sleeping
Sleeping
Patrick Rathje
commited on
Commit
·
4f181ef
1
Parent(s):
26a3c72
wip
Browse files
app.py
CHANGED
|
@@ -10,12 +10,13 @@ import time
|
|
| 10 |
|
| 11 |
from smolagents import ToolCollection, ToolCallingAgent, InferenceClientModel, EMPTY_PROMPT_TEMPLATES
|
| 12 |
|
| 13 |
-
MODEL = "
|
|
|
|
| 14 |
|
| 15 |
BUILD_SERVER_MCP_CONFIG = {"url": "https://prathje-gradio-motioncanvas-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"}
|
| 16 |
DOCS_SERVER_MCP_CONFIG = {"url": "https://prathje-gradio-motioncanvas-docs-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"}
|
| 17 |
|
| 18 |
-
SYSTEM_PROMPT = "You are a helpful assistant that generates motion canvas scenes. The user prompts his ideas. You should use the recursive_list to check for available classes and examples. You should generate the code for a single standalone motion canvas scene.tsx and build it using the build tool.
|
| 19 |
|
| 20 |
from smolagents import tool
|
| 21 |
|
|
@@ -31,7 +32,11 @@ def get_public_path(project_id):
|
|
| 31 |
|
| 32 |
BUILD_TIMEOUT=30
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
# In theory, we should be using the build server mcp, but it not working right now and we are running out of time ;)
|
|
|
|
| 35 |
@tool
|
| 36 |
def build_project(code: str) -> str:
|
| 37 |
"""Build a Motion Canvas project.
|
|
@@ -39,6 +44,14 @@ def build_project(code: str) -> str:
|
|
| 39 |
Args:
|
| 40 |
code: TypeScript code for the scene to build
|
| 41 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# generate a random uuid for the project
|
| 44 |
project_id = str(uuid.uuid4())
|
|
@@ -81,49 +94,52 @@ def build_project(code: str) -> str:
|
|
| 81 |
|
| 82 |
# check if the build was successful
|
| 83 |
if process.returncode != 0:
|
| 84 |
-
return "
|
| 85 |
else:
|
| 86 |
# copy dist/project.js to get_local_path(id)
|
| 87 |
shutil.copy(os.path.join(tmp_dir, "dist", "project.js"), get_local_path(project_id))
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
return "
|
| 92 |
|
| 93 |
finally:
|
| 94 |
# cleanup tmp dir
|
| 95 |
shutil.rmtree(tmp_dir)
|
| 96 |
|
| 97 |
all_tools = []
|
| 98 |
-
with ToolCollection.from_mcp(BUILD_SERVER_MCP_CONFIG, trust_remote_code=True) as build_tool_collection:
|
| 99 |
-
with ToolCollection.from_mcp(DOCS_SERVER_MCP_CONFIG, trust_remote_code=True) as docs_tool_collection:
|
| 100 |
-
all_tools = docs_tool_collection.tools + [build_project]
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
model = InferenceClientModel(model_id=MODEL)
|
| 104 |
|
| 105 |
-
agent = ToolCallingAgent(tools=[*all_tools], model=model, return_full_result=True)
|
| 106 |
|
| 107 |
agent.prompt_templates["system_prompt"] = SYSTEM_PROMPT
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
return "An error occurred while generating the code", "", ""
|
| 120 |
-
else:
|
| 121 |
-
print("No HF_TOKEN found, Zero GPU space not implemented")
|
| 122 |
-
def generate(message, history, code, logs):
|
| 123 |
-
return "LLM not available", "", ""
|
| 124 |
|
| 125 |
-
|
| 126 |
-
exit()
|
| 127 |
|
| 128 |
|
| 129 |
from gradio_motioncanvasplayer import MotionCanvasPlayer
|
|
@@ -139,7 +155,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
|
| 139 |
gr.Markdown("# Motion Canvas Agent")
|
| 140 |
gr.Markdown("Leverage the power of AI and Motion Canvas to create animations using TypeScript.")
|
| 141 |
|
| 142 |
-
player = MotionCanvasPlayer(example_project_path, auto=True, quality=0
|
| 143 |
|
| 144 |
code = gr.Code(value="", language="typescript", render=False)
|
| 145 |
logs = gr.Textbox(value="", label="Build Logs", interactive=False, render=False)
|
|
|
|
| 10 |
|
| 11 |
from smolagents import ToolCollection, ToolCallingAgent, InferenceClientModel, EMPTY_PROMPT_TEMPLATES
|
| 12 |
|
| 13 |
+
MODEL = "deepseek-ai/DeepSeek-R1-0528"
|
| 14 |
+
MAX_STEPS = 10
|
| 15 |
|
| 16 |
BUILD_SERVER_MCP_CONFIG = {"url": "https://prathje-gradio-motioncanvas-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"}
|
| 17 |
DOCS_SERVER_MCP_CONFIG = {"url": "https://prathje-gradio-motioncanvas-docs-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"}
|
| 18 |
|
| 19 |
+
SYSTEM_PROMPT = "You are a helpful assistant that generates motion canvas scenes. The user prompts his ideas. You should use the recursive_list with path '.' to check for available classes and examples. You should generate the code for a single standalone motion canvas scene.tsx and build it using the build tool. Please fix any errors. But if the build succeeds, you are done. You do not need to return the code or the logs. You can use all tools provided to you to help you with your task."
|
| 20 |
|
| 21 |
from smolagents import tool
|
| 22 |
|
|
|
|
| 32 |
|
| 33 |
BUILD_TIMEOUT=30
|
| 34 |
|
| 35 |
+
# Code, Path, Logs
|
| 36 |
+
LAST_BUILT_PROJECT_RESULT = "", "", ""
|
| 37 |
+
|
| 38 |
# In theory, we should be using the build server mcp, but it not working right now and we are running out of time ;)
|
| 39 |
+
# Note that we added success/failed to the response here.s
|
| 40 |
@tool
|
| 41 |
def build_project(code: str) -> str:
|
| 42 |
"""Build a Motion Canvas project.
|
|
|
|
| 44 |
Args:
|
| 45 |
code: TypeScript code for the scene to build
|
| 46 |
"""
|
| 47 |
+
global LAST_BUILT_PROJECT_RESULT
|
| 48 |
+
|
| 49 |
+
# we cache the result here in case the agent calls this tool multiple times with the same code...
|
| 50 |
+
if code == LAST_BUILT_PROJECT_RESULT[0]:
|
| 51 |
+
return "Success", LAST_BUILT_PROJECT_RESULT[1], LAST_BUILT_PROJECT_RESULT[2]
|
| 52 |
+
|
| 53 |
+
LAST_BUILT_PROJECT_RESULT = "", "", ""
|
| 54 |
+
|
| 55 |
|
| 56 |
# generate a random uuid for the project
|
| 57 |
project_id = str(uuid.uuid4())
|
|
|
|
| 94 |
|
| 95 |
# check if the build was successful
|
| 96 |
if process.returncode != 0:
|
| 97 |
+
return "Failed", "", acc_logs,
|
| 98 |
else:
|
| 99 |
# copy dist/project.js to get_local_path(id)
|
| 100 |
shutil.copy(os.path.join(tmp_dir, "dist", "project.js"), get_local_path(project_id))
|
| 101 |
+
|
| 102 |
+
LAST_BUILT_PROJECT_RESULT = code, get_public_path(project_id), acc_logs
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
return "Success", get_public_path(project_id), acc_logs
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
+
return "Failed", "", acc_logs + "\n" + "Error building project: " + str(e)
|
| 109 |
|
| 110 |
finally:
|
| 111 |
# cleanup tmp dir
|
| 112 |
shutil.rmtree(tmp_dir)
|
| 113 |
|
| 114 |
all_tools = []
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
def generate(message, history, code, logs):
|
| 117 |
+
global LAST_BUILT_PROJECT_RESULT
|
| 118 |
+
LAST_BUILT_PROJECT_RESULT = "", "", ""
|
| 119 |
+
|
| 120 |
+
output = "No response"
|
| 121 |
+
with ToolCollection.from_mcp(BUILD_SERVER_MCP_CONFIG, trust_remote_code=True) as build_tool_collection:
|
| 122 |
+
with ToolCollection.from_mcp(DOCS_SERVER_MCP_CONFIG, trust_remote_code=True) as docs_tool_collection:
|
| 123 |
+
all_tools = docs_tool_collection.tools + [build_project]
|
| 124 |
+
|
| 125 |
model = InferenceClientModel(model_id=MODEL)
|
| 126 |
|
| 127 |
+
agent = ToolCallingAgent(tools=[*all_tools], model=model, return_full_result=True, max_steps=MAX_STEPS)
|
| 128 |
|
| 129 |
agent.prompt_templates["system_prompt"] = SYSTEM_PROMPT
|
| 130 |
|
| 131 |
+
try:
|
| 132 |
+
res = agent.run(
|
| 133 |
+
message,
|
| 134 |
+
additional_args={'history': history, 'code': code, 'logs': logs}
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
output = res.output
|
| 138 |
+
except Exception as e:
|
| 139 |
+
print(e)
|
| 140 |
+
output = "An error occurred while generating the code"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
return output, LAST_BUILT_PROJECT_RESULT[0], LAST_BUILT_PROJECT_RESULT[1], LAST_BUILT_PROJECT_RESULT[2]
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
from gradio_motioncanvasplayer import MotionCanvasPlayer
|
|
|
|
| 155 |
gr.Markdown("# Motion Canvas Agent")
|
| 156 |
gr.Markdown("Leverage the power of AI and Motion Canvas to create animations using TypeScript.")
|
| 157 |
|
| 158 |
+
player = MotionCanvasPlayer(example_project_path, auto=True, quality=1.0, width=1920, height=1080, variables="{}", render=False)
|
| 159 |
|
| 160 |
code = gr.Code(value="", language="typescript", render=False)
|
| 161 |
logs = gr.Textbox(value="", label="Build Logs", interactive=False, render=False)
|