import gradio as gr from transformers import AutoTokenizer, AutoModelForSeq2SeqLM model_name = "my_awesome_opus_books_model" # Load the tokenizer and model tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSeq2SeqLM.from_pretrained(model_name) def translate(text): inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).input_ids outputs = model.generate(inputs, max_length=512, num_beams=5, early_stopping=True) translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) return translated_text # Create a Gradio interface interface = gr.Interface(fn=translate, inputs=gr.Textbox(lines=2, placeholder="Enter English text to translate..."), outputs=gr.Textbox(), title="English to French Translation", description="Translate English text to French using a fine-tuned model, start your sentence with translate English to French:") # Launch the interface interface.launch()