|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
translator_fr_to_en = pipeline('translation_fr_to_en', model='Helsinki-NLP/opus-mt-fr-en') |
|
translator_en_to_fr = pipeline('translation_en_to_fr', model='Helsinki-NLP/opus-mt-en-fr') |
|
|
|
def translate(text, direction): |
|
|
|
if direction == "Français à Anglais": |
|
return translator_fr_to_en(text, max_length=512)[0]['translation_text'] |
|
else: |
|
return translator_en_to_fr(text, max_length=512)[0]['translation_text'] |
|
|
|
|
|
custom_css = """ |
|
body { font-family: Arial, sans-serif; } |
|
h1 { color: #333366; } |
|
label { font-weight: bold; } |
|
input[type="text"], textarea { border-radius: 20px; border: 1px solid #ccc; padding: 10px; width: 100%; box-sizing: border-box; } |
|
.button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 12px; } |
|
footer{display:none !important} |
|
""" |
|
|
|
examples = [ |
|
["Elle est intéressée par la haute couture.", "Français à Anglais"], |
|
["I’m looking forward to seeing you.", "Anglais à Français"], |
|
["Appelons-le un jour.", "Français à Anglais"], |
|
["It’s the life.", "Anglais à Français"], |
|
["C’est la vie.", "Français à Anglais"], |
|
["Let's call him one day.", "Anglais à Français"] |
|
] |
|
|
|
|
|
interface = gr.Interface(fn=translate, |
|
inputs=[gr.Textbox(label="Texte à traduire"), |
|
gr.Radio(["Français à Anglais", "Anglais à Français"], |
|
label="Direction de traduction", |
|
value="Français à Anglais")], |
|
outputs=gr.Textbox(label="Texte traduit"), |
|
examples=examples, |
|
title="Traducteur Français-Anglais / English-French Translator", |
|
description="""<p>Une application simple pour traduire du texte du français vers l'anglais et vice versa. Entrez votre texte et sélectionnez la direction de la traduction.</p>""", |
|
css=custom_css, |
|
allow_flagging="never") |
|
|
|
|
|
interface.launch() |
|
|