insider12 commited on
Commit
68f0220
Β·
verified Β·
1 Parent(s): cf2029f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import json
4
+ from transformers import pipeline
5
+ import pyttsx3
6
+ import tempfile
7
+ import os
8
+
9
+ # Load the NLLB model
10
+ # model_path = "../text_translator/model/models--facebook--nllb-200-distilled-600M/snapshots/f8d333a098d19b4fd9a8b18f94170487ad3f821d"
11
+ # translator = pipeline("translation", model=model_path)
12
+
13
+ translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
14
+ # Load language codes
15
+ # def load_lang_codes(file_path='../text_translator/lang file/lang_code.json'):
16
+ def load_lang_codes(file_path='lang_code.json'):
17
+ try:
18
+ with open(file_path, 'r', encoding='utf-8') as f:
19
+ data = json.load(f)
20
+ return {entry['Language']: entry['FLORES-200 code'] for entry in data}
21
+ except Exception as e:
22
+ print(f"Error loading language codes: {e}")
23
+ return {}
24
+
25
+ lang_dict = load_lang_codes()
26
+ language_list = list(lang_dict.keys())
27
+
28
+ # Text-to-Speech function using pyttsx3
29
+ def text_to_speech(text):
30
+ engine = pyttsx3.init()
31
+ engine.setProperty('rate', 160)
32
+ engine.setProperty('volume', 1.0)
33
+
34
+ # Save speech to a temporary file
35
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
36
+ filename = fp.name
37
+ engine.save_to_file(text, filename)
38
+ engine.runAndWait()
39
+ return filename
40
+
41
+ # Translation + TTS function
42
+ def translate_and_speak(text, src_lang, tgt_lang):
43
+ src_code = lang_dict.get(src_lang)
44
+ tgt_code = lang_dict.get(tgt_lang)
45
+
46
+ if not src_code or not tgt_code:
47
+ return "Invalid language selected.", None
48
+
49
+ try:
50
+ result = translator(text, src_lang=src_code, tgt_lang=tgt_code)
51
+ translated_text = result[0]['translation_text']
52
+ audio_path = text_to_speech(translated_text)
53
+ return translated_text, audio_path
54
+ except Exception as e:
55
+ return f"Translation error: {e}", None
56
+
57
+ # Gradio UI
58
+ with gr.Blocks(css="""
59
+ .gradio-container {
60
+ font-family: 'Segoe UI', sans-serif;
61
+ background-color: #f4f4f4;
62
+ padding: 40px;
63
+ }
64
+ h1 {
65
+ color: black !important;
66
+ font-size: 28px !important;
67
+ margin-bottom: 30px !important;
68
+ }
69
+ .gr-button {
70
+ font-size: 16px !important;
71
+ padding: 10px 20px !important;
72
+ }
73
+ .gr-box {
74
+ padding: 20px !important;
75
+ }
76
+ """) as demo:
77
+ gr.Markdown("<h1 style='text-align: center;'>@GenAI MultiLanguage Translator Model</h1>")
78
+
79
+ with gr.Row():
80
+ with gr.Column(scale=1):
81
+ input_text = gr.Textbox(
82
+ label="🌐 Enter Text",
83
+ placeholder="Type something to translate...",
84
+ lines=4
85
+ )
86
+ gr.Markdown("<br>")
87
+ src_dropdown = gr.Dropdown(choices=language_list, label="πŸ“€ Source Language", value="English")
88
+ tgt_dropdown = gr.Dropdown(choices=language_list, label="πŸ“₯ Target Language", value="French")
89
+ gr.Markdown("<br>")
90
+ translate_button = gr.Button("πŸ”„ Translate", variant="primary")
91
+ with gr.Column(scale=1):
92
+ output_text = gr.Textbox(label="βœ… Translated Text", lines=6, interactive=False)
93
+ audio_output = gr.Audio(label="πŸ”Š Speak Translation", type="filepath", autoplay=True)
94
+
95
+ translate_button.click(fn=translate_and_speak,
96
+ inputs=[input_text, src_dropdown, tgt_dropdown],
97
+ outputs=[output_text, audio_output])
98
+
99
+ # Launch
100
+ demo.launch()