Views
No views yet
| Metric | Score |
|---|---|
| Exact Match Accuracy | 42.60% |
| BLEU (F1) | 93.39% |
| Edit Distance Similarity | 90.56% |
pip install torch transformers peft accelerate bitsandbytes1import torch
2from peft import AutoPeftModelForCausalLM
3from transformers import AutoTokenizer
4
5# Load model with LoRA adapters
6model = AutoPeftModelForCausalLM.from_pretrained(
7 "stefanj0/qwen2.5-3b-qlora-latex-fluentmath",
8 device_map="auto",
9 torch_dtype=torch.bfloat16,
10)
11tokenizer = AutoTokenizer.from_pretrained("stefanj0/qwen2.5-3b-qlora-latex-fluentmath")
12
13def translate_latex(latex: str) -> str:
14 """Translate LaTeX to FluentMath."""
15 user_prompt = (
16 "Convert the following LaTeX mathematical expression to natural spoken English:\n\n"
17 f"LaTeX: {latex}\n\n"
18 "Spoken form:"
19 )
20
21 # Format as chat
22 prompt = tokenizer.apply_chat_template(
23 [{"role": "user", "content": user_prompt}],
24 tokenize=False,
25 add_generation_prompt=True
26 )
27
28 # Generate
29 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
30 with torch.no_grad():
31 outputs = model.generate(
32 **inputs,
33 max_new_tokens=256,
34 pad_token_id=tokenizer.eos_token_id,
35 )
36
37 # Parse output
38 generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
39 if "Spoken form:" in generated:
40 verbalization = generated.split("Spoken form:")[-1].strip()
41 else:
42 verbalization = generated.strip()
43
44 # Remove role prefix if present
45 if verbalization.startswith("assistant\n"):
46 verbalization = verbalization[10:].strip()
47
48 return verbalization
49
50# Example usage
51latex = r"\int_a^b f'(x)\,dx = f(b) - f(a)"
52translation = translate_latex(latex)
53print(translation)
54# Output: "the integral from eigh to b of, f prime, of x, d x, equals, f of b minus f of eigh"| LaTeX | FluentMath Output |
|---|---|
\frac{a}{b} | a over b |
x^2 + y^2 = r^2 | x squared plus y squared equals r squared |
\int_a^b f'(x)\,dx = f(b) - f(a) | the integral from eigh to b of, f prime, of x, d x, equals, f of b minus f of eigh |
P(A|B) = \frac{P(B|A)P(A)}{P(B)} | P, the quantity Eigh divides B, equals, the fraction with numerator, P, the quantity B divides Eigh, P of Eigh, and denominator P of B |
\lim_{n\to\infty} \sum_{k=1}^{n} \frac{1}{n} | the limit as n approaches infinity, of, sum from k equals 1 to n of, 1 over n |
A\vec{v} = \lambda\vec{v} | Eigh, bold v, equals, lambda bold v |
i\hbar\frac{\partial}{\partial t}\Psi = \hat{H}\Psi | i h bar, the fraction with numerator, partial derivative, and denominator partial derivative t, Psi, equals, H hat, Psi |
\newcommand or custom LaTeX macros1@misc{qwen25-3b-qlora-latex-fluentmath,
2 author = {Stefan J.},
3 title = {Qwen2.5-3B QLoRA: LaTeX to FluentMath Translation},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/stefanj0/qwen2.5-3b-qlora-latex-fluentmath}
7}1@article{qwen2.5,
2 title={Qwen2.5: A Party of Foundation Models},
3 author={Qwen Team},
4 year={2024},
5 journal={arXiv preprint arXiv:2412.xxxxx}
6}