Downloads · 30 days
7
4% of all-time downloads
snskrt/Sanskrit-Qwen2.5-7B-chat
Sanskrit-Qwen2.5-7B-chat is a text generation model from snskrt. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as apache-2.0.
Note: This project represents a series of experimental attempts to create specialized Sanskrit language models. While significant effort was invested, the results did not meet the quality standards required for practi…
Downloads · 30 days
7
4% of all-time downloads
All-time downloads
172
Public
Parameters
7.6B
15.2 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors15.2 GB · 100%
From the Hugging Face model README
Note: This project represents a series of experimental attempts to create specialized Sanskrit language models. While significant effort was invested, the results did not meet the quality standards required for practical use. This repository is maintained for research documentation and learning purposes.
This model is a fine-tuned version of snskrt/Sanskrit-Qwen2.5-7B-base specialized for Sanskrit language tasks including bidirectional translation (Sanskrit ↔ English) and transliteration (Devanagari to IAST). The model has been trained in two phases: initial Sanskrit language modeling followed by chat template enhancement for translation and transliteration capabilities.
This is a specialized version of Qwen2.5-7B that has been enhanced for Sanskrit language processing through a two-phase training approach:
Phase 1: Sanskrit Language Model Training
Phase 2: Chat Template Enhancement
Key Features:
Capabilities:
Phase 1 - Sanskrit Language Model:
Phase 2 - Chat Enhancement:
The model was evaluated on a comprehensive test set of 500 samples from Test Dataset across three task types:
| Task Type | Samples | Exact Match | Jaccard Similarity | BLEU Score | Character Accuracy |
|---|---|---|---|---|---|
| Sanskrit → English | 114 | 0.0% | 30.9% | 10.9% | - |
| English → Sanskrit | 101 | 0.0% | 10.3% | 4.1% | - |
| Transliteration | 285 | 29.1% | - | - | 96.7% |
| Overall | 500 | - | - | - | - |
Key Insights:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load model and tokenizer
model_name = "snskrt/Sanskrit-Qwen2.5-7B-chat"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
)
# Sanskrit to English Translation
def translate_sanskrit_to_english(sanskrit_text):
messages = [
{"role": "system", "content": "You are a Sanskrit to English translation expert. Translate the given Sanskrit text accurately while preserving the meaning and context."},
{"role": "user", "content": f"Translate this Sanskrit text to English: {sanskrit_text}"}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
return response.strip()
# English to Sanskrit Translation
def translate_english_to_sanskrit(english_text):
messages = [
{"role": "system", "content": "You are an English to Sanskrit translation expert. Translate the given English text accurately into Sanskrit while preserving the meaning and context."},
{"role": "user", "content": f"Translate this English text to Sanskrit: {english_text}"}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
return response.strip()
# Transliteration (Devanagari to IAST)
def transliterate_to_iast(devanagari_text):
messages = [
{"role": "system", "content": "You are a Sanskrit transliteration expert. Convert the given Sanskrit text from Devanagari script to IAST (International Alphabet of Sanskrit Transliteration) format."},
{"role": "user", "content": f"Transliterate this Sanskrit text to IAST: {devanagari_text}"}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
return response.strip()
# Example usage
sanskrit_text = "नमस्ते संस्कृत भाषा ।"
english_translation = translate_sanskrit_to_english(sanskrit_text)
print(f"Sanskrit: {sanskrit_text}")
print(f"English: {english_translation}")
iast_transliteration = transliterate_to_iast(sanskrit_text)
print(f"IAST: {iast_transliteration}")
def chat_with_sanskrit_model(user_input, task_type="translation"):
if task_type == "translation":
system_prompt = "You are a Sanskrit to English translation expert. Translate the given Sanskrit text accurately while preserving the meaning and context."
elif task_type == "transliteration":
system_prompt = "You are a Sanskrit transliteration expert. Convert the given Sanskrit text from Devanagari script to IAST format."
else:
system_prompt = "You are a Sanskrit language expert. Help with Sanskrit text processing tasks."
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_input}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
return response.strip()
Hyperparameters:
Hyperparameters:
Phase 1 Results:
Phase 2 Results:
This model was trained using: <br> <img src="https://raw.githubusercontent.com/axolotl-ai-cloud/axolotl/main/image/axolotl-badge-web.png" alt="Built with Axolotl" width="200" height="32"/>
<details><summary>See Phase 1 axolotl config</summary>axolotl version: 0.12.2
# Base model configuration
base_model: Qwen/Qwen2.5-7B
trust_remote_code: true
# Custom Sanskrit tokenizer configuration
tokenizer_config: sanskrit_tokenizer
tokenizer_type: AutoTokenizer
tokenizer_use_fast: true
tokenizer_legacy: false
tokenizer_save_jinja_files: true
# Resize embeddings for new tokens if needed
resize_token_embeddings_to_32x: true
mean_resizing_embeddings: false
shrink_embeddings: false
# Dataset configuration for text completion
datasets:
- path: sanskrit_clean_dataset.jsonl
type: completion
field: text
# Dataset preparation
dataset_prepared_path:
val_set_size: 0.1
output_dir: ./outputs/sanskrt-qwen-lora
# Sequence configuration
sequence_len: 1024
sample_packing: true
eval_sample_packing: true
# LoRA configuration
adapter: lora
lora_r: 32
lora_alpha: 64
lora_dropout: 0.05
lora_target_linear: true
lora_target_modules: ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
gradient_accumulation_steps: 2
micro_batch_size: 2
num_epochs: 3
optimizer: adamw_bnb_8bit
lr_scheduler: cosine
learning_rate: 0.0003
# Precision configuration
bf16: auto
tf32: true
# Memory optimization
gradient_checkpointing: true
gradient_checkpointing_kwargs:
use_reentrant: false
flash_attention: true
# Training schedule
warmup_ratio: 0.1
evals_per_epoch: 4
saves_per_epoch: 1
weight_decay: 0.0
hub_model_id: snskrt/sanskrit-qwen2.5-7b-base-lora
</details>
<details><summary>See Phase 2 axolotl config</summary>
axolotl version: 0.12.2
# Post-Training Configuration for Sanskrit Translation & Transliteration Enhancement
base_model: snskrt/Sanskrit-Qwen2.5-7B-base
chat_template: qwen_25
datasets:
- path: sanskrit_train_set.jsonl
type: chat_template
field_messages: messages
message_property_mappings:
role: role
content: content
roles:
system:
- system
user:
- user
assistant:
- assistant
val_set_size: 0.01
output_dir: ./outputs/sanskrit-chat
adapter: lora
sequence_len: 1024
pad_to_sequence_len: false
lora_r: 32
lora_alpha: 16
lora_dropout: 0.05
lora_target_modules: ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
gradient_accumulation_steps: 4
micro_batch_size: 2
num_epochs: 3
optimizer: adamw_bnb_8bit
lr_scheduler: cosine
learning_rate: 0.0002
bf16: true
tf32: true
gradient_checkpointing: true
logging_steps: 1
flash_attention: true
warmup_ratio: 0.1
evals_per_epoch: 1
saves_per_epoch: 1
weight_decay: 0.0
hub_model_id: snskrt/Sanskrit-Qwen2.5-7B-chat-lora
</details><br>
If you use this model in your research, please cite:
@misc{sanskrit-qwen2.5-7b-chat,
title={Sanskrit Qwen2.5-7B Chat Model: A Specialized Language Model for Sanskrit Translation and Transliteration},
author={[Your Name]},
year={2024},
url={https://huggingface.co/snskrt/Sanskrit-Qwen2.5-7B-chat}
}
This model is released under the Apache 2.0 License. See the LICENSE file for more details.