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("Spanish")
- 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 = [code for code, language in LANGUAGES.items() if language == from_language][0]
- to_language_code = [code for code, language in LANGUAGES.items() if language == to_language][0]
- 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")
- def trans(text,language):
- translated = GoogleTranslator(target=language).translate(text)
- return translated
- def text_to_speech(text, language, output_file):
- tts = gTTS(text=text, lang=language, slow=False)
- tts.save(output_file)
- def play_audio(file):
- audio = AudioSegment.from_file(file)
- play(audio)
- if __name__ == "__main__":
- app = TranslationApp()
- app.mainloop()
- input_text = input("Enter the text in English: ")
- language = 'my' #input("Enter the language code (e.g., 'en' for English, 'ja' for Japanese): ")
- 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"
- translated_text = trans(input_text,language)
- file = open('translation.txt', 'a')
- file.write('Original >> '+input_text)
- file.write('\n')
- file.write('Translated >> '+translated_text)
- file.write('\n')
- file.close()
- text_to_speech(translated_text, language, output_mp3)
- play_audio(output_mp3)
- # နေကောင်းလား
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement