Views
No views yet
| Setup (Training → Input/Output) | Full (%) | Partial (%) | Invalid 32 (%) | Semantic (%) |
|---|---|---|---|---|
| Ground Truth (DEV Sanskrit + Hindi Prose) | 99.51 | 99.51 | 0.00 | 74.04 |
Phi-4 (SLP1 → SLP1+0-shot/SLP1) [*] greedy | 24.76 | 62.44 | 37.68 | 73.23 |
| Phi-4 (DEV → DEV+0-shot/DEV) greedy | 43.08 | 66.99 | 23.91 | 73.76 |
| Phi-4 (DEV → DEV+3-shot/DEV) greedy | 50.97 | 72.81 | 21.84 | 73.21 |
| Phi-4 (DEV → DEV+6-shot/DEV) greedy | 46.72 | 69.05 | 22.33 | 72.93 |
| Phi-4 (DEV → DEV+0-shot/DEV) Sampling | 42.90 | 64.68 | 21.78 | 72.71 |
| Phi-4 (DEV → DEV+3-shot/DEV) Sampling | 51.70 | 72.51 | 20.81 | 72.05 |
[*] Evaluation performed in Devanagari (SLP1 outputs converted to DEV)1import torch
2import pandas as pd
3from transformers import AutoTokenizer, AutoModelForCausalLM
4from unsloth.chat_templates import get_chat_template
5
6MAX_NEW_TOKENS = 110
7device = "cuda" if torch.cuda.is_available() else "cpu"
8
9model_path = "sanganaka/phi4-hindi2sanskrit-anustubh-lora-merged-step3400"
10
11tokenizer = AutoTokenizer.from_pretrained(model_path)
12tokenizer = get_chat_template(tokenizer, chat_template="phi-4")
13if tokenizer.pad_token is None:
14 tokenizer.pad_token = tokenizer.eos_token
15
16model = AutoModelForCausalLM.from_pretrained(
17 model_path,
18 torch_dtype=torch.bfloat16,
19)
20model.to(device)
21model.eval()
22
23print("MODEL LOADED")
24
25ANUSHTUP_INSTRUCTION = """The goal is to generate Sanskrit verse that follows the anushtup meter rules for the given input text.
26RULES:
27Verse Rules:
28The verse contains 32 syllables/akshara and 4 padas in total.
29The verse is divided into 2 lines, each containing 16 syllables.
30Each line is divided into 2 padas (quartets), each containing exactly 8 syllables.
31The fifth syllable of every pada must be LAGHU or short.
32The sixth syllable of every pada must be GURU or long.
33The seventh syllable of the second and fourth pada must be HRASVA.
34The seventh syllable of the first and third pada must be DEERGHA.
35
36Syllable Rules:
37LAGHU vowels: अ, इ, उ, ऋ, ऌ
38GURU vowels: आ, ई, ऊ, ॠ, ॡ, ए, ऐ, ओ, औ
39HRASVA vowels: अ, इ, उ, ऋ, ऌ
40DEERGHA vowels: आ, ई, ऊ, ॠ, ॡ, ए, ऐ, ओ, औ
41
42Syllable classification rules:
43- A syllable is marked Laghu/Guru and Hrasva/Deergha based on the vowel it contains.
44- Any syllable containing anusvāra (ं) or visarga (ः) is always Guru.
45- Any syllable followed by a conjunct consonant (saṁyuktākṣara) is always Guru.
46
47Now convert the given Hindi text into a Sanskrit Anushtup verse in Devanagari:"""
48
49def generate(hi_text):
50 messages = [
51 {"role": "system", "content": ANUSHTUP_INSTRUCTION},
52 {"role": "user", "content": "यह समय वाणीकी पहुँचके परे था उसका वर्णन करना कठिन था उस समय कोई भूपाल वहाँ इस विषयमें कुछ भी न बोल सके मौन रह गये वे बारचार केवल श्रीकृष्णके मुखकी ओर देखते रहे ॥"},
53 {"role": "assistant", "content": "ततः केचिन्महीपाला नानुवंस्तत्र किंचन अतीतवाक्पथे काले प्रेक्षमाणा जनार्दनम् ॥"},
54 {"role": "user", "content": "फिर तो उसने एक दूसरे भयंकर शत्रुको वहाँ आया हुआ देखा, जो सरकण्डेके फूलके समान भूरे रंगका था वह धरतीमें विवर बनाकर उसके भीतर सोया करता था"},
55 {"role": "assistant", "content": "अपश्यदपरं घोरमात्मनः शत्रुमागतम् शरप्रसूनसङ्काशं महीविवरशायिनम्॥"},
56 {"role": "user", "content": "जो मनुष्य पाण्डुनन्दन अर्जुनके इस चरित्रको प्रतिदिन सुनता है, उसके मनमै पापपूर्ण विषयभोगोंकी इच्छा नहीं होती ॥"},
57 {"role": "assistant", "content": "इदं यः शृणुयाद् वृत्तं नित्यं पाण्डुसुतस्य थे न तस्य कामः कामेषु पापकेषु प्रवर्तते ॥"},
58 {"role": "user", "content": hi_text},
59 ]
60
61 inputs = tokenizer.apply_chat_template(
62 messages,
63 tokenize=True,
64 add_generation_prompt=True,
65 return_dict=True,
66 return_tensors="pt",
67 ).to(device)
68
69 with torch.inference_mode():
70 outputs = model.generate(
71 **inputs,
72 max_new_tokens=MAX_NEW_TOKENS,
73 do_sample=False,
74 pad_token_id=tokenizer.eos_token_id,
75 eos_token_id=tokenizer.eos_token_id,
76 )
77
78 generated_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
79 return tokenizer.decode(generated_tokens, skip_special_tokens=True).strip()
80
81
82text = "राक्षस कुल के आनंद, तुम्हारे कारण लंका की अवस्था और हम सब अब निराश्रित हो गए हैं। अपने कर्मों से तुमने अपने शरीर को गिद्धों द्वारा खाए जाने योग्य और अपनी आत्मा को नरक जाने योग्य बना लिया है।"
83out = generate(text)
84
85print(f"PROSE : {text}\nPOETRY: {out}")messages = [] block with1 messages = [
2 {"role": "system", "content": ANUSHTUP_INSTRUCTION},
3 {"role": "user", "content": hi_text},
4 ]1do_sample=True,
2temperature=0.6,
3top_p=0.9,
4top_k=50,1@misc{jagadeeshan2026chandomitrageneratingstructuredsanskrit,
2 title={Chandomitra: Towards Generating Structured Sanskrit Poetry from Natural Language Inputs},
3 author={Manoj Balaji Jagadeeshan and Samarth Bhatia and Pretam Ray and Harshul Raj Surana and Akhil Rajeev P and Priya Mishra and Annarao Kulkarni and Ganesh Ramakrishnan and Prathosh AP and Pawan Goyal},
4 year={2026},
5 eprint={2506.00815},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2506.00815},
9}