File size: 3,391 Bytes
f4b1e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import google.generativeai as genai
import os
import re
import subprocess
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Configure Gemini API with environment variable
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))

model = genai.GenerativeModel(
    model_name="gemini-2.5-pro",
    generation_config={
        "temperature": 0.3,
        "top_p": 0.95,
        "top_k": 40,
        "max_output_tokens": 20000,
    }
)

task="""Advanced DSA Concepts

"""
# Step 1: Generate plan for Manim animation
planning_prompt = f"""

You are a professional with deep expertise in Manim CE 0.18.0.



Your task is to design a presentation-style video using Manim for the topic: "{task}"



 Guidelines:

- The video should consist of **slides of text** with **smooth transitions** between them.

- Structure the scene logically.

-I just want content for 1 minute only.

- Use **simple text objects only**, no equations, graphs, or images.

- Include details like:

    - Text to be shown

    - When to fade in/out

    - Slide duration

    - Transitions (e.g., `FadeIn`, `FadeOut`, `Transform`)

- Do not output any code here โ€” only the **presentation plan**, step by step.



 Output Format:

Scene 1:

- Text: "Title Slide: Second World War"

- Action: FadeIn

- Duration: 3 seconds

...



"""

plan_response = model.generate_content(planning_prompt)
plan = plan_response.text.strip()

# Step 2: Generate Manim code based on the plan
code_prompt = f"""

Using this animation plan:



{plan}



Write a full Manim CE 0.18.0 compatible class called `Video`.



 Important Instructions:

- Style it like a **PowerPoint presentation**: clean layout, one idea per slide.

-You can change the background to white&black glassmorphism and text to black also make it look more professional.

- Only use **text and transitions** (no math or images).

- Each slide should be a simple text message with appropriate **FadeIn, FadeOut, or Transform** effects.

- Do NOT use deprecated methods like `.to_edge()` or `.move_to(ORIGIN)` unnecessarily.

- Use `self.wait()` to control slide durations.

- Add brief pauses between scenes to simulate human-like pacing.



 High Priority:

-Text shouldn't merge with other text.

- Strictly use Manim CE 0.18.0 syntax.

- No explanations. Just return the final code block.



Output: Just the Python code for the Manim scene.



"""

final_code = model.generate_content(code_prompt)
print(final_code.text.strip())
# Get the LLM output
raw_code = final_code.text.strip()

# Remove leading ```python or ``` and trailing ``` if present
# Also handles '''python and ''' for safety
cleaned_code = re.sub(r"^(```|''')python\s*", "", raw_code)
cleaned_code = re.sub(r"(```|''')\s*$", "", cleaned_code)

# Set base filename
base_filename = "new"
extension = ".py"
i = 0

# Find next available filename
while os.path.exists(f"{base_filename}{i}{extension}"):
    i += 1

filename = f"{base_filename}{i}{extension}"

# Save cleaned code
with open(filename, "w", encoding="utf-8") as f:
    f.write(cleaned_code)

print(f"Code saved to {filename}")

try:
    print(f"๐Ÿš€ Running {filename} ...\n")
    subprocess.run(["manim", "-pql", filename, "Video"], check=True)
except subprocess.CalledProcessError as e:
    print(f"โŒ Error running {filename}:\n{e}")