fix bug
This commit is contained in:
		
							parent
							
								
									2887720999
								
							
						
					
					
						commit
						a099f98f0e
					
				@ -1,5 +1,6 @@
 | 
				
			|||||||
from toolbox import CatchException, update_ui
 | 
					from toolbox import CatchException, update_ui
 | 
				
			||||||
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
 | 
					from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
 | 
				
			||||||
 | 
					from .crazy_utils import input_clipping
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def inspect_dependency(chatbot, history):
 | 
					def inspect_dependency(chatbot, history):
 | 
				
			||||||
    # 尝试导入依赖,如果缺少依赖,则给出安装建议
 | 
					    # 尝试导入依赖,如果缺少依赖,则给出安装建议
 | 
				
			||||||
@ -21,13 +22,25 @@ def eval_manim(code):
 | 
				
			|||||||
    with open('gpt_log/MyAnimation.py', 'w', encoding='utf8') as f:
 | 
					    with open('gpt_log/MyAnimation.py', 'w', encoding='utf8') as f:
 | 
				
			||||||
        f.write(code)
 | 
					        f.write(code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    subprocess.check_output([sys.executable, '-c', "from gpt_log.MyAnimation import MyAnimation; MyAnimation().render()"])
 | 
					    def get_class_name(class_string):
 | 
				
			||||||
 | 
					        import re
 | 
				
			||||||
 | 
					        # Use regex to extract the class name
 | 
				
			||||||
 | 
					        class_name = re.search(r'class (\w+)\(Scene\)', class_string).group(1)
 | 
				
			||||||
 | 
					        return class_name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class_name = get_class_name(code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try: 
 | 
					    try: 
 | 
				
			||||||
 | 
					        subprocess.check_output([sys.executable, '-c', f"from gpt_log.MyAnimation import {class_name}; {class_name}().render()"])
 | 
				
			||||||
        shutil.copyfile('media/videos/1080p60/MyAnimation.mp4', f'gpt_log/{gen_time_str()}.mp4')
 | 
					        shutil.copyfile('media/videos/1080p60/MyAnimation.mp4', f'gpt_log/{gen_time_str()}.mp4')
 | 
				
			||||||
 | 
					    except subprocess.CalledProcessError as e:
 | 
				
			||||||
 | 
					        output = e.output.decode()
 | 
				
			||||||
 | 
					        print(f"Command returned non-zero exit status {e.returncode}: {output}")
 | 
				
			||||||
 | 
					        return "Evaluating python script failed"
 | 
				
			||||||
    except: 
 | 
					    except: 
 | 
				
			||||||
        print('generating mp4 failed')
 | 
					        print('generating mp4 failed')
 | 
				
			||||||
        return "Generating mp4 failed"
 | 
					        return "Generating mp4 failed"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return f'gpt_log/{gen_time_str()}.mp4'
 | 
					    return f'gpt_log/{gen_time_str()}.mp4'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_code_block(reply):
 | 
					def get_code_block(reply):
 | 
				
			||||||
@ -63,14 +76,20 @@ def 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt
 | 
				
			|||||||
    dep_ok = yield from inspect_dependency(chatbot=chatbot, history=history) # 刷新界面
 | 
					    dep_ok = yield from inspect_dependency(chatbot=chatbot, history=history) # 刷新界面
 | 
				
			||||||
    if not dep_ok: return
 | 
					    if not dep_ok: return
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    # 输入
 | 
				
			||||||
 | 
					    i_say = f'Generate a animation to show: ' + txt
 | 
				
			||||||
 | 
					    demo = ["Here is some examples of manim", examples_of_manim()]
 | 
				
			||||||
 | 
					    _, demo = input_clipping(inputs="", history=demo, max_token_limit=2560)
 | 
				
			||||||
    # 开始
 | 
					    # 开始
 | 
				
			||||||
    i_say = f'Generate a animation to show:' + txt
 | 
					 | 
				
			||||||
    gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
 | 
					    gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
 | 
				
			||||||
        inputs=i_say, inputs_show_user=i_say, 
 | 
					        inputs=i_say, inputs_show_user=i_say, 
 | 
				
			||||||
        llm_kwargs=llm_kwargs, chatbot=chatbot, history=[], 
 | 
					        llm_kwargs=llm_kwargs, chatbot=chatbot, history=demo, 
 | 
				
			||||||
        sys_prompt="Write a animation script with 3blue1brown's manim. Name the class as `MyAnimation`. And answer me with a code block wrapped by ```."
 | 
					        sys_prompt=
 | 
				
			||||||
 | 
					        r"Write a animation script with 3blue1brown's manim. "+
 | 
				
			||||||
 | 
					        r"Please begin with `from manim import *` and name the class as `MyAnimation`. " + 
 | 
				
			||||||
 | 
					        r"Answer me with a code block wrapped by ```."
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    chatbot.append((i_say, gpt_say))
 | 
					    chatbot.append(["开始生成动画", "..."])
 | 
				
			||||||
    history.extend([i_say, gpt_say])
 | 
					    history.extend([i_say, gpt_say])
 | 
				
			||||||
    yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
 | 
					    yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
@ -80,3 +99,108 @@ def 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    chatbot.append(("生成的视频文件路径", res))
 | 
					    chatbot.append(("生成的视频文件路径", res))
 | 
				
			||||||
    yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
 | 
					    yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 在这里放一些网上搜集的demo,辅助gpt生成代码
 | 
				
			||||||
 | 
					def examples_of_manim():
 | 
				
			||||||
 | 
					    return """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					# Moving Group To Destination
 | 
				
			||||||
 | 
					class MyAnimation(Scene):
 | 
				
			||||||
 | 
					    def construct(self):
 | 
				
			||||||
 | 
					        group = VGroup(Dot(LEFT), Dot(ORIGIN), Dot(RIGHT, color=RED), Dot(2 * RIGHT)).scale(1.4)
 | 
				
			||||||
 | 
					        dest = Dot([4, 3, 0], color=YELLOW)
 | 
				
			||||||
 | 
					        self.add(group, dest)
 | 
				
			||||||
 | 
					        self.play(group.animate.shift(dest.get_center() - group[2].get_center()))
 | 
				
			||||||
 | 
					        self.wait(0.5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					# Moving FrameBox
 | 
				
			||||||
 | 
					class MyAnimation(Scene):
 | 
				
			||||||
 | 
					    def construct(self):
 | 
				
			||||||
 | 
					        text=MathTex(
 | 
				
			||||||
 | 
					            "\\frac{d}{dx}f(x)g(x)=","f(x)\\frac{d}{dx}g(x)","+",
 | 
				
			||||||
 | 
					            "g(x)\\frac{d}{dx}f(x)"
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        self.play(Write(text))
 | 
				
			||||||
 | 
					        framebox1 = SurroundingRectangle(text[1], buff = .1)
 | 
				
			||||||
 | 
					        framebox2 = SurroundingRectangle(text[3], buff = .1)
 | 
				
			||||||
 | 
					        self.play(
 | 
				
			||||||
 | 
					            Create(framebox1),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        self.wait()
 | 
				
			||||||
 | 
					        self.play(
 | 
				
			||||||
 | 
					            ReplacementTransform(framebox1,framebox2),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        self.wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					# Point With Trace
 | 
				
			||||||
 | 
					class MyAnimation(Scene):
 | 
				
			||||||
 | 
					    def construct(self):
 | 
				
			||||||
 | 
					        path = VMobject()
 | 
				
			||||||
 | 
					        dot = Dot()
 | 
				
			||||||
 | 
					        path.set_points_as_corners([dot.get_center(), dot.get_center()])
 | 
				
			||||||
 | 
					        def update_path(path):
 | 
				
			||||||
 | 
					            previous_path = path.copy()
 | 
				
			||||||
 | 
					            previous_path.add_points_as_corners([dot.get_center()])
 | 
				
			||||||
 | 
					            path.become(previous_path)
 | 
				
			||||||
 | 
					        path.add_updater(update_path)
 | 
				
			||||||
 | 
					        self.add(path, dot)
 | 
				
			||||||
 | 
					        self.play(Rotating(dot, radians=PI, about_point=RIGHT, run_time=2))
 | 
				
			||||||
 | 
					        self.wait()
 | 
				
			||||||
 | 
					        self.play(dot.animate.shift(UP))
 | 
				
			||||||
 | 
					        self.play(dot.animate.shift(LEFT))
 | 
				
			||||||
 | 
					        self.wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					# SinAndCosFunctionPlot
 | 
				
			||||||
 | 
					class MyAnimation(Scene):
 | 
				
			||||||
 | 
					    def construct(self):
 | 
				
			||||||
 | 
					        axes = Axes(
 | 
				
			||||||
 | 
					            x_range=[-10, 10.3, 1],
 | 
				
			||||||
 | 
					            y_range=[-1.5, 1.5, 1],
 | 
				
			||||||
 | 
					            x_length=10,
 | 
				
			||||||
 | 
					            axis_config={"color": GREEN},
 | 
				
			||||||
 | 
					            x_axis_config={
 | 
				
			||||||
 | 
					                "numbers_to_include": np.arange(-10, 10.01, 2),
 | 
				
			||||||
 | 
					                "numbers_with_elongated_ticks": np.arange(-10, 10.01, 2),
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            tips=False,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        axes_labels = axes.get_axis_labels()
 | 
				
			||||||
 | 
					        sin_graph = axes.plot(lambda x: np.sin(x), color=BLUE)
 | 
				
			||||||
 | 
					        cos_graph = axes.plot(lambda x: np.cos(x), color=RED)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        sin_label = axes.get_graph_label(
 | 
				
			||||||
 | 
					            sin_graph, "\\sin(x)", x_val=-10, direction=UP / 2
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        cos_label = axes.get_graph_label(cos_graph, label="\\cos(x)")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        vert_line = axes.get_vertical_line(
 | 
				
			||||||
 | 
					            axes.i2gp(TAU, cos_graph), color=YELLOW, line_func=Line
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        line_label = axes.get_graph_label(
 | 
				
			||||||
 | 
					            cos_graph, "x=2\pi", x_val=TAU, direction=UR, color=WHITE
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        plot = VGroup(axes, sin_graph, cos_graph, vert_line)
 | 
				
			||||||
 | 
					        labels = VGroup(axes_labels, sin_label, cos_label, line_label)
 | 
				
			||||||
 | 
					        self.add(plot, labels)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user