File size: 2,102 Bytes
68b259c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b73554a
b34dfee
68b259c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gradio as gr
from transformers import pipeline

# Load the translation pipeline
pipe = pipeline("translation", model="facebook/nllb-200-distilled-600M")

# Define the translation function
def translate_text(text, source_language, target_language, max_length=200):
    # Define source and target languages (e.g., 'eng_Latn' -> 'fra_Latn')
    language_pair = f"{source_language}-{target_language}"
    response = pipe(
        text,
        src_lang=source_language,
        tgt_lang=target_language,
        max_length=max_length
    )
    return response[0]['translation_text']

# Create the Gradio app interface
with gr.Blocks() as app:
    gr.Markdown("## Translation App")
    gr.Markdown(
        "Enter text in the source language, and the model will translate it to the target language. "
        "This app uses the `facebook/nllb-200-distilled-600M` model."
    )

    gr.Markdown("### To choose the languages: [https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200)")
    
    with gr.Row():
        input_text = gr.Textbox(
            label="Input Text",
            placeholder="Type the text to translate...",
            lines=4
        )
        output_translation = gr.Textbox(label="Translated Text", lines=4)

    with gr.Row():
        source_language = gr.Textbox(
            label="Source Language Code",
            placeholder="e.g., eng_Latn (for English)"
        )
        target_language = gr.Textbox(
            label="Target Language Code",
            placeholder="e.g., fra_Latn (for French)"
        )

    max_length = gr.Slider(
        label="Max Length",
        minimum=50,
        maximum=500,
        step=50,
        value=200
    )

    submit_button = gr.Button("Translate")
    
    submit_button.click(
        fn=translate_text,
        inputs=[input_text, source_language, target_language, max_length],
        outputs=output_translation
    )

# Launch the app
if __name__ == "__main__":
    app.launch()