File size: 1,527 Bytes
f3d941a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()