Advertisement
DrAungWinHtut

ttstranslate.py

Apr 22nd, 2023
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 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("Spanish")
  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 = [code for code, language in LANGUAGES.items() if language == from_language][0]
  53.             to_language_code = [code for code, language in LANGUAGES.items() if language == to_language][0]
  54.  
  55.             translation = self.translator.translate(source_text, src=from_language_code, dest=to_language_code)
  56.  
  57.             self.translated_text.config(state="normal")
  58.             self.translated_text.delete(1.0, tk.END)
  59.             self.translated_text.insert(tk.END, translation.text)
  60.             self.translated_text.config(state="disabled")
  61.  
  62.  
  63.  
  64.  
  65. def trans(text,language):
  66.     translated = GoogleTranslator(target=language).translate(text)
  67.     return translated
  68.  
  69. def text_to_speech(text, language, output_file):
  70.     tts = gTTS(text=text, lang=language, slow=False)
  71.     tts.save(output_file)
  72.  
  73. def play_audio(file):
  74.     audio = AudioSegment.from_file(file)
  75.     play(audio)
  76.  
  77. if __name__ == "__main__":
  78.     app = TranslationApp()
  79.     app.mainloop()
  80.  
  81. input_text = input("Enter the text in English: ")
  82. language =  'my' #input("Enter the language code (e.g., 'en' for English, 'ja' for Japanese): ")
  83.  
  84. current_dir = os.getcwd()
  85. # list all file in dir
  86. files = os.listdir(current_dir)
  87. mp3 = []
  88. for file in files:
  89.     if file.endswith(".mp3"):
  90.         mp3.append(file)
  91. print(mp3)
  92. mp3.sort()
  93. print(mp3)
  94. # last element of list
  95. last_file = mp3[-1]
  96. # remove file extension
  97. last_file = last_file.split(".")[0] # strip extension , just file name
  98. last_file = eval(last_file) + 1
  99.        
  100. output_mp3 = str(last_file) + ".mp3"
  101.  
  102. translated_text = trans(input_text,language)
  103. file = open('translation.txt', 'a')
  104. file.write('Original >> '+input_text)
  105. file.write('\n')
  106. file.write('Translated >> '+translated_text)
  107. file.write('\n')
  108. file.close()
  109.  
  110. text_to_speech(translated_text, language, output_mp3)
  111. play_audio(output_mp3)
  112.  
  113. # နေကောင်းလား
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement