from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline

model_name = "ai4bharat/indictrans2-en-indic-1B"

# Load model with trust_remote_code=True
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, trust_remote_code=True)

# Proper pipeline setup with src and tgt language tags
translator = pipeline(
    "translation",
    model=model,
    tokenizer=tokenizer,
    src_lang="eng_Latn",      # Source: English
    tgt_lang="hin_Deva",      # Target: Hindi
    device=0                  # use CUDA:0, or use -1 for CPU
)

# Translate a sample English sentence
english_text = "Lord Shiva is known as the destroyer in Hindu mythology."
result = translator(english_text, max_length=512)

translated_text = result[0]['translation_text']
print("✅ Hindi Translation:\n", translated_text)
