Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	fix bug
Browse files- crazy_functions/数学动画生成manim.py +129 -5
    	
        crazy_functions/数学动画生成manim.py
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 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 | 
             
                # 尝试导入依赖,如果缺少依赖,则给出安装建议
         | 
| @@ -21,13 +22,25 @@ def eval_manim(code): | |
| 21 | 
             
                with open('gpt_log/MyAnimation.py', 'w', encoding='utf8') as f:
         | 
| 22 | 
             
                    f.write(code)
         | 
| 23 |  | 
| 24 | 
            -
                 | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 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):
         | 
| @@ -63,14 +76,20 @@ def 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt | |
| 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= | 
|  | |
|  | |
|  | |
| 72 | 
             
                )
         | 
| 73 | 
            -
                chatbot.append( | 
| 74 | 
             
                history.extend([i_say, gpt_say])
         | 
| 75 | 
             
                yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
         | 
| 76 |  | 
| @@ -80,3 +99,108 @@ def 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt | |
| 80 |  | 
| 81 | 
             
                chatbot.append(("生成的视频文件路径", res))
         | 
| 82 | 
             
                yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
             
            from toolbox import CatchException, update_ui
         | 
| 2 | 
             
            from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
         | 
| 3 | 
            +
            from .crazy_utils import input_clipping
         | 
| 4 |  | 
| 5 | 
             
            def inspect_dependency(chatbot, history):
         | 
| 6 | 
             
                # 尝试导入依赖,如果缺少依赖,则给出安装建议
         | 
|  | |
| 22 | 
             
                with open('gpt_log/MyAnimation.py', 'w', encoding='utf8') as f:
         | 
| 23 | 
             
                    f.write(code)
         | 
| 24 |  | 
| 25 | 
            +
                def get_class_name(class_string):
         | 
| 26 | 
            +
                    import re
         | 
| 27 | 
            +
                    # Use regex to extract the class name
         | 
| 28 | 
            +
                    class_name = re.search(r'class (\w+)\(Scene\)', class_string).group(1)
         | 
| 29 | 
            +
                    return class_name
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                class_name = get_class_name(code)
         | 
| 32 |  | 
| 33 | 
             
                try: 
         | 
| 34 | 
            +
                    subprocess.check_output([sys.executable, '-c', f"from gpt_log.MyAnimation import {class_name}; {class_name}().render()"])
         | 
| 35 | 
             
                    shutil.copyfile('media/videos/1080p60/MyAnimation.mp4', f'gpt_log/{gen_time_str()}.mp4')
         | 
| 36 | 
            +
                except subprocess.CalledProcessError as e:
         | 
| 37 | 
            +
                    output = e.output.decode()
         | 
| 38 | 
            +
                    print(f"Command returned non-zero exit status {e.returncode}: {output}")
         | 
| 39 | 
            +
                    return "Evaluating python script failed"
         | 
| 40 | 
             
                except: 
         | 
| 41 | 
             
                    print('generating mp4 failed')
         | 
| 42 | 
             
                    return "Generating mp4 failed"
         | 
| 43 | 
            +
             | 
| 44 | 
             
                return f'gpt_log/{gen_time_str()}.mp4'
         | 
| 45 |  | 
| 46 | 
             
            def get_code_block(reply):
         | 
|  | |
| 76 | 
             
                dep_ok = yield from inspect_dependency(chatbot=chatbot, history=history) # 刷新界面
         | 
| 77 | 
             
                if not dep_ok: return
         | 
| 78 |  | 
| 79 | 
            +
                # 输入
         | 
| 80 | 
            +
                i_say = f'Generate a animation to show: ' + txt
         | 
| 81 | 
            +
                demo = ["Here is some examples of manim", examples_of_manim()]
         | 
| 82 | 
            +
                _, demo = input_clipping(inputs="", history=demo, max_token_limit=2560)
         | 
| 83 | 
             
                # 开始
         | 
|  | |
| 84 | 
             
                gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
         | 
| 85 | 
             
                    inputs=i_say, inputs_show_user=i_say, 
         | 
| 86 | 
            +
                    llm_kwargs=llm_kwargs, chatbot=chatbot, history=demo, 
         | 
| 87 | 
            +
                    sys_prompt=
         | 
| 88 | 
            +
                    r"Write a animation script with 3blue1brown's manim. "+
         | 
| 89 | 
            +
                    r"Please begin with `from manim import *` and name the class as `MyAnimation`. " + 
         | 
| 90 | 
            +
                    r"Answer me with a code block wrapped by ```."
         | 
| 91 | 
             
                )
         | 
| 92 | 
            +
                chatbot.append(["开始生成动画", "..."])
         | 
| 93 | 
             
                history.extend([i_say, gpt_say])
         | 
| 94 | 
             
                yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
         | 
| 95 |  | 
|  | |
| 99 |  | 
| 100 | 
             
                chatbot.append(("生成的视频文件路径", res))
         | 
| 101 | 
             
                yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
         | 
| 102 | 
            +
             | 
| 103 | 
            +
            # 在这里放一些网上搜集的demo,辅助gpt生成代码
         | 
| 104 | 
            +
            def examples_of_manim():
         | 
| 105 | 
            +
                return """
         | 
| 106 | 
            +
             | 
| 107 | 
            +
             | 
| 108 | 
            +
            ```
         | 
| 109 | 
            +
            # Moving Group To Destination
         | 
| 110 | 
            +
            class MyAnimation(Scene):
         | 
| 111 | 
            +
                def construct(self):
         | 
| 112 | 
            +
                    group = VGroup(Dot(LEFT), Dot(ORIGIN), Dot(RIGHT, color=RED), Dot(2 * RIGHT)).scale(1.4)
         | 
| 113 | 
            +
                    dest = Dot([4, 3, 0], color=YELLOW)
         | 
| 114 | 
            +
                    self.add(group, dest)
         | 
| 115 | 
            +
                    self.play(group.animate.shift(dest.get_center() - group[2].get_center()))
         | 
| 116 | 
            +
                    self.wait(0.5)
         | 
| 117 | 
            +
             | 
| 118 | 
            +
            ```
         | 
| 119 | 
            +
             | 
| 120 | 
            +
             | 
| 121 | 
            +
            ```
         | 
| 122 | 
            +
            # Moving FrameBox
         | 
| 123 | 
            +
            class MyAnimation(Scene):
         | 
| 124 | 
            +
                def construct(self):
         | 
| 125 | 
            +
                    text=MathTex(
         | 
| 126 | 
            +
                        "\\frac{d}{dx}f(x)g(x)=","f(x)\\frac{d}{dx}g(x)","+",
         | 
| 127 | 
            +
                        "g(x)\\frac{d}{dx}f(x)"
         | 
| 128 | 
            +
                    )
         | 
| 129 | 
            +
                    self.play(Write(text))
         | 
| 130 | 
            +
                    framebox1 = SurroundingRectangle(text[1], buff = .1)
         | 
| 131 | 
            +
                    framebox2 = SurroundingRectangle(text[3], buff = .1)
         | 
| 132 | 
            +
                    self.play(
         | 
| 133 | 
            +
                        Create(framebox1),
         | 
| 134 | 
            +
                    )
         | 
| 135 | 
            +
                    self.wait()
         | 
| 136 | 
            +
                    self.play(
         | 
| 137 | 
            +
                        ReplacementTransform(framebox1,framebox2),
         | 
| 138 | 
            +
                    )
         | 
| 139 | 
            +
                    self.wait()
         | 
| 140 | 
            +
             | 
| 141 | 
            +
            ```
         | 
| 142 | 
            +
             | 
| 143 | 
            +
             | 
| 144 | 
            +
             | 
| 145 | 
            +
            ```
         | 
| 146 | 
            +
            # Point With Trace
         | 
| 147 | 
            +
            class MyAnimation(Scene):
         | 
| 148 | 
            +
                def construct(self):
         | 
| 149 | 
            +
                    path = VMobject()
         | 
| 150 | 
            +
                    dot = Dot()
         | 
| 151 | 
            +
                    path.set_points_as_corners([dot.get_center(), dot.get_center()])
         | 
| 152 | 
            +
                    def update_path(path):
         | 
| 153 | 
            +
                        previous_path = path.copy()
         | 
| 154 | 
            +
                        previous_path.add_points_as_corners([dot.get_center()])
         | 
| 155 | 
            +
                        path.become(previous_path)
         | 
| 156 | 
            +
                    path.add_updater(update_path)
         | 
| 157 | 
            +
                    self.add(path, dot)
         | 
| 158 | 
            +
                    self.play(Rotating(dot, radians=PI, about_point=RIGHT, run_time=2))
         | 
| 159 | 
            +
                    self.wait()
         | 
| 160 | 
            +
                    self.play(dot.animate.shift(UP))
         | 
| 161 | 
            +
                    self.play(dot.animate.shift(LEFT))
         | 
| 162 | 
            +
                    self.wait()
         | 
| 163 | 
            +
             | 
| 164 | 
            +
            ```
         | 
| 165 | 
            +
             | 
| 166 | 
            +
            ```
         | 
| 167 | 
            +
            # SinAndCosFunctionPlot
         | 
| 168 | 
            +
            class MyAnimation(Scene):
         | 
| 169 | 
            +
                def construct(self):
         | 
| 170 | 
            +
                    axes = Axes(
         | 
| 171 | 
            +
                        x_range=[-10, 10.3, 1],
         | 
| 172 | 
            +
                        y_range=[-1.5, 1.5, 1],
         | 
| 173 | 
            +
                        x_length=10,
         | 
| 174 | 
            +
                        axis_config={"color": GREEN},
         | 
| 175 | 
            +
                        x_axis_config={
         | 
| 176 | 
            +
                            "numbers_to_include": np.arange(-10, 10.01, 2),
         | 
| 177 | 
            +
                            "numbers_with_elongated_ticks": np.arange(-10, 10.01, 2),
         | 
| 178 | 
            +
                        },
         | 
| 179 | 
            +
                        tips=False,
         | 
| 180 | 
            +
                    )
         | 
| 181 | 
            +
                    axes_labels = axes.get_axis_labels()
         | 
| 182 | 
            +
                    sin_graph = axes.plot(lambda x: np.sin(x), color=BLUE)
         | 
| 183 | 
            +
                    cos_graph = axes.plot(lambda x: np.cos(x), color=RED)
         | 
| 184 | 
            +
             | 
| 185 | 
            +
                    sin_label = axes.get_graph_label(
         | 
| 186 | 
            +
                        sin_graph, "\\sin(x)", x_val=-10, direction=UP / 2
         | 
| 187 | 
            +
                    )
         | 
| 188 | 
            +
                    cos_label = axes.get_graph_label(cos_graph, label="\\cos(x)")
         | 
| 189 | 
            +
             | 
| 190 | 
            +
                    vert_line = axes.get_vertical_line(
         | 
| 191 | 
            +
                        axes.i2gp(TAU, cos_graph), color=YELLOW, line_func=Line
         | 
| 192 | 
            +
                    )
         | 
| 193 | 
            +
                    line_label = axes.get_graph_label(
         | 
| 194 | 
            +
                        cos_graph, "x=2\pi", x_val=TAU, direction=UR, color=WHITE
         | 
| 195 | 
            +
                    )
         | 
| 196 | 
            +
             | 
| 197 | 
            +
                    plot = VGroup(axes, sin_graph, cos_graph, vert_line)
         | 
| 198 | 
            +
                    labels = VGroup(axes_labels, sin_label, cos_label, line_label)
         | 
| 199 | 
            +
                    self.add(plot, labels)
         | 
| 200 | 
            +
             | 
| 201 | 
            +
            ```
         | 
| 202 | 
            +
             | 
| 203 | 
            +
             | 
| 204 | 
            +
             | 
| 205 | 
            +
             | 
| 206 | 
            +
            """
         |