Skip to content

Commit

Permalink
deep_translator with tkinter gui
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Paul authored and Samir Paul committed Sep 19, 2023
1 parent cab73e0 commit cb0117f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 143 deletions.
79 changes: 79 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import tkinter as tk
import threading
import speech_recognition as sr
from deep_translator import GoogleTranslator
from gtts import gTTS
from playsound import playsound
import os

keep_running = False

def update_translation():
global keep_running

if keep_running:
r = sr.Recognizer()
translator = GoogleTranslator()

with sr.Microphone() as source:
log_output.insert(tk.END, "Speak Now!\n")
audio = r.listen(source)

try:
speech_text = r.recognize_google(audio)
print(speech_text)
if speech_text.lower() in {'exit', 'stop'}:
keep_running = False
return

translated_text = GoogleTranslator(source='auto', target='hi').translate(text=speech_text)
print(translated_text)

voice = gTTS(translated_text, lang='hi')
voice.save('voice.mp3')
playsound('voice.mp3')
os.remove('voice.mp3')

log_output.insert(tk.END, translated_text + "\n")

except sr.UnknownValueError:
log_output.insert(tk.END, "Could not understand!\n")
except sr.RequestError:
log_output.insert(tk.END, "Could not request from Google!\n")

root.after(100, update_translation)

def run_translator():
global keep_running

if not keep_running:
keep_running = True
update_translation_thread = threading.Thread(target=update_translation)
update_translation_thread.start()

def kill_execution():
global keep_running
keep_running = False

# Create the main window
root = tk.Tk()
root.title("Voice Translator GUI")

# Create the frame for buttons
button_frame = tk.Frame(root)
button_frame.pack(pady=10)

# Create the "Run" button
run_button = tk.Button(button_frame, text="Start Translation", command=run_translator)
run_button.pack(side=tk.LEFT, padx=10)

# Create the "Kill" button
kill_button = tk.Button(button_frame, text="Kill Execution", command=kill_execution)
kill_button.pack(side=tk.LEFT, padx=10)

# Create the log output window
log_output = tk.Text(root, height=20, width=50)
log_output.pack(pady=10)

# Run the Tkinter event loop
root.mainloop()
137 changes: 0 additions & 137 deletions main.py

This file was deleted.

12 changes: 7 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
playsound
googletrans
SpeechRecognition
PyAudio
gTTs
gTTS-token
pyaudio
google-trans-new
gTTS
playsound
deep-translator
subprocess.run
pyautogui
1 change: 0 additions & 1 deletion tempCodeRunnerFile.py

This file was deleted.

32 changes: 32 additions & 0 deletions translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import speech_recognition as sr
# from google_trans_new import google_translator
from deep_translator import GoogleTranslator
from gtts import gTTS
from playsound import playsound
import os

r = sr.Recognizer()
translator = GoogleTranslator()

while True:
with sr.Microphone() as source:
print("Speak Now!")
audio = r.record(source, duration=5)
try:
speech_text = r.recognize_google(audio)
print(speech_text)
if speech_text.lower() in {'exit', 'stop'}:
break
except sr.UnknownValueError:
print("Could not understand!")
except sr.RequestError:
print("Could not request from google")

translated_test = GoogleTranslator(source='auto', target='hi').translate(text=speech_text)
print(translated_test)

voice = gTTS(translated_test, lang='hi')
voice.save('voice.mp3')
playsound('voice.mp3')
os.remove('voice.mp3')

0 comments on commit cb0117f

Please sign in to comment.