File size: 1,481 Bytes
25a7c2f
d3087fa
25a7c2f
 
 
 
 
0674d75
 
25a7c2f
 
 
 
 
0674d75
 
 
25a7c2f
 
 
 
 
 
 
0674d75
 
 
 
 
 
 
 
25a7c2f
0674d75
 
 
 
 
 
 
 
 
 
 
9af06f8
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
import torch
import gradio as gr
import json

# Use a pipeline as a high-level helper
from transformers import pipeline

# Initialize the translation pipeline
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)

# Load the JSON data from the file
with open('language.json', 'r') as file:
    language_data = json.load(file)

# Extract language names from the JSON data
language_names = [entry['Language'] for entry in language_data]

def get_FLORES_code_from_language(language):
    for entry in language_data:
        if entry['Language'].lower() == language.lower():
            return entry['FLORES-200 code']
    return None

def translate_text(text, destination_language):
    dest_code = get_FLORES_code_from_language(destination_language)
    if dest_code:
        translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
        return translation[0]["translation_text"]
    else:
        return "Destination language code not found."

# Create and launch the Gradio interface
gr.close_all()
demo = gr.Interface(
    fn=translate_text,
    inputs=[
        gr.Textbox(label="Input text to translate", lines=6),
        gr.Dropdown(language_names, label="Select Destination Language")
    ],
    outputs=[gr.Textbox(label="Translated text", lines=4)],
    title="Multi-language Translator",
    description="This application translates any English text to multiple languages."
)

demo.launch()