Harish102005 commited on
Commit
68f009d
·
verified ·
1 Parent(s): b557b89

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +248 -2
README.md CHANGED
@@ -46,6 +46,252 @@ inference:
46
  max_new_tokens: 512
47
  ---
48
 
49
- # 🎬 Qwen2.5-Coder-7B Fine-tuned for Manim Code Generation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- [Rest of README content here...]
 
46
  max_new_tokens: 512
47
  ---
48
 
49
+ ---
50
+
51
+ # Qwen2.5-Coder-7B-Manim
52
+
53
+ [![Model on HF](https://huggingface.co/datasets/huggingface/badges/resolve/main/model-on-hf-md.svg)](https://huggingface.co/Harish102005/Qwen2.5-Coder-7B-manim)
54
+
55
+ [![Base Model](https://img.shields.io/badge/Base_Model-Qwen2.5--Coder--7B-green)](https://huggingface.co/Qwen/Qwen2.5-Coder-7B)
56
+
57
+ **Generate Manim (Mathematical Animation Engine) Python code from natural language descriptions!**
58
+ Fine-tuned on **2,407 examples** from the 3Blue1Brown Manim dataset using **QLoRA** with Unsloth.
59
+
60
+ ---
61
+
62
+ ## 🚀 Quick Start
63
+
64
+ ### Installation
65
+
66
+ ```bash
67
+ pip install unsloth transformers accelerate
68
+ ```
69
+
70
+ ### Load Model
71
+
72
+ ```python
73
+ from unsloth import FastLanguageModel
74
+
75
+ model, tokenizer = FastLanguageModel.from_pretrained(
76
+ model_name="Harish102005/Qwen2.5-Coder-7B-manim",
77
+ max_seq_length=2048,
78
+ dtype=None,
79
+ load_in_4bit=True,
80
+ )
81
+ FastLanguageModel.for_inference(model)
82
+ ```
83
+
84
+ ### Generate Manim Code
85
+
86
+ ```python
87
+ # Alpaca-style prompt template
88
+ 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.
89
+
90
+ ### Instruction:
91
+ {}
92
+
93
+ ### Input:
94
+ {}
95
+
96
+ ### Response:
97
+ {}"""
98
+
99
+ prompt = "Create a blue circle that grows to twice its size"
100
+
101
+ inputs = tokenizer([
102
+ alpaca_prompt.format(
103
+ "Generate Manim code for the following task:",
104
+ prompt,
105
+ ""
106
+ )
107
+ ], return_tensors="pt").to("cuda")
108
+
109
+ outputs = model.generate(
110
+ **inputs,
111
+ max_new_tokens=512,
112
+ temperature=0.3,
113
+ top_p=0.9,
114
+ repetition_penalty=1.1,
115
+ do_sample=True,
116
+ )
117
+
118
+ generated_code = tokenizer.decode(outputs, skip_special_tokens=True)
119
+ print(generated_code.split("### Response:")[-1].strip())
120
+ ```
121
+
122
+ ---
123
+
124
+ ### Helper Function
125
+
126
+ ```python
127
+ def generate_manim_code(prompt, max_tokens=512):
128
+ 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.
129
+
130
+ ### Instruction:
131
+ {}
132
+
133
+ ### Input:
134
+ {}
135
+
136
+ ### Response:
137
+ {}"""
138
+
139
+ formatted_prompt = alpaca_prompt.format(
140
+ "Generate Manim code for the following task:",
141
+ prompt,
142
+ ""
143
+ )
144
+
145
+ inputs = tokenizer([formatted_prompt], return_tensors="pt").to("cuda")
146
+ outputs = model.generate(
147
+ **inputs,
148
+ max_new_tokens=max_tokens,
149
+ temperature=0.3,
150
+ top_p=0.9,
151
+ repetition_penalty=1.1,
152
+ do_sample=True,
153
+ )
154
+
155
+ generated_text = tokenizer.decode(outputs, skip_special_tokens=True)
156
+ if "### Response:" in generated_text:
157
+ code = generated_text.split("### Response:")[-1].strip()
158
+ if "### Instruction:" in code:
159
+ code = code.split("### Instruction:")[0].strip()
160
+ return code
161
+
162
+ return generated_text
163
+
164
+ # Example
165
+ code = generate_manim_code("Create a rotating square")
166
+ print(code)
167
+ ```
168
+
169
+ ---
170
+
171
+ ## 📊 Example Outputs
172
+
173
+ ### 1. Color-Changing Rotation
174
+
175
+ **Prompt:** `"Create a rotating square that changes color from blue to red"`
176
+
177
+ ```python
178
+ from manim import *
179
+
180
+ class MyScene(Scene):
181
+ def construct(self):
182
+ square = Square(color=BLUE)
183
+ self.add(square)
184
+ self.play(square.animate.rotate(PI), run_time=2)
185
+ square.set_color(RED)
186
+ ```
187
+
188
+ ### 2. Mathematical Function
189
+
190
+ **Prompt:** `"Draw a sine wave from 0 to 2π with animation"`
191
+
192
+ ```python
193
+ from manim import *
194
+
195
+ class MyScene(Scene):
196
+ def construct(self):
197
+ axes = Axes(x_range=[0, 2*PI], y_range=[-1, 1])
198
+ graph = axes.plot(lambda x: np.sin(x), color=BLUE)
199
+ self.add(axes, graph)
200
+ ```
201
+
202
+ ### 3. Formula Display
203
+
204
+ **Prompt:** `"Show the equation E=mc² and fade it in"`
205
+
206
+ ```python
207
+ from manim import *
208
+
209
+ class MyScene(Scene):
210
+ def construct(self):
211
+ e_mc_squared = MathTex("E=mc^2")
212
+ self.play(Write(e_mc_squared))
213
+ self.wait()
214
+ ```
215
+
216
+ ---
217
+
218
+ ## 📈 Model Details
219
+
220
+ * **Base Model:** [Qwen/Qwen2.5-Coder-7B](https://huggingface.co/Qwen/Qwen2.5-Coder-7B)
221
+ * **Fine-tuning Method:** QLoRA (4-bit) with [Unsloth](https://github.com/unslothai/unsloth)
222
+ * **Dataset:** [dalle2/3blue1brown-manim](https://huggingface.co/datasets/dalle2/3blue1brown-manim)
223
+ * **Dataset Size:** 2,407 prompt-code pairs
224
+ * **Final Training Loss:** 0.553
225
+ * **Model Type:** Qwen2ForCausalLM
226
+ * **Parameters:** ~7.6B (base), Trainable: 40.4M (0.53%)
227
+
228
+ ### Hyperparameters
229
+
230
+ | Parameter | Value |
231
+ | ------------------- | ------------------------------------------------------------- |
232
+ | LoRA Rank (r) | 16 |
233
+ | LoRA Alpha | 16 |
234
+ | LoRA Dropout | 0.0 |
235
+ | Target Modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
236
+ | Max Sequence Length | 2048 |
237
+ | Precision | BFloat16 |
238
+ | Quantization | 4-bit NF4 (double quantization) |
239
+
240
+ ---
241
+
242
+ ## 🎯 Use Cases
243
+
244
+ * Generate educational animations (math tutorials, visualizations)
245
+ * Rapid prototyping of visual content in Manim
246
+ * Learning Manim syntax and animation techniques
247
+ * Content automation (batch animation generation)
248
+
249
+ ---
250
+
251
+ ## ⚠️ Limitations
252
+
253
+ * Primarily for **2D Manim animations**; may struggle with complex 3D scenes
254
+ * Training data limited to **3Blue1Brown patterns** (2,407 examples)
255
+ * Minor manual corrections may be needed for complex animations
256
+ * Advanced Manim features (custom shaders, complex mobjects) not fully supported
257
+
258
+ ---
259
+
260
+ ## 🔧 Advanced Usage
261
+
262
+ ### Streaming Output
263
+
264
+ ```python
265
+ from transformers import TextStreamer
266
+
267
+ text_streamer = TextStreamer(tokenizer, skip_prompt=True)
268
+ _ = model.generate(**inputs, streamer=text_streamer, max_new_tokens=512, temperature=0.3)
269
+ ```
270
+
271
+ ### Batch Generation
272
+
273
+ ```python
274
+ prompts = ["Create a blue circle", "Draw a red square", "Show a green triangle"]
275
+
276
+ for prompt in prompts:
277
+ code = generate_manim_code(prompt)
278
+ print(f"Prompt: {prompt}\n{code}\n{'-'*60}")
279
+ ```
280
+
281
+ ---
282
+
283
+ ## 🙏 Acknowledgments
284
+
285
+ * **Base Model:** [Qwen Team](https://github.com/QwenLM/Qwen)
286
+ * **Dataset:** [dalle2](https://huggingface.co/datasets/dalle2)
287
+ * **Training Framework:** [Unsloth](https://github.com/unslothai/unsloth)
288
+ * **Inspiration:** [3Blue1Brown](https://www.3blue1brown.com/) and the Manim Community
289
+
290
+ ---
291
+
292
+ ---
293
+
294
+ ✅ **Star this model** if you find it useful!
295
+
296
+ ---
297