File size: 915 Bytes
4c52a34
 
 
 
 
 
 
 
 
 
 
7cb7cf4
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
import gradio as gr

# Load mBART model and tokenizer for multilingual translation
model_name = 'facebook/mbart-large-50-many-to-one-mmt'  # mBART 50 for many-to-one translation (many languages to English)
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
model = MBartForConditionalGeneration.from_pretrained(model_name)

# Set the source language code (Tamil)
tokenizer.src_lang = "ta_IN"  # Tamil language code

# Function for translation
def translate(text):
    inputs = tokenizer(text, return_tensors="pt")
    generated_tokens = model.generate(**inputs)
    translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
    return translated_text

# Launch a Gradio interface
interface = gr.Interface(fn=translate, inputs="text", outputs="text", title="Tamil to English Translator")
interface.launch()