File size: 1,032 Bytes
49c09fe
 
eb0342c
49c09fe
 
eb0342c
49c09fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import subprocess
subprocess.check_call(["pip", "install", "--upgrade", "huggingface-hub"])
subprocess.check_call(["pip", "install", "transformers"])
subprocess.check_call(["pip", "install", "torch"])
  

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("balaramas/mbart-sahitrans_new_data")
model = AutoModelForSeq2SeqLM.from_pretrained("balaramas/mbart-sahitrans_new_data")


def sanmt(txt):
    
    tokenizer.src_lang = "hi_IN"
    encoded_ar = tokenizer(txt, return_tensors="pt")   
    generated_tokens = model.generate(
        **encoded_ar,
        forced_bos_token_id=tokenizer.lang_code_to_id["hi_IN"]
    )
    output = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]   
    return output

iface = gr.Interface(
    fn=sanmt, 
    inputs=gr.Textbox(label="Enter text in Sanskrit", placeholder="Type here..."), 
    outputs=gr.Textbox(label="Translated Hindi Text"),
    title="Sanskrit to Hindi Translator"
)
iface.launch()