File size: 3,266 Bytes
6d34b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
license: mit
language:
- sa
- en
tags:
- sanskrit
- paraphrase
- diffusion
- d3pm
- pytorch
pipeline_tag: text2text-generation
---

# Sanskrit D3PM Encoder-Decoder Model

Roman/IAST Sanskrit input to Devanagari output using a custom D3PM checkpoint.
This package is configured for the `d3pm_encoder_decoder` checkpoint stored in
`best_model.pt`.
Hugging Face model repo: `bhsinghgrid/devflow2`

## Files Included

- `best_model.pt` — trained checkpoint
- `model_settings.json` — packaged runtime metadata
- `config.py` — runtime config
- `inference.py` — model loading + generation loop
- `inference_api.py` — simple Python API (`predict`)
- `handler.py` — Hugging Face Endpoint handler
- `model/`, `diffusion/` — architecture modules
- `sanskrit_src_tokenizer.json`, `sanskrit_tgt_tokenizer.json` — tokenizers

## Quick Local Test

```python
from inference_api import predict
print(predict("dharmo rakṣati rakṣitaḥ")["output"])
```

## Runtime Settings

For local/API usage, the runtime first reads `model_settings.json`, then allows
optional environment variable overrides:

- `HF_MODEL_TYPE` = `d3pm_cross_attention` or `d3pm_encoder_decoder`
- `HF_INCLUDE_NEG` = `true` or `false`
- `HF_NUM_STEPS` = diffusion step count for the packaged checkpoint

Packaged settings for this repo:

```bash
export HF_MODEL_TYPE=d3pm_encoder_decoder
export HF_INCLUDE_NEG=false
export HF_NUM_STEPS=4
```

## Use This Model In A Hugging Face Space

In your Space settings, set:

- `HF_CHECKPOINT_REPO=bhsinghgrid/devflow2`
- `HF_CHECKPOINT_FILE=best_model.pt`

If your Space reads model metadata automatically, no extra model-type variables
are required. If it does not, also set:

```bash
HF_DEFAULT_MODEL_TYPE=d3pm_encoder_decoder
HF_DEFAULT_INCLUDE_NEG=false
HF_DEFAULT_NUM_STEPS=4
```

## Transformer-Style Usage (Custom Runtime)

This checkpoint is a custom D3PM architecture (`.pt`), not a native `transformers`
`AutoModel` format. Use it via the provided runtime:

```python
import torch
from config import CONFIG
from inference import load_model, run_inference, _decode_clean
from model.tokenizer import SanskritSourceTokenizer, SanskritTargetTokenizer

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model, cfg = load_model("best_model.pt", CONFIG, device)

src_tok = SanskritSourceTokenizer(vocab_size=16000, max_len=cfg["model"]["max_seq_len"])
tgt_tok = SanskritTargetTokenizer(vocab_size=16000, max_len=cfg["model"]["max_seq_len"])

text = "dharmo rakṣati rakṣitaḥ"
ids = torch.tensor([src_tok.encode(text)], dtype=torch.long, device=device)
out = run_inference(model, ids, cfg)
print(_decode_clean(tgt_tok, out[0].tolist()))
```

If you need full `transformers` compatibility (`AutoModel.from_pretrained`),
export weights to a Hugging Face Transformers model format first.

## Endpoint Payload

```json
{
  "inputs": "yadā mano nivarteta viṣayebhyaḥ svabhāvataḥ",
  "parameters": {
    "temperature": 0.7,
    "top_k": 40,
    "repetition_penalty": 1.2,
    "diversity_penalty": 0.0,
    "num_steps": 4,
    "clean_output": true
  }
}
```

## Push This Folder To Model Hub

```bash
cd hf_model_repo_encoder_decoder
git add .
git commit -m "Add encoder-decoder T4 model package"
git push -u hf main
```