CoolFace
Modelpublic

Harish102005/Qwen2.5-Coder-7B-manim

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
2likes92downloads
Model Card

Qwen2.5-Coder-7B-Manim

![Model on HF](https://huggingface.co/Harish102005/Qwen2.5-Coder-7B-manim)

![Base Model](https://huggingface.co/Qwen/Qwen2.5-Coder-7B)

Generate Manim (Mathematical Animation Engine) Python code from natural language descriptions! Fine-tuned on 2,407 examples from the 3Blue1Brown Manim dataset using QLoRA with Unsloth.


๐Ÿš€ Quick Start

Installation

bash
pip install unsloth transformers accelerate

Load Model

python
from unsloth import FastLanguageModel

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="Harish102005/Qwen2.5-Coder-7B-manim",
    max_seq_length=2048,
    dtype=None,
    load_in_4bit=True,
)
FastLanguageModel.for_inference(model)

Generate Manim Code

python
# Alpaca-style prompt template
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{}

### Input:
{}

### Response:
{}"""

prompt = "Create a blue circle that grows to twice its size"

inputs = tokenizer([
    alpaca_prompt.format(
        "Generate Manim code for the following task:",
        prompt,
        ""
    )
], return_tensors="pt").to("cuda")

outputs = model.generate(
    **inputs,
    max_new_tokens=512,
    temperature=0.3,
    top_p=0.9,
    repetition_penalty=1.1,
    do_sample=True,
)

generated_code = tokenizer.decode(outputs, skip_special_tokens=True)
print(generated_code.split("### Response:")[-1].strip())

Helper Function

python
def generate_manim_code(prompt, max_tokens=512):
    alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{}

### Input:
{}

### Response:
{}"""
    
    formatted_prompt = alpaca_prompt.format(
        "Generate Manim code for the following task:",
        prompt,
        ""
    )
    
    inputs = tokenizer([formatted_prompt], return_tensors="pt").to("cuda")
    outputs = model.generate(
        **inputs,
        max_new_tokens=max_tokens,
        temperature=0.3,
        top_p=0.9,
        repetition_penalty=1.1,
        do_sample=True,
    )
    
    generated_text = tokenizer.decode(outputs, skip_special_tokens=True)
    if "### Response:" in generated_text:
        code = generated_text.split("### Response:")[-1].strip()
        if "### Instruction:" in code:
            code = code.split("### Instruction:")[0].strip()
        return code
    
    return generated_text

# Example
code = generate_manim_code("Create a rotating square")
print(code)

๐Ÿ“Š Example Outputs

1. Color-Changing Rotation

Prompt: "Create a rotating square that changes color from blue to red"

python
from manim import *

class MyScene(Scene):
    def construct(self):
        square = Square(color=BLUE)
        self.add(square)
        self.play(square.animate.rotate(PI), run_time=2)
        square.set_color(RED)

2. Mathematical Function

Prompt: "Draw a sine wave from 0 to 2ฯ€ with animation"

python
from manim import *

class MyScene(Scene):
    def construct(self):
        axes = Axes(x_range=[0, 2*PI], y_range=[-1, 1])
        graph = axes.plot(lambda x: np.sin(x), color=BLUE)
        self.add(axes, graph)

3. Formula Display

Prompt: "Show the equation E=mcยฒ and fade it in"

python
from manim import *

class MyScene(Scene):
    def construct(self):
        e_mc_squared = MathTex("E=mc^2")
        self.play(Write(e_mc_squared))
        self.wait()

๐Ÿ“ˆ Model Details

  • โ€”Base Model: Qwen/Qwen2.5-Coder-7B
  • โ€”Fine-tuning Method: QLoRA (4-bit) with Unsloth
  • โ€”Dataset: dalle2/3blue1brown-manim
  • โ€”Dataset Size: 2,407 prompt-code pairs
  • โ€”Final Training Loss: 0.553
  • โ€”Model Type: Qwen2ForCausalLM
  • โ€”Parameters: ~7.6B (base), Trainable: 40.4M (0.53%)

Hyperparameters

ParameterValue
LoRA Rank (r)16
LoRA Alpha16
LoRA Dropout0.0
Target Modulesqproj, kproj, vproj, oproj, gateproj, upproj, down_proj
Max Sequence Length2048
PrecisionBFloat16
Quantization4-bit NF4 (double quantization)

๐ŸŽฏ Use Cases

  • โ€”Generate educational animations (math tutorials, visualizations)
  • โ€”Rapid prototyping of visual content in Manim
  • โ€”Learning Manim syntax and animation techniques
  • โ€”Content automation (batch animation generation)

โš ๏ธ Limitations

  • โ€”Primarily for 2D Manim animations; may struggle with complex 3D scenes
  • โ€”Training data limited to 3Blue1Brown patterns (2,407 examples)
  • โ€”Minor manual corrections may be needed for complex animations
  • โ€”Advanced Manim features (custom shaders, complex mobjects) not fully supported

๐Ÿ”ง Advanced Usage

Streaming Output

python
from transformers import TextStreamer

text_streamer = TextStreamer(tokenizer, skip_prompt=True)
_ = model.generate(**inputs, streamer=text_streamer, max_new_tokens=512, temperature=0.3)

Batch Generation

python
prompts = ["Create a blue circle", "Draw a red square", "Show a green triangle"]

for prompt in prompts:
    code = generate_manim_code(prompt)
    print(f"Prompt: {prompt}\n{code}\n{'-'*60}")

๐Ÿ™ Acknowledgments



โœ… Star this model if you find it useful!