File size: 1,027 Bytes
f64ea65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# KINYARWANDA TO ENGLISH
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer

# Suppose you found a model 'Helsinki-NLP/opus-mt-rw-en' that translates from Kinyarwanda to English
model_name = 'Helsinki-NLP/opus-mt-rw-en' 
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

# Function to translate text from Kinyarwanda to English
def translate(text):
    # Tokenize the text using the __call__ method
    model_inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    
    # Perform the translation
    gen = model.generate(**model_inputs)
    
    # Decode the generated tokens to string
    translation = tokenizer.batch_decode(gen, skip_special_tokens=True)
    return translation[0]

# Create a Gradio interface
iface = gr.Interface(
    fn=translate, 
    inputs=gr.Textbox(lines=2, placeholder="Enter Text in Kinyarwanda..."), 
    outputs=gr.Textbox()
)

# Launch the interface
iface.launch(debug=True,inline=False)