|
|
import torch |
|
|
import gradio as gr |
|
|
import json |
|
|
from transformers import pipeline |
|
|
import pyttsx3 |
|
|
import tempfile |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M") |
|
|
|
|
|
|
|
|
def load_lang_codes(file_path='lang_code.json'): |
|
|
try: |
|
|
with open(file_path, 'r', encoding='utf-8') as f: |
|
|
data = json.load(f) |
|
|
return {entry['Language']: entry['FLORES-200 code'] for entry in data} |
|
|
except Exception as e: |
|
|
print(f"Error loading language codes: {e}") |
|
|
return {} |
|
|
|
|
|
lang_dict = load_lang_codes() |
|
|
language_list = list(lang_dict.keys()) |
|
|
|
|
|
|
|
|
def text_to_speech(text): |
|
|
engine = pyttsx3.init() |
|
|
engine.setProperty('rate', 160) |
|
|
engine.setProperty('volume', 1.0) |
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp: |
|
|
filename = fp.name |
|
|
engine.save_to_file(text, filename) |
|
|
engine.runAndWait() |
|
|
return filename |
|
|
|
|
|
|
|
|
def translate_and_speak(text, src_lang, tgt_lang): |
|
|
src_code = lang_dict.get(src_lang) |
|
|
tgt_code = lang_dict.get(tgt_lang) |
|
|
|
|
|
if not src_code or not tgt_code: |
|
|
return "Invalid language selected.", None |
|
|
|
|
|
try: |
|
|
result = translator(text, src_lang=src_code, tgt_lang=tgt_code) |
|
|
translated_text = result[0]['translation_text'] |
|
|
audio_path = text_to_speech(translated_text) |
|
|
return translated_text, audio_path |
|
|
except Exception as e: |
|
|
return f"Translation error: {e}", None |
|
|
|
|
|
|
|
|
with gr.Blocks(css=""" |
|
|
.gradio-container { |
|
|
font-family: 'Segoe UI', sans-serif; |
|
|
background-color: #f4f4f4; |
|
|
padding: 40px; |
|
|
} |
|
|
h1 { |
|
|
color: black !important; |
|
|
font-size: 28px !important; |
|
|
margin-bottom: 30px !important; |
|
|
} |
|
|
.gr-button { |
|
|
font-size: 16px !important; |
|
|
padding: 10px 20px !important; |
|
|
} |
|
|
.gr-box { |
|
|
padding: 20px !important; |
|
|
} |
|
|
""") as demo: |
|
|
gr.Markdown("<h1 style='text-align: center;'>@GenAI MultiLanguage Translator Model</h1>") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
input_text = gr.Textbox( |
|
|
label="π Enter Text", |
|
|
placeholder="Type something to translate...", |
|
|
lines=4 |
|
|
) |
|
|
gr.Markdown("<br>") |
|
|
src_dropdown = gr.Dropdown(choices=language_list, label="π€ Source Language", value="English") |
|
|
tgt_dropdown = gr.Dropdown(choices=language_list, label="π₯ Target Language", value="French") |
|
|
gr.Markdown("<br>") |
|
|
translate_button = gr.Button("π Translate", variant="primary") |
|
|
with gr.Column(scale=1): |
|
|
output_text = gr.Textbox(label="β
Translated Text", lines=6, interactive=False) |
|
|
audio_output = gr.Audio(label="π Speak Translation", type="filepath", autoplay=True) |
|
|
|
|
|
translate_button.click(fn=translate_and_speak, |
|
|
inputs=[input_text, src_dropdown, tgt_dropdown], |
|
|
outputs=[output_text, audio_output]) |
|
|
|
|
|
|
|
|
demo.launch() |
|
|
|