Video-generator / video_generator.py
parthraninga's picture
Upload 8 files
1dfc9df verified
raw
history blame
3.39 kB
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}")