Spaces:
Runtime error
Runtime error
# 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() | |