Instructions to use BenguerineMohammed/nmt-seq2seq-translator with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use BenguerineMohammed/nmt-seq2seq-translator with Transformers:
# Load model directly from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("BenguerineMohammed/nmt-seq2seq-translator") model = AutoModelForSeq2SeqLM.from_pretrained("BenguerineMohammed/nmt-seq2seq-translator") - Notebooks
- Google Colab
- Kaggle
| """ | |
| """ | |
| import warnings | |
| warnings.filterwarnings("ignore") | |
| import gradio as gr | |
| from src.ai_translator.languages import SUPPORTED_LANGUAGES | |
| from src.ai_translator.translate import translate_text, batch_translate | |
| from src.ai_translator.speech import speech_to_text | |
| from src.ai_translator.evaluate import calculate_bleu | |
| # Gradio wrapper functions | |
| def gradio_translate(text: str, src_lang: str, tgt_lang: str) -> str: | |
| """Wrapper for single translation.""" | |
| return translate_text(text, src_lang, tgt_lang) | |
| def gradio_speech_translate(audio, src_lang: str, tgt_lang: str): | |
| """Wrapper for speech-to-text + translation.""" | |
| if audio is None: | |
| return "β οΈ No audio provided", "" | |
| transcribed = speech_to_text(audio, src_lang) | |
| if transcribed.startswith(("β", "β οΈ")): | |
| return transcribed, "" | |
| return transcribed, translate_text(transcribed, src_lang, tgt_lang) | |
| def gradio_batch_translate(texts: str, src_lang: str, tgt_lang: str) -> str: | |
| """Wrapper for batch translation.""" | |
| return batch_translate(texts, src_lang, tgt_lang) | |
| def gradio_bleu(reference: str, hypothesis: str) -> str: | |
| """Wrapper for BLEU evaluation.""" | |
| if not reference or not hypothesis: | |
| return "Please provide both reference and hypothesis translations." | |
| _, report = calculate_bleu(reference, hypothesis) | |
| return report | |
| # Gradio UI β identical to original app.py | |
| with gr.Blocks( | |
| title="π Neural Machine Translation", | |
| theme=gr.themes.Soft(), | |
| css=""" | |
| .gradio-container { max-width: 1200px !important; } | |
| .tab-nav button { font-size: 16px !important; font-weight: 600 !important; } | |
| """, | |
| ) as demo: | |
| gr.Markdown( | |
| """ | |
| # π Neural Machine Translation System | |
| ### Powered by Facebook NLLB-200 | 200+ Languages | PyTorch 2.10 | |
| """ | |
| ) | |
| with gr.Tabs(): | |
| # Text Translation | |
| with gr.Tab("π¬ Text Translation"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| src_lang_text = gr.Dropdown( | |
| choices=SUPPORTED_LANGUAGES, value="English", | |
| label="π Source Language", interactive=True, | |
| ) | |
| input_text = gr.Textbox( | |
| lines=10, placeholder="Enter text to translate...", | |
| label="π Input Text", show_copy_button=True, | |
| ) | |
| with gr.Column(scale=1): | |
| tgt_lang_text = gr.Dropdown( | |
| choices=SUPPORTED_LANGUAGES, value="French", | |
| label="π Target Language", interactive=True, | |
| ) | |
| output_text = gr.Textbox( | |
| lines=10, label="β¨ Translation", show_copy_button=True, | |
| ) | |
| translate_btn = gr.Button("π Translate", variant="primary", size="lg") | |
| translate_btn.click( | |
| fn=gradio_translate, | |
| inputs=[input_text, src_lang_text, tgt_lang_text], | |
| outputs=output_text, | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["Hello, how are you today?", "English", "French"], | |
| ["Machine learning is fascinating.", "English", "Spanish"], | |
| ["I love traveling around the world.", "English", "Arabic"], | |
| ["The weather is beautiful.", "English", "German"], | |
| ], | |
| inputs=[input_text, src_lang_text, tgt_lang_text], | |
| ) | |
| # Speech Translation | |
| with gr.Tab("π€ Speech Translation"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| src_lang_speech = gr.Dropdown( | |
| choices=SUPPORTED_LANGUAGES, value="English", | |
| label="π Speech Language", | |
| ) | |
| tgt_lang_speech = gr.Dropdown( | |
| choices=SUPPORTED_LANGUAGES, value="French", | |
| label="π Target Language", | |
| ) | |
| audio_input = gr.Audio( | |
| sources=["microphone", "upload"], | |
| type="filepath", | |
| label="ποΈ Record or Upload Audio", | |
| ) | |
| transcribed_output = gr.Textbox(label="π Transcribed Text", show_copy_button=True) | |
| speech_translation_output = gr.Textbox(label="β¨ Translation", show_copy_button=True) | |
| speech_translate_btn = gr.Button("π Transcribe & Translate", variant="primary", size="lg") | |
| speech_translate_btn.click( | |
| fn=gradio_speech_translate, | |
| inputs=[audio_input, src_lang_speech, tgt_lang_speech], | |
| outputs=[transcribed_output, speech_translation_output], | |
| ) | |
| # Batch Translation | |
| with gr.Tab("π¦ Batch Translation"): | |
| gr.Markdown( | |
| """ | |
| ### Translate multiple sentences at once | |
| Enter one sentence per line for faster processing. | |
| """ | |
| ) | |
| with gr.Row(): | |
| src_lang_batch = gr.Dropdown( | |
| choices=SUPPORTED_LANGUAGES, value="English", label="π Source Language", | |
| ) | |
| tgt_lang_batch = gr.Dropdown( | |
| choices=SUPPORTED_LANGUAGES, value="Spanish", label="π Target Language", | |
| ) | |
| batch_input = gr.Textbox( | |
| lines=10, | |
| placeholder="Enter sentences (one per line):\n\nSentence 1\nSentence 2\nSentence 3", | |
| label="π Input Sentences", | |
| ) | |
| batch_output = gr.Textbox(lines=10, label="β¨ Batch Translations", show_copy_button=True) | |
| batch_btn = gr.Button("π Translate Batch", variant="primary", size="lg") | |
| batch_btn.click( | |
| fn=gradio_batch_translate, | |
| inputs=[batch_input, src_lang_batch, tgt_lang_batch], | |
| outputs=batch_output, | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["Hello, how are you?\nWhat is your name?\nI love coding.", "English", "French"], | |
| ], | |
| inputs=[batch_input, src_lang_batch, tgt_lang_batch], | |
| ) | |
| # BLEU Evaluation | |
| with gr.Tab("π BLEU Evaluation"): | |
| gr.Markdown( | |
| """ | |
| ### Evaluate Translation Quality | |
| Compare reference translation with model output using BLEU score. | |
| **BLEU Score Guide:** | |
| - 60-100: Excellent β | |
| - 40-60: Good π | |
| - 20-40: Fair β οΈ | |
| - 0-20: Poor β | |
| """ | |
| ) | |
| reference_text = gr.Textbox( | |
| lines=5, placeholder="Enter reference (ground truth) translation...", | |
| label="π Reference Translation", | |
| ) | |
| hypothesis_text = gr.Textbox( | |
| lines=5, placeholder="Enter model-generated translation...", | |
| label="π€ Model Translation", | |
| ) | |
| bleu_output = gr.Textbox(lines=15, label="π BLEU Score Report") | |
| bleu_btn = gr.Button("π Calculate BLEU", variant="primary", size="lg") | |
| bleu_btn.click( | |
| fn=gradio_bleu, | |
| inputs=[reference_text, hypothesis_text], | |
| outputs=bleu_output, | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["Le chat est sur le tapis", "Le chat est sur le tapis"], | |
| ["Bonjour, comment allez-vous?","Bonjour, comment vas-tu?"], | |
| ], | |
| inputs=[reference_text, hypothesis_text], | |
| ) | |
| gr.Markdown( | |
| """ | |
| --- | |
| **Model:** Facebook NLLB-200-distilled-600M | **Framework:** PyTorch 2.10 + Transformers | |
| Built with β€οΈ using Gradio | |
| """ | |
| ) | |
| # ββ Launch ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=False) | |