Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from gtts import gTTS
- import os
- from pydub import AudioSegment
- from pydub.playback import play
- from deep_translator import GoogleTranslator
- from googletrans import Translator, LANGUAGES
- import tkinter as tk
- from tkinter import ttk
- class TranslationApp(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title("Translation Software")
- self.geometry("400x300")
- self.translator = Translator()
- self.create_widgets()
- def create_widgets(self):
- self.from_label = ttk.Label(self, text="From:")
- self.from_label.grid(column=0, row=0, padx=10, pady=10)
- self.from_language = ttk.Combobox(self, values=list(LANGUAGES.values()), state="readonly")
- self.from_language.set("english")
- self.from_language.grid(column=1, row=0, padx=10, pady=10)
- self.to_label = ttk.Label(self, text="To:")
- self.to_label.grid(column=2, row=0, padx=10, pady=10)
- self.to_language = ttk.Combobox(self, values=list(LANGUAGES.values()), state="readonly")
- self.to_language.set("myanmar (burmese)")
- self.to_language.grid(column=3, row=0, padx=10, pady=10)
- self.text_to_translate = tk.Text(self, wrap=tk.WORD, height=5)
- self.text_to_translate.grid(column=0, row=1, columnspan=4, padx=10, pady=10)
- self.translate_button = ttk.Button(self, text="Translate", command=self.translate_text)
- self.translate_button.grid(column=0, row=2, columnspan=4)
- self.translated_text = tk.Text(self, wrap=tk.WORD, height=5, state="disabled")
- self.translated_text.grid(column=0, row=3, columnspan=4, padx=10, pady=10)
- def translate_text(self):
- source_text = self.text_to_translate.get(1.0, tk.END).strip()
- from_language = self.from_language.get()
- to_language = self.to_language.get()
- if source_text and from_language and to_language:
- from_language_code = None
- to_language_code = None
- for code, language in LANGUAGES.items():
- if language == from_language:
- from_language_code = code
- if language == to_language:
- to_language_code = code
- if from_language_code and to_language_code:
- break
- if from_language_code and to_language_code:
- translation = self.translator.translate(source_text, src=from_language_code, dest=to_language_code)
- self.translated_text.config(state="normal")
- self.translated_text.delete(1.0, tk.END)
- self.translated_text.insert(tk.END, translation.text)
- self.translated_text.config(state="disabled")
- self.text_to_speech(translation.text,to_language_code,'out.mp3')
- self.play_audio('out.mp3')
- else:
- print("Error: Language not found.")
- def generate_output_mp3_filename(self):
- current_dir = os.getcwd()
- # list all file in dir
- files = os.listdir(current_dir)
- mp3 = []
- for file in files:
- if file.endswith(".mp3"):
- mp3.append(file)
- print(mp3)
- mp3.sort()
- print(mp3)
- # last element of list
- last_file = mp3[-1]
- # remove file extension
- last_file = last_file.split(".")[0] # strip extension , just file name
- last_file = eval(last_file) + 1
- output_mp3 = str(last_file) + ".mp3"
- return output_mp3
- def text_to_speech(self,text, language, output_file):
- tts = gTTS(text=text, lang=language, slow=False)
- tts.save(output_file)
- def play_audio(self,file):
- audio = AudioSegment.from_file(file)
- play(audio)
- # strating point
- if __name__ == "__main__":
- app = TranslationApp()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement