Spaces:
Build error
Build error
down
Browse files- .gitignore +1 -0
- crazy_functional.py +11 -0
- crazy_functions/crazy_functions_test.py +8 -1
- crazy_functions/数学动画生成manim.py +82 -0
.gitignore
CHANGED
@@ -149,3 +149,4 @@ crazy_functions/test_samples
|
|
149 |
request_llm/jittorllms
|
150 |
multi-language
|
151 |
request_llm/moss
|
|
|
|
149 |
request_llm/jittorllms
|
150 |
multi-language
|
151 |
request_llm/moss
|
152 |
+
media
|
crazy_functional.py
CHANGED
@@ -269,5 +269,16 @@ def get_crazy_functions():
|
|
269 |
"Function": HotReload(总结音视频)
|
270 |
}
|
271 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
###################### 第n组插件 ###########################
|
273 |
return function_plugins
|
|
|
269 |
"Function": HotReload(总结音视频)
|
270 |
}
|
271 |
})
|
272 |
+
try:
|
273 |
+
from crazy_functions.数学动画生成manim import 动画生成
|
274 |
+
function_plugins.update({
|
275 |
+
"数学动画生成(Manim)": {
|
276 |
+
"Color": "stop",
|
277 |
+
"AsButton": False,
|
278 |
+
"Function": HotReload(动画生成)
|
279 |
+
}
|
280 |
+
})
|
281 |
+
except:
|
282 |
+
print('Load function plugin failed')
|
283 |
###################### 第n组插件 ###########################
|
284 |
return function_plugins
|
crazy_functions/crazy_functions_test.py
CHANGED
@@ -99,6 +99,12 @@ def test_解析ipynb文件():
|
|
99 |
print(cb)
|
100 |
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
# test_解析一个Python项目()
|
103 |
# test_Latex英文润色()
|
104 |
# test_Markdown中译英()
|
@@ -108,7 +114,8 @@ def test_解析ipynb文件():
|
|
108 |
# test_下载arxiv论文并翻译摘要()
|
109 |
# test_解析一个Cpp项目()
|
110 |
# test_联网回答问题()
|
111 |
-
test_解析ipynb文件()
|
|
|
112 |
|
113 |
input("程序完成,回车退出。")
|
114 |
print("退出。")
|
|
|
99 |
print(cb)
|
100 |
|
101 |
|
102 |
+
def test_数学动画生成manim():
|
103 |
+
from crazy_functions.数学动画生成manim import 动画生成
|
104 |
+
txt = "A ball split into 2, and then split into 4, and finally split into 8."
|
105 |
+
for cookies, cb, hist, msg in 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
106 |
+
print(cb)
|
107 |
+
|
108 |
# test_解析一个Python项目()
|
109 |
# test_Latex英文润色()
|
110 |
# test_Markdown中译英()
|
|
|
114 |
# test_下载arxiv论文并翻译摘要()
|
115 |
# test_解析一个Cpp项目()
|
116 |
# test_联网回答问题()
|
117 |
+
# test_解析ipynb文件()
|
118 |
+
test_数学动画生成manim()
|
119 |
|
120 |
input("程序完成,回车退出。")
|
121 |
print("退出。")
|
crazy_functions/数学动画生成manim.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from toolbox import CatchException, update_ui
|
2 |
+
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
3 |
+
|
4 |
+
def inspect_dependency(chatbot, history):
|
5 |
+
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
6 |
+
try:
|
7 |
+
import manim
|
8 |
+
return True
|
9 |
+
except:
|
10 |
+
chatbot.append(["导入依赖失败", "使用该模块需要额外依赖,安装方法:```pip install manimgl```"])
|
11 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
12 |
+
return False
|
13 |
+
|
14 |
+
def gen_time_str():
|
15 |
+
import time
|
16 |
+
return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
|
17 |
+
|
18 |
+
def eval_manim(code):
|
19 |
+
import subprocess, sys, os, shutil
|
20 |
+
|
21 |
+
with open('gpt_log/MyAnimation.py', 'w', encoding='utf8') as f:
|
22 |
+
f.write(code)
|
23 |
+
|
24 |
+
subprocess.check_output([sys.executable, '-c', "from gpt_log.MyAnimation import MyAnimation; MyAnimation().render()"])
|
25 |
+
|
26 |
+
try:
|
27 |
+
shutil.copyfile('media/videos/1080p60/MyAnimation.mp4', f'gpt_log/{gen_time_str()}.mp4')
|
28 |
+
except:
|
29 |
+
print('generating mp4 failed')
|
30 |
+
return "Generating mp4 failed"
|
31 |
+
return f'gpt_log/{gen_time_str()}.mp4'
|
32 |
+
|
33 |
+
def get_code_block(reply):
|
34 |
+
import re
|
35 |
+
pattern = r"```([\s\S]*?)```" # regex pattern to match code blocks
|
36 |
+
matches = re.findall(pattern, reply) # find all code blocks in text
|
37 |
+
if len(matches) != 1:
|
38 |
+
raise RuntimeError("GPT is not generating proper code.")
|
39 |
+
return matches[0].strip('python') # code block
|
40 |
+
|
41 |
+
@CatchException
|
42 |
+
def 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
43 |
+
"""
|
44 |
+
txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径
|
45 |
+
llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行
|
46 |
+
plugin_kwargs 插件模型的参数,暂时没有用武之地
|
47 |
+
chatbot 聊天显示框的句柄,用于显示给用户
|
48 |
+
history 聊天历史,前情提要
|
49 |
+
system_prompt 给gpt的静默提醒
|
50 |
+
web_port 当前软件运行的端口号
|
51 |
+
"""
|
52 |
+
# 清空历史,以免输入溢出
|
53 |
+
history = []
|
54 |
+
|
55 |
+
# 基本信息:功能、贡献者
|
56 |
+
chatbot.append([
|
57 |
+
"函数插件功能?",
|
58 |
+
"生成数学动画, 作者: binary-husky, 插件初始化中 ..."
|
59 |
+
])
|
60 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
61 |
+
|
62 |
+
# 尝试导入依赖, 如果缺少依赖, 则给出安装建议
|
63 |
+
dep_ok = yield from inspect_dependency(chatbot=chatbot, history=history) # 刷新界面
|
64 |
+
if not dep_ok: return
|
65 |
+
|
66 |
+
# 开始
|
67 |
+
i_say = f'Generate a animation to show:' + txt
|
68 |
+
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
69 |
+
inputs=i_say, inputs_show_user=i_say,
|
70 |
+
llm_kwargs=llm_kwargs, chatbot=chatbot, history=[],
|
71 |
+
sys_prompt="Write a animation script with 3blue1brown's manim. Name the class as `MyAnimation`. And answer me with a code block wrapped by ```."
|
72 |
+
)
|
73 |
+
chatbot.append((i_say, gpt_say))
|
74 |
+
history.extend([i_say, gpt_say])
|
75 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
76 |
+
|
77 |
+
# 将代码转为动画
|
78 |
+
code = get_code_block(gpt_say)
|
79 |
+
res = eval_manim(code)
|
80 |
+
|
81 |
+
chatbot.append(("生成的视频文件路径", res))
|
82 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|