Text Generation
Transformers
Safetensors
English
mistral
text-to-code
manim
python
fine-tuned
lora
qlora
conversational
text-generation-inference
4-bit precision
bitsandbytes
Instructions to use TheSon2202/mistral-manim-python-coder-v01 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use TheSon2202/mistral-manim-python-coder-v01 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="TheSon2202/mistral-manim-python-coder-v01") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("TheSon2202/mistral-manim-python-coder-v01") model = AutoModelForCausalLM.from_pretrained("TheSon2202/mistral-manim-python-coder-v01", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use TheSon2202/mistral-manim-python-coder-v01 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "TheSon2202/mistral-manim-python-coder-v01" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TheSon2202/mistral-manim-python-coder-v01", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/TheSon2202/mistral-manim-python-coder-v01
- SGLang
How to use TheSon2202/mistral-manim-python-coder-v01 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "TheSon2202/mistral-manim-python-coder-v01" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TheSon2202/mistral-manim-python-coder-v01", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "TheSon2202/mistral-manim-python-coder-v01" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TheSon2202/mistral-manim-python-coder-v01", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use TheSon2202/mistral-manim-python-coder-v01 with Docker Model Runner:
docker model run hf.co/TheSon2202/mistral-manim-python-coder-v01
| 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%** | | |
|  | |
| > **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) | |
| ``` |