File size: 1,538 Bytes
0eac25b
e428704
0eac25b
e428704
0eac25b
e428704
4648011
0eac25b
109a6dd
0eac25b
 
e428704
 
4648011
0eac25b
 
 
6ea9d16
 
 
 
 
 
 
 
 
 
 
 
 
0eac25b
 
 
 
109a6dd
 
 
 
 
4648011
36c1a4b
109a6dd
0eac25b
 
4648011
0eac25b
 
6ea9d16
4648011
 
 
 
6ea9d16
4648011
 
 
 
 
6ea9d16
 
 
4648011
6ea9d16
0eac25b
 
6ea9d16
0eac25b
 
6ea9d16
 
 
 
 
0eac25b
 
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
67
68
69
70
71
72
73
74
75
from transformers import pipeline
import gradio as gr
import torch

device = 0 if torch.cuda.is_available() else -1

# Translator model
translator = pipeline(
    "translation",
    model="facebook/nllb-200-distilled-600M",
    device=device
)

# Languages
languages = {
    "English": "eng_Latn",
    "Urdu": "urd_Arab",
    "Hindi": "hin_Deva",
    "Chinese": "zho_Hans",
    "Japanese": "jpn_Jpan",
    "Bengali": "ben_Beng",
    "Arabic": "arb_Arab",
    "French": "fra_Latn",
    "German": "deu_Latn",
    "Spanish": "spa_Latn",
    "Russian": "rus_Cyrl",
    "Turkish": "tur_Latn",
    "Korean": "kor_Hang",
    "Italian": "ita_Latn",
    "Portuguese": "por_Latn"
}

def translate(text, src, tgt):
    try:
        result = translator(
            text,
            src_lang=languages[src],
            tgt_lang=languages[tgt]
        )
        return result[0]["translation_text"]
    except Exception as e:
        return str(e)

with gr.Blocks() as app:

    gr.Markdown("# 🌍 AI Translator")

    src = gr.Dropdown(
        choices=list(languages.keys()),
        value="English",
        label="Source Language"
    )

    tgt = gr.Dropdown(
        choices=list(languages.keys()),
        value="Urdu",
        label="Target Language"
    )

    text = gr.Textbox(
        label="Enter Text",
        placeholder="Type text here..."
    )

    btn = gr.Button("Translate")

    output = gr.Textbox(label="Output")

    btn.click(
        translate,
        inputs=[text, src, tgt],
        outputs=output
    )

app.launch()