Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Chargement des pipelines | |
| fr_en_nllb_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="fra_Latn", tgt_lang="eng_Latn") | |
| en_fr_nllb_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="eng_Latn", tgt_lang="fra_Latn") | |
| en_fr_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr") | |
| fr_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en") | |
| # Fonction de traduction | |
| def translate_text(text, model_choice): | |
| if model_choice == "NLLB-200-FR-EN (facebook)": | |
| result = fr_en_nllb_translator(text.strip())[0]["translation_text"] | |
| elif model_choice == "NLLB-200-EN-FR (facebook)": | |
| result = en_fr_nllb_translator(text.strip())[0]["translation_text"] | |
| elif model_choice == "Opus-MT-EN-FR (Helsinki-NLP)": | |
| result = en_fr_translator(text.strip())[0]["translation_text"] | |
| else: | |
| result = fr_en_translator(text.strip())[0]["translation_text"] | |
| return result | |
| # Interface Gradio | |
| demo = gr.Interface( | |
| fn=translate_text, | |
| inputs=[ | |
| gr.Textbox(label="Texte à traduire", placeholder="Entrez du texte en français ici..."), | |
| gr.Radio(["NLLB-200-FR-EN (facebook)", "NLLB-200-EN-FR (facebook)", | |
| "Opus-MT-FR-EN (Helsinki-NLP)","Opus-MT-EN-FR (Helsinki-NLP)"], label="Choisissez le modèle") | |
| ], | |
| outputs=gr.Textbox(label="Texte traduit"), | |
| title="Traduction FR-EN & EN-FR" | |
| ) | |
| demo.launch() | |