Skip to content

Commit

Permalink
Merge pull request #1 from overcrash66/develop
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
overcrash66 authored Nov 22, 2024
2 parents f39e899 + 373909c commit 93f28a9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
4 changes: 3 additions & 1 deletion OpenTranslator/audio_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from autosub import Translator, GOOGLE_SPEECH_API_KEY
from gtts import gTTS

from transformers import AutoTokenizer, AutoModelForCausalLM

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class CustomTranslator:
Expand Down Expand Up @@ -159,7 +161,6 @@ def process_audio_chunk(self, input_path, target_language,src_lang, chunk_idx, o
model_Target_language = language_mapping.get(target_language, "en_XX")

# Generate tokens on the GPU
#generated_tokens = tt.generate(**model_inputs,forced_bos_token_id=tokenizer.lang_code_to_id[model_Target_language])
generated_tokens = tt.generate(input_ids=input_ids, forced_bos_token_id=tokenizer.lang_code_to_id[model_Target_language])

# Decode and join the translated text
Expand All @@ -170,6 +171,7 @@ def process_audio_chunk(self, input_path, target_language,src_lang, chunk_idx, o
end_time = time.time()
execution_time = (end_time - start_time) / 60
print(f"Local Translation Execution time: {execution_time:.2f} minutes")

if target_language == 'en':
translated_text = transcription

Expand Down
34 changes: 18 additions & 16 deletions OpenTranslator/translator_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def __init__(self, master):
helpdropdown = CustomDropdownMenu(widget=self.help, width=50)
helpdropdown.add_option(option="About", command=self.show_about)
master.title("Open Translator")
master.geometry("740x680")
master.minsize(740,680)
master.maxsize(740,680)
master.attributes('-fullscreen', False)
#master.geometry("740x680")
#master.minsize(740,680)
#master.maxsize(740,680)
#master.attributes('-fullscreen', False)

self.label = customtkinter.CTkLabel(master=master, text="Open Translator", font=("Comic Sans MS", 30, "bold"),text_color="red")
self.label = customtkinter.CTkLabel(master=master, text="Open Translator", font=("Arial", 30, "bold"),text_color="white")
self.label.pack(side="top", pady=10)

# Create a frame for widgets using pack
Expand Down Expand Up @@ -137,7 +137,8 @@ def __init__(self, master):
self.switch_1 = customtkinter.CTkSwitch(master=pack_frame, text="Play translated audio file", command=self.switch_event,variable=self.switch_var, onvalue="on", offvalue="off")
self.switch_1.pack(padx=20, pady=10)

self.stop_button = customtkinter.CTkButton(pack_frame, text="Stop Playing Translated File",fg_color="#222121",text_color='#222121',command=self.stop_playing)
#self.stop_button = customtkinter.CTkButton(pack_frame, text="Stop Playing Translated File",fg_color="#222121",text_color='#222121',command=self.stop_playing)
self.stop_button = customtkinter.CTkButton(pack_frame, text="Stop Playing Translated File",command=self.stop_playing)
self.stop_button.pack(pady=5)

# Create a frame for widgets using grid
Expand All @@ -147,7 +148,8 @@ def __init__(self, master):
#self.label_translated_text = customtkinter.CTkLabel(grid_frame, text="Translated Text:", font=("Arial", 16, "bold"), text_color="white")
#self.label_translated_text.grid(row=5, column=0, columnspan=2, pady=10)

self.clear_button = customtkinter.CTkButton(grid_frame, text="Clear",fg_color="#222121",text_color='#222121', command=self.clear_text)
#self.clear_button = customtkinter.CTkButton(grid_frame, text="Clear",fg_color="#222121",text_color='#222121', command=self.clear_text)
self.clear_button = customtkinter.CTkButton(grid_frame, text="Clear", command=self.clear_text)
self.clear_button.grid(row=6, column=0, columnspan=1, pady=10)

self.text_translated = tk.Text(grid_frame, height=20, width=45, wrap = 'word')
Expand Down Expand Up @@ -228,7 +230,7 @@ def run_ffmpeg_command(self, command, output_audio):

def VideoTextAdder(self):
def runVideoTextAdder():
VideoTextAdder_subprocess = subprocess.run(["python", VideoTextAdder_path], check=True)
VideoTextAdder_subprocess = subprocess.run(["py", VideoTextAdder_path], check=True)

VideoTextAdder_path = r'OpenTranslator\VideoTextAdder.py'
if os.path.exists(VideoTextAdder_path):
Expand All @@ -237,7 +239,7 @@ def runVideoTextAdder():

def AudioReplacerGUI(self):
def runAudioReplacerGUI():
AudioReplacerGUI_subprocess = subprocess.run(["python", AudioReplacerGUI_path], check=True)
AudioReplacerGUI_subprocess = subprocess.run(["py", AudioReplacerGUI_path], check=True)

AudioReplacerGUI_path = r'OpenTranslator\ReplaceVideoAudio.py'
if os.path.exists(AudioReplacerGUI_path):
Expand All @@ -246,7 +248,7 @@ def runAudioReplacerGUI():

def YouTubeDownloader(self):
def runYt():
YouTubeDownloader_subprocess = subprocess.run(["python", YouTubeDownloader_path], check=True)
YouTubeDownloader_subprocess = subprocess.run(["py", YouTubeDownloader_path], check=True)

YouTubeDownloader_path = r'OpenTranslator\youtube_downloader.py'
if os.path.exists(YouTubeDownloader_path):
Expand Down Expand Up @@ -310,7 +312,7 @@ def open_link(self):
webbrowser.open("https://github.com/overcrash66/OpenTranslator")

def show_about(self):
msg = CTkMessagebox(title="About",message = "Open Translator v1.0.0\n\nCreated by Wael Sahli\n\n",option_1="Visite our website",option_2="Close")
msg = CTkMessagebox(title="About",message = "Open Translator v1.0.1\n\nCreated by Wael Sahli\n\n",option_1="Visite our website",option_2="Close")
if msg.get()=='Visite our website':
self.open_link()

Expand All @@ -332,14 +334,14 @@ def clear_text(self):
self.text_translated.configure(state='disabled')
self.label_file_title.configure(text=f"")
self.save_button.configure(fg_color="#222121",text_color='#222121')
self.clear_button.configure(fg_color="#222121",text_color='#222121')
self.stop_button.configure(fg_color="#222121",text_color='#222121')
#self.clear_button.configure(fg_color="#222121",text_color='#222121')
#self.stop_button.configure(fg_color="#222121",text_color='#222121')

def run_translation(self, output_path):
input_file = self.audio_path
self.save_button.configure(fg_color="#222121",text_color='#222121')
self.clear_button.configure(fg_color="#222121",text_color='#222121')
self.stop_button.configure(fg_color="#222121",text_color='#222121')
#self.stop_button.configure(fg_color="#222121",text_color='#222121')
# Get the duration of the input audio file
ffprobe_cmd = f'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "{input_file}"'
input_duration = float(subprocess.check_output(ffprobe_cmd, shell=True))
Expand Down Expand Up @@ -431,7 +433,7 @@ def run_translation(self, output_path):
print("Audio File less or equal 30 sec !")
self.save_button.configure(fg_color="#222121",text_color='#222121')
self.clear_button.configure(fg_color="#222121",text_color='#222121')
self.stop_button.configure(fg_color="#222121",text_color='#222121')
#self.stop_button.configure(fg_color="#222121",text_color='#222121')
# Update label text
self.label_status.configure(
text=f"Translation in progress...",
Expand Down Expand Up @@ -467,7 +469,7 @@ def run_translation(self, output_path):
if self.switch_var.get() == 'on':
# Play the final merged audio file
self.translator_instance.play_audio(output_path)
self.stop_button.configure(fg_color="#2B7FA3",text_color='white')
#self.stop_button.configure(fg_color="#2B7FA3",text_color='white')

#self.progress_bar.stop()

Expand Down
38 changes: 15 additions & 23 deletions OpenTranslator/youtube_downloader.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from tkinter import filedialog, messagebox
from pytube import YouTube
from PIL import Image, ImageTk
import customtkinter
import subprocess
import re
import os
import threading
import unicodedata
import subprocess

class YouTubeDownloader:
def __init__(self):
Expand All @@ -22,7 +20,7 @@ def setup_gui(self):
url_label = customtkinter.CTkLabel(new_window, text="YouTube URL:")
url_label.grid(row=0, column=0, padx=10, pady=10)

self.url_entry = customtkinter.CTkEntry(new_window, width=300) # Set width to 300
self.url_entry = customtkinter.CTkEntry(new_window, width=300)
self.url_entry.grid(row=0, column=1, padx=10, pady=10)

self.download_button = customtkinter.CTkButton(new_window, text="Download", command=self.start_download)
Expand All @@ -38,7 +36,6 @@ def sanitize_filename(self, title):
title = unicodedata.normalize('NFKD', title).encode('ASCII', 'ignore').decode('utf-8')
return title


def start_download(self):
url = self.url_entry.get()

Expand All @@ -51,25 +48,20 @@ def start_download(self):

def download(self, url):
try:
yt = YouTube(url)
video_title = self.sanitize_filename(yt.title)
output_path = os.path.join(os.getcwd(), f"{video_title}.mp4")

if os.path.exists(output_path):
os.remove(output_path)
print(f"Deleted existing file: {output_path}")

output_path = os.getcwd()
self.status_label.configure(text="Downloading...")

video_stream = yt.streams.get_highest_resolution()
video_url = video_stream.url

subprocess.run(['ffmpeg', '-i', video_url, '-c', 'copy', output_path])

message = f"Download complete!\nFile saved as: {output_path}"
self.status_label.configure(text=message)

except Exception as e:

# Using yt-dlp to download the video
result = subprocess.run([
'yt-dlp',
'-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]',
'-o', f'{output_path}/%(title)s.%(ext)s',
url
], check=True)

self.status_label.configure(text="Download complete!")

except subprocess.CalledProcessError as e:
error_message = f"Error: {str(e)}"
self.status_label.configure(text=error_message)

Expand Down
15 changes: 14 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,22 @@ venv\Scripts\activate

If you would like to use CUDA 118 - GPU:

PY -3.10
```bash
pip install torch==2.1.2+cu118 torchaudio==2.1.2+cu118 --index-url https://download.pytorch.org/whl/cu118
pip install -r requirements.txt
```

PY -3.12
```bash
pip install torch==2.2.1+cu118 torchaudio==2.2.1+cu118 --index-url https://download.pytorch.org/whl/cu118
```

Install mecab
https://github.com/ikegami-yukino/mecab/releases


```bash
pip install -r requirements_Py312.txt
```

OR by default you use CPU only:
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
sounddevice
transformers
TTS
mecab-python3
coqui-tts
torchaudio
Pillow
pydub
pytube
yt-dlp
librosa
customtkinter
CTkMessagebox
Expand Down

0 comments on commit 93f28a9

Please sign in to comment.