Views
No views yet
pip install unsloth transformers accelerate1from unsloth import FastLanguageModel
2
3model, tokenizer = FastLanguageModel.from_pretrained(
4 model_name="Harish102005/Qwen2.5-Coder-7B-manim",
5 max_seq_length=2048,
6 dtype=None,
7 load_in_4bit=True,
8)
9FastLanguageModel.for_inference(model)1# Alpaca-style prompt template
2alpaca_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.
3
4### Instruction:
5{}
6
7### Input:
8{}
9
10### Response:
11{}"""
12
13prompt = "Create a blue circle that grows to twice its size"
14
15inputs = tokenizer([
16 alpaca_prompt.format(
17 "Generate Manim code for the following task:",
18 prompt,
19 ""
20 )
21], return_tensors="pt").to("cuda")
22
23outputs = model.generate(
24 **inputs,
25 max_new_tokens=512,
26 temperature=0.3,
27 top_p=0.9,
28 repetition_penalty=1.1,
29 do_sample=True,
30)
31
32generated_code = tokenizer.decode(outputs, skip_special_tokens=True)
33print(generated_code.split("### Response:")[-1].strip())1def generate_manim_code(prompt, max_tokens=512):
2 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.
3
4### Instruction:
5{}
6
7### Input:
8{}
9
10### Response:
11{}"""
12
13 formatted_prompt = alpaca_prompt.format(
14 "Generate Manim code for the following task:",
15 prompt,
16 ""
17 )
18
19 inputs = tokenizer([formatted_prompt], return_tensors="pt").to("cuda")
20 outputs = model.generate(
21 **inputs,
22 max_new_tokens=max_tokens,
23 temperature=0.3,
24 top_p=0.9,
25 repetition_penalty=1.1,
26 do_sample=True,
27 )
28
29 generated_text = tokenizer.decode(outputs, skip_special_tokens=True)
30 if "### Response:" in generated_text:
31 code = generated_text.split("### Response:")[-1].strip()
32 if "### Instruction:" in code:
33 code = code.split("### Instruction:")[0].strip()
34 return code
35
36 return generated_text
37
38# Example
39code = generate_manim_code("Create a rotating square")
40print(code)"Create a rotating square that changes color from blue to red"1from manim import *
2
3class MyScene(Scene):
4 def construct(self):
5 square = Square(color=BLUE)
6 self.add(square)
7 self.play(square.animate.rotate(PI), run_time=2)
8 square.set_color(RED)"Draw a sine wave from 0 to 2π with animation"1from manim import *
2
3class MyScene(Scene):
4 def construct(self):
5 axes = Axes(x_range=[0, 2*PI], y_range=[-1, 1])
6 graph = axes.plot(lambda x: np.sin(x), color=BLUE)
7 self.add(axes, graph)"Show the equation E=mc² and fade it in"1from manim import *
2
3class MyScene(Scene):
4 def construct(self):
5 e_mc_squared = MathTex("E=mc^2")
6 self.play(Write(e_mc_squared))
7 self.wait()| Parameter | Value |
|---|---|
| LoRA Rank (r) | 16 |
| LoRA Alpha | 16 |
| LoRA Dropout | 0.0 |
| Target Modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
| Max Sequence Length | 2048 |
| Precision | BFloat16 |
| Quantization | 4-bit NF4 (double quantization) |
1from transformers import TextStreamer
2
3text_streamer = TextStreamer(tokenizer, skip_prompt=True)
4_ = model.generate(**inputs, streamer=text_streamer, max_new_tokens=512, temperature=0.3)1prompts = ["Create a blue circle", "Draw a red square", "Show a green triangle"]
2
3for prompt in prompts:
4 code = generate_manim_code(prompt)
5 print(f"Prompt: {prompt}\n{code}\n{'-'*60}")