File size: 1,097 Bytes
61708a8
f113e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35

# Import necessary libraries
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Load the tokenizer and model
model_id = "huggingface-course/marian-finetuned-kde4-en-to-fr"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)

# Function to translate text from English to French
def translate(text):
    # Tokenize the text; encode the input text and add the EOS token
    input_tokens = tokenizer.encode(text, return_tensors="pt", add_special_tokens=True)

    # Generate the sequence of tokens corresponding to the translation
    translated_tokens = model.generate(input_tokens, max_length=512)

    # Decode the tokens to a string
    translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)

    return translation

# Example usage
input_text = "This is an example sentence that we want to translate into French."
translated_text = translate(input_text)
print("Translated Text:", translated_text)


import gradio as gr


demo = gr.Interface(fn=translate, inputs="text", outputs="text")
demo.launch()