Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
from deep_translator import GoogleTranslator
|
4 |
+
from gtts import gTTS
|
5 |
+
import os
|
6 |
+
from pydub import AudioSegment
|
7 |
+
|
8 |
+
# Initialize the recognizer
|
9 |
+
recognizer = sr.Recognizer()
|
10 |
+
|
11 |
+
# Function to perform translation
|
12 |
+
def translate_text(text, src_lang, target_lang):
|
13 |
+
try:
|
14 |
+
translation = GoogleTranslator(source=src_lang, target=target_lang).translate(text)
|
15 |
+
return translation
|
16 |
+
except Exception as e:
|
17 |
+
return str(e)
|
18 |
+
|
19 |
+
# Function to provide text-to-speech for the translation
|
20 |
+
def text_to_speech(translation, lang):
|
21 |
+
try:
|
22 |
+
tts = gTTS(translation, lang=lang)
|
23 |
+
audio_path = "output.mp3"
|
24 |
+
tts.save(audio_path)
|
25 |
+
return audio_path if os.path.exists(audio_path) else "Audio generation failed"
|
26 |
+
except Exception as e:
|
27 |
+
return str(e)
|
28 |
+
|
29 |
+
# Function to count words and characters in the source text
|
30 |
+
def count_words_and_chars(text):
|
31 |
+
word_count = len(text.split())
|
32 |
+
char_count = len(text)
|
33 |
+
return f"Words: {word_count}, Characters: {char_count}"
|
34 |
+
|
35 |
+
# Function to clear text fields
|
36 |
+
def clear_text_fields():
|
37 |
+
return "", "", "", ""
|
38 |
+
|
39 |
+
# Function to save translation to a text file
|
40 |
+
def save_translation(text, translation):
|
41 |
+
with open("saved_translations.txt", "a") as f:
|
42 |
+
f.write(f"Original: {text}\nTranslated: {translation}\n\n")
|
43 |
+
return "Translation saved!"
|
44 |
+
|
45 |
+
# Function for speech recognition (live voice input to text)
|
46 |
+
def recognize_speech(audio):
|
47 |
+
try:
|
48 |
+
audio_segment = AudioSegment.from_file(audio)
|
49 |
+
audio_segment.export("temp.wav", format="wav")
|
50 |
+
|
51 |
+
with sr.AudioFile("temp.wav") as source:
|
52 |
+
audio_data = recognizer.record(source) # read the entire audio file
|
53 |
+
text = recognizer.recognize_google(audio_data)
|
54 |
+
return text
|
55 |
+
except sr.UnknownValueError:
|
56 |
+
return "Could not understand audio."
|
57 |
+
except sr.RequestError as e:
|
58 |
+
return f"Could not request results from Google Speech Recognition service; {e}"
|
59 |
+
|
60 |
+
# List of languages
|
61 |
+
languages = [
|
62 |
+
('English', 'en'),
|
63 |
+
('Spanish', 'es'),
|
64 |
+
('French', 'fr'),
|
65 |
+
('German', 'de'),
|
66 |
+
('Italian', 'it'),
|
67 |
+
('Portuguese', 'pt'),
|
68 |
+
('Russian', 'ru'),
|
69 |
+
('Chinese (Simplified)', 'zh-CN'),
|
70 |
+
('Chinese (Traditional)', 'zh-TW'),
|
71 |
+
('Japanese', 'ja'),
|
72 |
+
('Korean', 'ko'),
|
73 |
+
('Hindi', 'hi'),
|
74 |
+
('Telugu', 'te'),
|
75 |
+
('Tamil', 'ta'),
|
76 |
+
('Arabic', 'ar')
|
77 |
+
]
|
78 |
+
|
79 |
+
# Define Gradio interface with merged components and custom title style
|
80 |
+
css = """
|
81 |
+
body {
|
82 |
+
background-color: white; /* Set background color to white */
|
83 |
+
color: black; /* Set text color to black */
|
84 |
+
margin: 0;
|
85 |
+
padding: 0;
|
86 |
+
height: 100vh;
|
87 |
+
display: flex;
|
88 |
+
justify-content: center;
|
89 |
+
align-items: center;
|
90 |
+
}
|
91 |
+
.gradio-container {
|
92 |
+
background-color: white; /* Container background color */
|
93 |
+
padding: 20px;
|
94 |
+
display: flex;
|
95 |
+
flex-direction: column;
|
96 |
+
align-items: center;
|
97 |
+
gap: 10px;
|
98 |
+
overflow-y: auto; /* Enable vertical scrolling */
|
99 |
+
max-height: 100vh;
|
100 |
+
width: 100%;
|
101 |
+
box-sizing: border-box;
|
102 |
+
}
|
103 |
+
.textbox-group, .button-row {
|
104 |
+
display: flex;
|
105 |
+
flex-direction: column;
|
106 |
+
align-items: center;
|
107 |
+
gap: 10px;
|
108 |
+
width: 100%;
|
109 |
+
}
|
110 |
+
#translate-btn {
|
111 |
+
background-color: #4CAF50; /* Green */
|
112 |
+
color: white; /* White text */
|
113 |
+
}
|
114 |
+
#tts-btn {
|
115 |
+
background-color: #2196F3; /* Blue */
|
116 |
+
color: white; /* White text */
|
117 |
+
}
|
118 |
+
#clear-btn {
|
119 |
+
background-color: #f44336; /* Red */
|
120 |
+
color: white; /* White text */
|
121 |
+
}
|
122 |
+
#save-btn {
|
123 |
+
background-color: #FF9800; /* Orange */
|
124 |
+
color: white; /* White text */
|
125 |
+
}
|
126 |
+
#upload-audio-btn {
|
127 |
+
background-color: #9C27B0; /* Purple */
|
128 |
+
color: white; /* White text */
|
129 |
+
}
|
130 |
+
#translate-btn, #tts-btn, #clear-btn, #save-btn, #upload-audio-btn {
|
131 |
+
width: 100%; /* Stretch buttons to full width */
|
132 |
+
margin: 5px 0;
|
133 |
+
border: none; /* Remove border */
|
134 |
+
padding: 10px; /* Add padding */
|
135 |
+
border-radius: 5px; /* Rounded corners */
|
136 |
+
cursor: pointer; /* Pointer cursor */
|
137 |
+
font-size: 16px; /* Font size */
|
138 |
+
}
|
139 |
+
h1 {
|
140 |
+
text-align: center; /* Center the title */
|
141 |
+
font-family: 'Algerian', sans-serif; /* Change the font style */
|
142 |
+
font-weight: bold; /* Make the font bold */
|
143 |
+
font-size: 32px; /* Increase font size */
|
144 |
+
color: black; /* Set title color to dark purple */
|
145 |
+
}
|
146 |
+
#label {
|
147 |
+
color: black; /* Set label text color to black */
|
148 |
+
font-size: 16px; /* Set label font size */
|
149 |
+
font-weight: bold;
|
150 |
+
}
|
151 |
+
"""
|
152 |
+
|
153 |
+
with gr.Blocks(css=css) as demo:
|
154 |
+
gr.Markdown("<h1>Word Bridge</h1>")
|
155 |
+
|
156 |
+
# Grouping all components in a single column for unified layout
|
157 |
+
with gr.Column(elem_classes="textbox-group"):
|
158 |
+
# Input fields
|
159 |
+
src_text = gr.Textbox(label="Text / Upload Audio",
|
160 |
+
placeholder="Type text or upload audio below...",
|
161 |
+
lines=4,
|
162 |
+
interactive=True,
|
163 |
+
elem_id="src-text-area")
|
164 |
+
|
165 |
+
src_lang = gr.Dropdown(choices=[(name, code) for name, code in languages], value='en', label="Source Language")
|
166 |
+
target_lang = gr.Dropdown(choices=[(name, code) for name, code in languages], value='fr', label="Target Language")
|
167 |
+
|
168 |
+
# Outputs
|
169 |
+
translation_output = gr.Textbox(label="Translated Text", interactive=False, lines=4, elem_id="translation-output")
|
170 |
+
audio_output = gr.Audio(label="Listen to Translation")
|
171 |
+
word_char_count = gr.Textbox(label="Word and Character Count", interactive=False, elem_id="word-char-count")
|
172 |
+
status_message = gr.Textbox(label="Status", interactive=False, elem_id="status-message")
|
173 |
+
|
174 |
+
# Buttons all merged into a single column
|
175 |
+
with gr.Column(elem_classes="button-row"):
|
176 |
+
translate_btn = gr.Button("Translate", elem_id="translate-btn", variant="primary")
|
177 |
+
tts_btn = gr.Button("Listen to Translation", elem_id="tts-btn", variant="secondary")
|
178 |
+
clear_btn = gr.Button("Clear", elem_id="clear-btn", variant="secondary")
|
179 |
+
save_btn = gr.Button("Save Translation", elem_id="save-btn", variant="secondary")
|
180 |
+
upload_audio_btn = gr.Button("Upload Audio", elem_id="upload-audio-btn", variant="secondary")
|
181 |
+
|
182 |
+
# When the translate button is clicked
|
183 |
+
translate_btn.click(translate_text, [src_text, src_lang, target_lang], translation_output)
|
184 |
+
|
185 |
+
# Count words and characters after translation
|
186 |
+
src_text.change(count_words_and_chars, src_text, word_char_count)
|
187 |
+
|
188 |
+
# When the text-to-speech button is clicked
|
189 |
+
tts_btn.click(text_to_speech, [translation_output, target_lang], audio_output)
|
190 |
+
|
191 |
+
# When the clear button is clicked
|
192 |
+
clear_btn.click(clear_text_fields, [], [src_text, translation_output, word_char_count, status_message])
|
193 |
+
|
194 |
+
# When the save button is clicked
|
195 |
+
save_btn.click(save_translation, [src_text, translation_output], status_message)
|
196 |
+
|
197 |
+
# Upload audio button to start voice recognition
|
198 |
+
upload_audio_btn.click(recognize_speech, inputs=gr.Audio(type='filepath'), outputs=src_text)
|
199 |
+
|
200 |
+
# Launch the app
|
201 |
+
demo.launch()
|