README.md
| 1 | --- |
| 2 | license: mit |
| 3 | language: |
| 4 | - sa |
| 5 | - en |
| 6 | pipeline_tag: translation |
| 7 | tags: |
| 8 | - translation |
| 9 | - sanskrit |
| 10 | - nmt |
| 11 | - from-scratch |
| 12 | --- |
| 13 | |
| 14 | # Sanskrit → English — Custom Transformer (trained from scratch) |
| 15 | |
| 16 | A compact encoder–decoder Transformer trained **from scratch** on 10,000 Sanskrit–English pairs |
| 17 | (NLU Assignment 2), with a joint SentencePiece BPE vocabulary. It is small and fast — meant to be |
| 18 | efficient rather than to match large pretrained models. This is **not** a 🤗 Transformers |
| 19 | architecture, so it ships with a self-contained `modeling.py`. |
| 20 | |
| 21 | | | | |
| 22 | |---|---| |
| 23 | | Parameters | ~9.4M | |
| 24 | | Architecture | 4+4 layer Transformer, d_model 256, 4 heads, tied embeddings | |
| 25 | | Vocabulary | 8,000 (joint SentencePiece BPE) | |
| 26 | | Test BLEU / BERTScore-F1 | 0.089 / 0.346 | |
| 27 | | Inference | ~7 ms/sentence | |
| 28 | |
| 29 | Files: `pytorch_model.bin` (weights), `spm.model` (tokenizer), `config.json` (hyperparameters), |
| 30 | `modeling.py` (model + `load`/`translate` helpers). |
| 31 | |
| 32 | ## Install |
| 33 | |
| 34 | ```bash |
| 35 | pip install torch sentencepiece huggingface_hub |
| 36 | ``` |
| 37 | |
| 38 | ## Inference |
| 39 | |
| 40 | ```python |
| 41 | from huggingface_hub import snapshot_download |
| 42 | import sys |
| 43 | |
| 44 | d = snapshot_download("krpraveen/sanskrit-en-custom-transformer") |
| 45 | sys.path.insert(0, d) |
| 46 | from modeling import load, translate |
| 47 | |
| 48 | model, sp, cfg = load(d) # add device="cuda" on a GPU |
| 49 | print(translate(model, sp, cfg, ["बाल: भवत्सु प्रेमं प्रकटयति ।"])) |
| 50 | # ['Boy displays love in you.'] |
| 51 | ``` |
| 52 | |
| 53 | ## Use it from an open-source chat UI (Gradio) |
| 54 | |
| 55 | ```python |
| 56 | import gradio as gr |
| 57 | |
| 58 | def respond(message, history): |
| 59 | return translate(model, sp, cfg, [message])[0] |
| 60 | |
| 61 | gr.ChatInterface( |
| 62 | respond, |
| 63 | title="Sanskrit → English (custom Transformer)", |
| 64 | description="Type a Sanskrit sentence in Devanagari.", |
| 65 | examples=["बाल: भवत्सु प्रेमं प्रकटयति ।", "अस्तु, इदं सम्यक् दृश्यते ।"], |
| 66 | ).launch() |
| 67 | ``` |
| 68 | |
| 69 | `pip install gradio` first. To host it, create a Hugging Face **Space** (SDK: Gradio) with an |
| 70 | `app.py` (the load + `respond` code) and a `requirements.txt` of |
| 71 | `torch sentencepiece huggingface_hub gradio`. |
| 72 | |
| 73 | ## Notes |
| 74 | |
| 75 | Trained only on the provided dataset — no pretrained weights and no external data. Being a |
| 76 | from-scratch model on 10k pairs, quality is modest; a larger model or more data would help. |
| 77 | For higher quality see the fine-tuned IndicTrans2 model |
| 78 | [`krpraveen/indictrans2-sanskrit-en-finetuned`](https://huggingface.co/krpraveen/indictrans2-sanskrit-en-finetuned). |
| 79 | |