Advertisement
DrAungWinHtut

ttsTranslateGUI.py

Apr 22nd, 2023
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. from gtts import gTTS
  2. import os
  3. from pydub import AudioSegment
  4. from pydub.playback import play
  5. from deep_translator import GoogleTranslator
  6. from googletrans import Translator, LANGUAGES
  7. import tkinter as tk
  8. from tkinter import ttk
  9.  
  10.  
  11. class TranslationApp(tk.Tk):
  12.     def __init__(self):
  13.         super().__init__()
  14.  
  15.         self.title("Translation Software")
  16.         self.geometry("400x300")
  17.  
  18.         self.translator = Translator()
  19.  
  20.         self.create_widgets()
  21.  
  22.     def create_widgets(self):
  23.         self.from_label = ttk.Label(self, text="From:")
  24.         self.from_label.grid(column=0, row=0, padx=10, pady=10)
  25.  
  26.         self.from_language = ttk.Combobox(self, values=list(LANGUAGES.values()), state="readonly")
  27.         self.from_language.set("english")
  28.         self.from_language.grid(column=1, row=0, padx=10, pady=10)
  29.  
  30.         self.to_label = ttk.Label(self, text="To:")
  31.         self.to_label.grid(column=2, row=0, padx=10, pady=10)
  32.  
  33.         self.to_language = ttk.Combobox(self, values=list(LANGUAGES.values()), state="readonly")
  34.         self.to_language.set("myanmar (burmese)")
  35.         self.to_language.grid(column=3, row=0, padx=10, pady=10)
  36.  
  37.         self.text_to_translate = tk.Text(self, wrap=tk.WORD, height=5)
  38.         self.text_to_translate.grid(column=0, row=1, columnspan=4, padx=10, pady=10)
  39.  
  40.         self.translate_button = ttk.Button(self, text="Translate", command=self.translate_text)
  41.         self.translate_button.grid(column=0, row=2, columnspan=4)
  42.  
  43.         self.translated_text = tk.Text(self, wrap=tk.WORD, height=5, state="disabled")
  44.         self.translated_text.grid(column=0, row=3, columnspan=4, padx=10, pady=10)
  45.  
  46.     def translate_text(self):
  47.         source_text = self.text_to_translate.get(1.0, tk.END).strip()
  48.         from_language = self.from_language.get()
  49.         to_language = self.to_language.get()
  50.  
  51.         if source_text and from_language and to_language:
  52.             from_language_code = None
  53.             to_language_code = None
  54.  
  55.             for code, language in LANGUAGES.items():
  56.                 if language == from_language:
  57.                     from_language_code = code
  58.                 if language == to_language:
  59.                     to_language_code = code
  60.                 if from_language_code and to_language_code:
  61.                     break
  62.  
  63.             if from_language_code and to_language_code:
  64.                 translation = self.translator.translate(source_text, src=from_language_code, dest=to_language_code)
  65.  
  66.                 self.translated_text.config(state="normal")
  67.                 self.translated_text.delete(1.0, tk.END)
  68.                 self.translated_text.insert(tk.END, translation.text)
  69.                 self.translated_text.config(state="disabled")
  70.                 self.text_to_speech(translation.text,to_language_code,'out.mp3')
  71.                 self.play_audio('out.mp3')
  72.             else:
  73.                 print("Error: Language not found.")
  74.  
  75.     def generate_output_mp3_filename(self):
  76.         current_dir = os.getcwd()
  77.         # list all file in dir
  78.         files = os.listdir(current_dir)
  79.         mp3 = []
  80.         for file in files:
  81.             if file.endswith(".mp3"):
  82.                 mp3.append(file)
  83.         print(mp3)
  84.         mp3.sort()
  85.         print(mp3)
  86.         # last element of list
  87.         last_file = mp3[-1]
  88.         # remove file extension
  89.         last_file = last_file.split(".")[0] # strip extension , just file name
  90.         last_file = eval(last_file) + 1
  91.         output_mp3 = str(last_file) + ".mp3"
  92.         return output_mp3
  93.     def text_to_speech(self,text, language, output_file):
  94.         tts = gTTS(text=text, lang=language, slow=False)
  95.         tts.save(output_file)
  96.  
  97.     def play_audio(self,file):
  98.         audio = AudioSegment.from_file(file)
  99.         play(audio)
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. # strating point
  112. if __name__ == "__main__":
  113.     app = TranslationApp()
  114.     app.mainloop()
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement