File size: 2,451 Bytes
c066aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
from transformers import pipeline

# Initialize translation pipelines
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 no direction is provided or "Français à Anglais" is selected
    if direction == "Français à Anglais":
        return translator_fr_to_en(text, max_length=512)[0]['translation_text']
    else:  # "Anglais à Français" is selected
        return translator_en_to_fr(text, max_length=512)[0]['translation_text']

# Custom CSS to style the Gradio interface
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"]
]

# Create the Gradio interface
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")

# Launch the app
interface.launch()