-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
1146 lines (963 loc) · 34.6 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, jsonify, send_from_directory, url_for
import os
import tempfile
import subprocess
import logging
import uuid
import shutil
import json
from manim import *
import openai
from dotenv import load_dotenv
from datetime import datetime
import time
import random
import io
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__,
static_url_path='/static',
static_folder='static')
app.logger.setLevel(logging.INFO)
# Configure Manim
config.media_dir = "media"
config.video_dir = "videos"
config.images_dir = "images"
config.text_dir = "texts"
config.tex_dir = "tex"
config.log_dir = "log"
config.renderer = "cairo"
config.text_renderer = "cairo"
config.use_opengl_renderer = False
# Set up required directories
def setup_directories():
"""Create all required directories for the application"""
directories = [
os.path.join(app.root_path, 'static'),
os.path.join(app.root_path, 'static', 'videos'),
os.path.join(app.root_path, 'tmp'),
os.path.join(app.root_path, 'media'),
os.path.join(app.root_path, 'media', 'videos'),
os.path.join(app.root_path, 'media', 'videos', 'scene'),
os.path.join(app.root_path, 'media', 'videos', 'scene', '720p30'),
os.path.join(app.root_path, 'media', 'videos', 'scene', '1080p60')
]
for directory in directories:
os.makedirs(directory, exist_ok=True)
app.logger.info(f'Created directory: {directory}')
# Set up directories at startup
setup_directories()
# Ensure static directory exists
os.makedirs(os.path.join(app.root_path, 'static', 'videos'), exist_ok=True)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize OpenAI client
openai.api_key = os.getenv('OPENAI_API_KEY')
# Set media and temporary directories with fallback to local paths
if os.environ.get('DOCKER_ENV'):
app.config['MEDIA_DIR'] = os.getenv('MEDIA_DIR', '/app/media')
app.config['TEMP_DIR'] = os.getenv('TEMP_DIR', '/app/tmp')
else:
app.config['MEDIA_DIR'] = os.path.join(os.path.dirname(__file__), 'media')
app.config['TEMP_DIR'] = os.path.join(os.path.dirname(__file__), 'tmp')
# Ensure directories exist
os.makedirs(app.config['MEDIA_DIR'], exist_ok=True)
os.makedirs(app.config['TEMP_DIR'], exist_ok=True)
os.makedirs(os.path.join(app.config['MEDIA_DIR'], 'videos', 'scene', '720p30'), exist_ok=True)
os.makedirs(os.path.join(app.static_folder, 'videos'), exist_ok=True)
def sanitize_input(text):
"""Sanitize input text by removing extra whitespace and newlines"""
return ' '.join(text.strip().split())
def sanitize_title(text):
"""Sanitize text for use in title"""
text = sanitize_input(text)
return text.replace('"', '').replace("'", "").strip()
def generate_manim_prompt(concept):
"""Generate a detailed prompt for GPT to create Manim code"""
return f"""Create a detailed Manim animation to demonstrate and explain: {concept}
Create a Scene class named MainScene that follows these requirements:
1. Scene Setup:
- For 3D concepts: Use ThreeDScene with appropriate camera angles
- For 2D concepts: Use Scene with NumberPlane when relevant
- Add title and clear mathematical labels
2. Mathematical Elements:
- Use MathTex for equations with proper LaTeX syntax
- Include step-by-step derivations when showing formulas
- Add mathematical annotations and explanations
- Show key points and important relationships
3. Visual Elements:
- Create clear geometric shapes and diagrams
- Use color coding to highlight important parts
- Add arrows or lines to show relationships
- Include coordinate axes when relevant
4. Animation Flow:
- Break down complex concepts into simple steps
- Use smooth transitions between steps
- Add pauses (self.wait()) at key moments
- Use transform animations to show changes
5. Specific Requirements:
- For equations: Show step-by-step solutions
- For theorems: Visualize proof steps
- For geometry: Show construction process
- For 3D: Include multiple camera angles
- For graphs: Show coordinate system and gridlines
6. Code Structure:
- Import required Manim modules
- Use proper class inheritance
- Define clear animation sequences
- Include helpful comments
Example structure:
```python
from manim import *
class MainScene(Scene): # or ThreeDScene for 3D
def construct(self):
# 1. Setup and introduction
title = Title("Concept Name")
# 2. Create mathematical objects
equation = MathTex(r"your_equation")
# 3. Create geometric objects
shapes = VGroup(...)
# 4. Add annotations
labels = VGroup(...)
# 5. Animate step by step
self.play(Write(title))
self.play(Create(shapes))
# 6. Show relationships
self.play(Transform(...))
# 7. Conclude
self.wait()
```
Only output valid Manim Python code without any additional text or markdown."""
def select_template(concept):
"""Select appropriate template based on the concept."""
concept = concept.lower().strip()
# Define template mappings with keywords
template_mappings = {
'pythagorean': {
'keywords': ['pythagoras', 'pythagorean', 'right triangle', 'hypotenuse'],
'generator': generate_pythagorean_code
},
'quadratic': {
'keywords': ['quadratic', 'parabola', 'x squared', 'x^2'],
'generator': generate_quadratic_code
},
'trigonometry': {
'keywords': ['sine', 'cosine', 'trigonometry', 'trig', 'unit circle'],
'generator': generate_trig_code
},
'3d_surface': {
'keywords': ['3d surface', 'surface plot', '3d plot', 'three dimensional'],
'generator': generate_3d_surface_code
},
'sphere': {
'keywords': ['sphere', 'ball', 'spherical'],
'generator': generate_sphere_code
},
'cube': {
'keywords': ['cube', 'cubic', 'box'],
'generator': generate_cube_code
},
'derivative': {
'keywords': ['derivative', 'differentiation', 'slope', 'rate of change'],
'generator': generate_derivative_code
},
'integral': {
'keywords': ['integration', 'integral', 'area under curve', 'antiderivative'],
'generator': generate_integral_code
},
'matrix': {
'keywords': ['matrix', 'matrices', 'linear transformation'],
'generator': generate_matrix_code
},
'eigenvalue': {
'keywords': ['eigenvalue', 'eigenvector', 'characteristic'],
'generator': generate_eigenvalue_code
},
'complex': {
'keywords': ['complex', 'imaginary', 'complex plane'],
'generator': generate_complex_code
},
'differential_equation': {
'keywords': ['differential equation', 'ode', 'pde'],
'generator': generate_diff_eq_code
}
}
# Find best matching template
best_match = None
max_matches = 0
for template_name, template_info in template_mappings.items():
matches = sum(1 for keyword in template_info['keywords'] if keyword in concept)
if matches > max_matches:
max_matches = matches
best_match = template_info['generator']
# Return best matching template or fallback to basic visualization
if best_match and max_matches > 0:
try:
return best_match()
except Exception as e:
logger.error(f"Error generating template {best_match.__name__}: {str(e)}")
return generate_basic_visualization_code()
# Default to basic visualization if no good match found
return generate_basic_visualization_code()
def generate_pythagorean_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create triangle
triangle = Polygon(
ORIGIN, RIGHT*3, UP*4,
color=WHITE
)
# Add labels using Text instead of MathTex
a = Text("a", font_size=36).next_to(triangle, DOWN)
b = Text("b", font_size=36).next_to(triangle, RIGHT)
c = Text("c", font_size=36).next_to(
triangle.get_center() + UP + RIGHT,
UP+RIGHT
)
# Add equation using Text
equation = Text("a² + b² = c²", font_size=36)
equation.to_edge(UP)
# Create the animation
self.play(Create(triangle))
self.play(Write(a), Write(b), Write(c))
self.play(Write(equation))
self.wait()'''
def generate_derivative_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create coordinate system
axes = Axes(
x_range=[-2, 2],
y_range=[-1, 2],
axis_config={"include_tip": True}
)
# Add custom labels
x_label = Text("x").next_to(axes.x_axis.get_end(), RIGHT)
y_label = Text("y").next_to(axes.y_axis.get_end(), UP)
# Create function
def func(x):
return x**2
graph = axes.plot(func, color=BLUE)
# Create derivative function
def deriv(x):
return 2*x
derivative = axes.plot(deriv, color=RED)
# Create labels
func_label = Text("f(x) = x²").set_color(BLUE)
deriv_label = Text("f'(x) = 2x").set_color(RED)
# Position labels
func_label.to_corner(UL)
deriv_label.next_to(func_label, DOWN)
# Create animations
self.play(Create(axes), Write(x_label), Write(y_label))
self.play(Create(graph), Write(func_label))
self.wait()
self.play(Create(derivative), Write(deriv_label))
self.wait()'''
def generate_integral_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create coordinate system
axes = Axes(
x_range=[-2, 2],
y_range=[-1, 2],
axis_config={"include_tip": True}
)
# Add custom labels
x_label = Text("x").next_to(axes.x_axis.get_end(), RIGHT)
y_label = Text("y").next_to(axes.y_axis.get_end(), UP)
# Create function
def func(x):
return x**2
graph = axes.plot(func, color=BLUE)
# Create area
area = axes.get_area(
graph,
x_range=[0, 1],
color=YELLOW,
opacity=0.3
)
# Create labels
func_label = Text("f(x) = x²").set_color(BLUE)
integral_label = Text("Area = 1/3").set_color(YELLOW)
# Position labels
func_label.to_corner(UL)
integral_label.next_to(func_label, DOWN)
# Create animations
self.play(Create(axes), Write(x_label), Write(y_label))
self.play(Create(graph), Write(func_label))
self.wait()
self.play(FadeIn(area), Write(integral_label))
self.wait()'''
def generate_3d_surface_code():
return '''from manim import *
import numpy as np
class MainScene(ThreeDScene):
def construct(self):
# Set up the axes with better spacing
axes = ThreeDAxes(
x_range=[-3, 3, 1],
y_range=[-3, 3, 1],
z_range=[-2, 2, 0.5],
x_length=6,
y_length=6,
z_length=4,
axis_config={"include_tip": True}
)
# Create surface function
def param_surface(u, v):
x = u
y = v
z = np.sin(np.sqrt(x**2 + y**2))
return np.array([x, y, z])
# Create surface with optimized resolution
surface = Surface(
lambda u, v: param_surface(u, v),
u_range=[-3, 3],
v_range=[-3, 3],
resolution=(20, 20),
should_make_jagged=False,
stroke_opacity=0
)
# Add color and styling
surface.set_style(
fill_opacity=0.8,
stroke_color=BLUE,
stroke_width=0.5,
fill_color=BLUE
)
surface.set_fill_by_value(
axes=axes,
colors=[(RED, -0.5), (YELLOW, 0), (GREEN, 0.5)],
axis=2
)
# Set up the scene
self.set_camera_orientation(
phi=60 * DEGREES,
theta=45 * DEGREES,
zoom=0.6
)
# Animate
self.begin_ambient_camera_rotation(rate=0.2)
self.play(Create(axes))
self.play(Create(surface))
self.wait(2)
self.stop_ambient_camera_rotation()
'''
def generate_sphere_code():
return '''from manim import *
class MainScene(ThreeDScene):
def construct(self):
# Set up the scene
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
axes = ThreeDAxes(
x_range=[-3, 3],
y_range=[-3, 3],
z_range=[-3, 3],
x_length=6,
y_length=6,
z_length=6
)
# Create sphere
radius = 2
sphere = Surface(
lambda u, v: np.array([
radius * np.cos(u) * np.cos(v),
radius * np.cos(u) * np.sin(v),
radius * np.sin(u)
]),
u_range=[-PI/2, PI/2],
v_range=[0, TAU],
checkerboard_colors=[BLUE_D, BLUE_E],
resolution=(15, 32)
)
# Create radius line and label
radius_line = Line3D(
start=ORIGIN,
end=[radius, 0, 0],
color=YELLOW
)
r_label = Text("r", font_size=36).set_color(YELLOW)
r_label.rotate(PI/2, RIGHT)
r_label.next_to(radius_line, UP)
# Create volume formula
volume_formula = Text(
"V = \\frac{4}{3}\\pi r^3"
).to_corner(UL)
# Add everything to scene
self.add(axes)
self.play(Create(sphere))
self.wait()
self.play(Create(radius_line), Write(r_label))
self.wait()
self.play(Write(volume_formula))
self.wait()
# Rotate camera for better view
self.begin_ambient_camera_rotation(rate=0.2)
self.wait(5)
self.stop_ambient_camera_rotation()'''
def generate_cube_code():
return '''from manim import *
class MainScene(ThreeDScene):
def construct(self):
# Set up the scene
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
axes = ThreeDAxes(
x_range=[-3, 3],
y_range=[-3, 3],
z_range=[-3, 3]
)
# Create cube
cube = Cube(side_length=2, fill_opacity=0.7, stroke_width=2)
cube.set_color(BLUE)
# Labels for sides
a_label = Text("a", font_size=36).set_color(YELLOW)
a_label.next_to(cube, RIGHT)
# Surface area formula
area_formula = Text(
"A = 6a^2"
).to_corner(UL)
# Add everything to scene
self.add(axes)
self.play(Create(cube))
self.wait()
self.play(Write(a_label))
self.wait()
self.play(Write(area_formula))
self.wait()
# Rotate camera for better view
self.begin_ambient_camera_rotation(rate=0.2)
self.wait(5)
self.stop_ambient_camera_rotation()'''
def generate_matrix_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create matrices
matrix_a = VGroup(
Text("2 1"),
Text("1 3")
).arrange(DOWN)
matrix_a.add(SurroundingRectangle(matrix_a))
matrix_b = VGroup(
Text("1"),
Text("2")
).arrange(DOWN)
matrix_b.add(SurroundingRectangle(matrix_b))
# Create multiplication symbol and equals sign
times = Text("×")
equals = Text("=")
# Create result matrix
result = VGroup(
Text("4"),
Text("7")
).arrange(DOWN)
result.add(SurroundingRectangle(result))
# Position everything
equation = VGroup(
matrix_a, times, matrix_b,
equals, result
).arrange(RIGHT)
# Create step-by-step calculations
calc1 = Text("= [2(1) + 1(2)]")
calc2 = Text("= [2 + 2]")
calc3 = Text("= [4]")
calcs = VGroup(calc1, calc2, calc3).arrange(DOWN)
calcs.next_to(equation, DOWN, buff=1)
# Create animations
self.play(Create(matrix_a))
self.play(Create(matrix_b))
self.play(Write(times), Write(equals))
self.play(Create(result))
self.wait()
self.play(Write(calc1))
self.play(Write(calc2))
self.play(Write(calc3))
self.wait()'''
def generate_eigenvalue_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create matrix and vector
matrix = VGroup(
Text("2 1"),
Text("1 2")
).arrange(DOWN)
matrix.add(SurroundingRectangle(matrix))
vector = VGroup(
Text("v₁"),
Text("v₂")
).arrange(DOWN)
vector.add(SurroundingRectangle(vector))
# Create lambda and equation
lambda_text = Text("λ")
equation = Text("Av = λv")
# Position everything
group = VGroup(matrix, vector, lambda_text, equation).arrange(RIGHT)
group.to_edge(UP)
# Create characteristic equation steps
char_eq = Text("det(A - λI) = 0")
expanded = Text("|2-λ 1|")
expanded2 = Text("|1 2-λ|")
solved = Text("(2-λ)² - 1 = 0")
result = Text("λ = 1, 3")
# Position steps
steps = VGroup(
char_eq, expanded, expanded2,
solved, result
).arrange(DOWN)
steps.next_to(group, DOWN, buff=1)
# Create animations
self.play(Create(matrix), Create(vector))
self.play(Write(lambda_text), Write(equation))
self.wait()
self.play(Write(char_eq))
self.play(Write(expanded), Write(expanded2))
self.play(Write(solved))
self.play(Write(result))
self.wait()'''
def generate_complex_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Set up plane
plane = ComplexPlane()
self.play(Create(plane))
# Create complex number
z = 3 + 2j
dot = Dot([3, 2, 0], color=YELLOW)
# Create vector and labels
vector = Arrow(
ORIGIN, dot.get_center(),
buff=0, color=YELLOW
)
re_line = DashedLine(
ORIGIN, [3, 0, 0], color=BLUE
)
im_line = DashedLine(
[3, 0, 0], [3, 2, 0], color=RED
)
# Add labels
z_label = Text("z = 3 + 2i", font_size=36)
z_label.next_to(dot, UR)
re_label = Text("Re(z) = 3", font_size=36)
re_label.next_to(re_line, DOWN)
im_label = Text("Im(z) = 2", font_size=36)
im_label.next_to(im_line, RIGHT)
# Animations
self.play(Create(vector))
self.play(Write(z_label))
self.wait()
self.play(
Create(re_line),
Create(im_line)
)
self.play(
Write(re_label),
Write(im_label)
)
self.wait()'''
def generate_diff_eq_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create differential equation
eq = Text(
"\\frac{dy}{dx} + 2y = e^x"
)
# Solution steps
step1 = Text(
"y = e^{-2x}\\int e^x \\cdot e^{2x} dx"
)
step2 = Text(
"y = e^{-2x}\\int e^{3x} dx"
)
step3 = Text(
"y = e^{-2x} \\cdot \\frac{1}{3}e^{3x} + Ce^{-2x}"
)
step4 = Text(
"y = \\frac{1}{3}e^x + Ce^{-2x}"
)
# Arrange equations
VGroup(
eq, step1, step2, step3, step4
).arrange(DOWN, buff=0.5)
# Create graph
axes = Axes(
x_range=[-2, 2],
y_range=[-2, 2],
axis_config={"include_tip": True}
)
# Plot particular solution (C=0)
graph = axes.plot(
lambda x: (1/3)*np.exp(x),
color=YELLOW
)
# Animations
self.play(Write(eq))
self.wait()
self.play(Write(step1))
self.wait()
self.play(Write(step2))
self.wait()
self.play(Write(step3))
self.wait()
self.play(Write(step4))
self.wait()
# Show graph
self.play(
FadeOut(VGroup(eq, step1, step2, step3, step4))
)
self.play(Create(axes), Create(graph))
self.wait()'''
def generate_trig_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create coordinate plane
plane = NumberPlane(
x_range=[-4, 4],
y_range=[-2, 2],
axis_config={"include_tip": True}
)
# Add custom labels
x_label = Text("x").next_to(plane.x_axis.get_end(), RIGHT)
y_label = Text("y").next_to(plane.y_axis.get_end(), UP)
# Create unit circle
circle = Circle(radius=1, color=BLUE)
# Create angle tracker
theta = ValueTracker(0)
# Create dot that moves around circle
dot = always_redraw(
lambda: Dot(
circle.point_at_angle(theta.get_value()),
color=YELLOW
)
)
# Create lines to show sine and cosine
x_line = always_redraw(
lambda: Line(
start=[circle.point_at_angle(theta.get_value())[0], 0, 0],
end=circle.point_at_angle(theta.get_value()),
color=GREEN
)
)
y_line = always_redraw(
lambda: Line(
start=[0, 0, 0],
end=[circle.point_at_angle(theta.get_value())[0], 0, 0],
color=RED
)
)
# Create labels
sin_label = Text("sin(θ)").next_to(x_line).set_color(GREEN)
cos_label = Text("cos(θ)").next_to(y_line).set_color(RED)
# Add everything to scene
self.play(Create(plane), Write(x_label), Write(y_label))
self.play(Create(circle))
self.play(Create(dot))
self.play(Create(x_line), Create(y_line))
self.play(Write(sin_label), Write(cos_label))
# Animate angle
self.play(
theta.animate.set_value(2*PI),
run_time=4,
rate_func=linear
)
self.wait()'''
def generate_quadratic_code():
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create coordinate system
axes = Axes(
x_range=[-4, 4],
y_range=[-2, 8],
axis_config={"include_tip": True}
)
# Add custom labels
x_label = Text("x").next_to(axes.x_axis.get_end(), RIGHT)
y_label = Text("y").next_to(axes.y_axis.get_end(), UP)
# Create quadratic function
def func(x):
return x**2
graph = axes.plot(
func,
color=BLUE,
x_range=[-3, 3]
)
# Create labels and equation
equation = Text("f(x) = x²").to_corner(UL)
# Create dot and value tracker
x = ValueTracker(-3)
dot = always_redraw(
lambda: Dot(
axes.c2p(
x.get_value(),
func(x.get_value())
),
color=YELLOW
)
)
# Create lines to show x and y values
v_line = always_redraw(
lambda: axes.get_vertical_line(
axes.input_to_graph_point(
x.get_value(),
graph
),
color=RED
)
)
h_line = always_redraw(
lambda: axes.get_horizontal_line(
axes.input_to_graph_point(
x.get_value(),
graph
),
color=GREEN
)
)
# Add everything to scene
self.play(Create(axes), Write(x_label), Write(y_label))
self.play(Create(graph))
self.play(Write(equation))
self.play(Create(dot), Create(v_line), Create(h_line))
# Animate x value
self.play(
x.animate.set_value(3),
run_time=6,
rate_func=there_and_back
)
self.wait()'''
def generate_3d_surface_code():
return '''from manim import *
class MainScene(ThreeDScene):
def construct(self):
# Configure the scene
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
# Create axes
axes = ThreeDAxes()
# Create surface
def func(x, y):
return np.sin(x) * np.cos(y)
surface = Surface(
lambda u, v: axes.c2p(u, v, func(u, v)),
u_range=[-3, 3],
v_range=[-3, 3],
resolution=32,
checkerboard_colors=[BLUE_D, BLUE_E]
)
# Add custom labels
x_label = Text("x").next_to(axes.x_axis.get_end(), RIGHT)
y_label = Text("y").next_to(axes.y_axis.get_end(), UP)
z_label = Text("z").next_to(axes.z_axis.get_end(), OUT)
# Create animations
self.begin_ambient_camera_rotation(rate=0.2)
self.play(Create(axes), Write(x_label), Write(y_label), Write(z_label))
self.play(Create(surface))
self.wait(2)
self.stop_ambient_camera_rotation()
self.wait()'''
def generate_sphere_code():
return '''from manim import *
class MainScene(ThreeDScene):
def construct(self):
# Configure the scene
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
# Create axes
axes = ThreeDAxes()
# Create sphere
sphere = Surface(
lambda u, v: np.array([
np.cos(u) * np.cos(v),
np.cos(u) * np.sin(v),
np.sin(u)
]),
u_range=[-PI/2, PI/2],
v_range=[0, TAU],
checkerboard_colors=[BLUE_D, BLUE_E]
)
# Add custom labels
x_label = Text("x").next_to(axes.x_axis.get_end(), RIGHT)
y_label = Text("y").next_to(axes.y_axis.get_end(), UP)
z_label = Text("z").next_to(axes.z_axis.get_end(), OUT)
# Create animations
self.begin_ambient_camera_rotation(rate=0.2)
self.play(Create(axes), Write(x_label), Write(y_label), Write(z_label))
self.play(Create(sphere))
self.wait(2)
self.stop_ambient_camera_rotation()
self.wait()'''
def generate_manim_code(concept):
"""Generate Manim code based on the concept."""
try:
# First try to find a matching template
return select_template(concept.lower())
except Exception as e:
app.logger.error(f"Error generating Manim code: {str(e)}")
return generate_basic_visualization_code()
def generate_basic_visualization_code():
"""Generate code for basic visualization."""
return '''from manim import *
class MainScene(Scene):
def construct(self):
# Create title
title = Text("Mathematical Visualization", font_size=36).to_edge(UP)
# Create axes
axes = Axes(
x_range=[-5, 5, 1],
y_range=[-3, 3, 1],
axis_config={"include_tip": True},
x_length=10,
y_length=6
)
# Add labels
x_label = Text("x", font_size=24).next_to(axes.x_axis.get_end(), RIGHT)
y_label = Text("y", font_size=24).next_to(axes.y_axis.get_end(), UP)
# Create function graphs
sin_graph = axes.plot(lambda x: np.sin(x), color=BLUE)
cos_graph = axes.plot(lambda x: np.cos(x), color=RED)
# Create labels for functions
sin_label = Text("sin(x)", font_size=24, color=BLUE).next_to(sin_graph, UP)
cos_label = Text("cos(x)", font_size=24, color=RED).next_to(cos_graph, DOWN)
# Create dot to track movement
moving_dot = Dot(color=YELLOW)
moving_dot.move_to(axes.c2p(-5, 0))
# Create path for dot to follow
path = VMobject()
path.set_points_smoothly([
axes.c2p(x, np.sin(x))
for x in np.linspace(-5, 5, 100)
])
# Animate everything
self.play(Write(title))
self.play(Create(axes), Write(x_label), Write(y_label))