File size: 4,741 Bytes
f0dda20
37cd56f
 
 
 
 
 
 
 
 
 
 
 
7012dfa
 
2874b75
 
 
3d513c4
f0dda20
 
37cd56f
f0dda20
37cd56f
f0dda20
37cd56f
f0dda20
37cd56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0dda20
37cd56f
f0dda20
37cd56f
f0dda20
37cd56f
f0dda20
37cd56f
 
 
 
 
f0dda20
 
37cd56f
f0dda20
37cd56f
f0dda20
37cd56f
f0dda20
37cd56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0dda20
37cd56f
f0dda20
37cd56f
 
f0dda20
37cd56f
 
 
 
 
 
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
---
language:
- en
license: apache-2.0
tags:
- text-to-code
- manim
- python
- mistral
- fine-tuned
- lora
- qlora
base_model: mistralai/Mistral-7B-v0.3
datasets:
- Edoh/manim_python
metrics:
- accuracy
pipeline_tag: text-generation
library_name: transformers
---

# Mistral Manim Python Coder (`TheSon2202/mistral-manim-python-coder-v01`)

This model is a fine-tuned version of **Mistral-7B-v0.3** using **QLoRA (4-bit NF4)**, specialized in translating natural language instructions (**Text-to-Instruction**) into precise Python code for the mathematical animation library **Manim**.

---

## 1. Hyperparameters & Configuration

| Configuration Parameter | Value |
| :--- | :--- |
| **Base Model** | `mistralai/Mistral-7B-v0.3` |
| **Dataset** | `Edoh/manim_python` |
| **Maximum Sequence Length** | `512` tokens |
| **Learning Rate** | `2e-4` (0.0002) |
| **Weight Decay** | `0.03` |
| **Per-Device Batch Size** | `2` |
| **Gradient Accumulation Steps** | `4` |
| **Number of Epochs** | `2` (Total 120 steps) |
| **Optimizer** | `paged_adamw_32bit` |
| **LR Scheduler** | `cosine` |
| **Gradient Clipping (`max_grad_norm`)** | `0.3` |
| **Warmup Steps Ratio** | `0.1` (10%) |

### PEFT (LoRA) Config
* **Rank (`r`):** `16`
* **Alpha (`lora_alpha`):** `32`
* **Dropout (`lora_dropout`):** `0.05`
* **Target Modules:** `["q_proj", "k_proj", "v_proj", "o_proj"]`
* **Task Type:** `CAUSAL_LM`

### Quantization Config (BitsAndBytes)
* **Load in 4-bit:** `True`
* **Quant Type:** `nf4` (Normal Float 4)
* **Compute Dtype:** `torch.float16`
* **Double Quantization:** `True`

---

## 2. Training Metrics & Evaluation Results

The training process recorded convergence milestones across checkpoints (saved periodically every 50 steps):

| Training Step | Training Loss | Validation Loss | Num Tokens | Mean Token Accuracy |
| :---: | :---: | :---: | :---: | :---: |
| **Step 50** | `0.2506` | `0.2504` | 41,922 | **94.41%** |
| **Step 100** | `0.2271` | `0.2374` | 83,632 | **94.83%** |
| **Step 120 (Final)** | `0.2259` | `0.2359` | 100,332 | **94.88%** |


![Screenshot 2026-08-05 at 03.12.42](https://cdn-uploads.huggingface.co/production/uploads/68c7b25941cddd7ea63d8e89/9HeDsCgzfa3dBj2oSgGWk.png)

> **General Overview:** Both training and validation losses decreased steadily and closely tracked each other (showing no signs of overfitting). Combined with an average token accuracy of approximately **94.88%**, this demonstrates that the model successfully learned Manim's syntax and programming conventions.

---

## 3. Inference Demo

You can load the model directly from the Hugging Face Hub to generate Manim code using the following Python snippet:

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "TheSon2202/mistral-manim-python-coder-v01"

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="auto",
    torch_dtype=torch.float16
)

# Configure Chat Template for Mistral Base Model
tokenizer.chat_template = (
    "{{ bos_token }}"
    "{% for message in messages %}"
        "{% if message['role'] == 'system' %}"
            "{{ 'System: ' + message['content'] + '\n\n' }}"
        "{% elif message['role'] == 'user' %}"
            "{{ '[INST] ' + message['content'] + ' [/INST]' }}"
        "{% elif message['role'] == 'assistant' %}"
            "{{ ' ' + message['content'] + eos_token }}"
        "{% endif %}"
    "{% endfor %}"
)

def generate_manim_code(instruction):
    system_prompt = "Yor are an Coding Python Expert, read the instruction and complete these code correctly"
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": instruction}
    ]
    
    prompt = tokenizer.apply_chat_template(
        messages, 
        tokenize=False, 
        add_generation_prompt=True
    )
    
    inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=256,
            temperature=0.2,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
    
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Test code generation
test_instruction = "Create a square with side length 4 and color it red, then animate it to shift right by 3 units."
print(generate_manim_code(test_instruction))
```
---

### 📤 Expected Output (Clean Python Code)

```python
from manim import *

class MyScene(Scene):
    def construct(self):
        square = Square(side_length=4, color=RED)
        self.add(square)
        self.play(square.animate.shift(RIGHT * 3), run_time=3)
```