JulianHame's picture
Update app.py
edf2e81
# Importing required libraries and modules
import streamlit as st # Import streamlit
from pytube import YouTube # Import YouTube from pytube
from pytube import extract # Import extract from pytube
from youtube_transcript_api import YouTubeTranscriptApi # Import YouTubeTranscriptApi
import whisper # Import whisper library
import pandas as pd # Import pandas
import numpy as np # Import numpy
import argostranslate.package # Import argostranslate.package
import argostranslate.translate # Import argostranslate.translate
import os # Import os
from gtts import gTTS # Import gTTS
st.title('Foreign Whispers - YouTube Video Translation') # Header of application
# Take YouTube video link as input
video_url = st.text_input('Paste the link of a YouTube video:',
value = "https://www.youtube.com/watch?v=2ylerrtD_4Y")
# Dropdown box for selecting target language
language = st.selectbox('Translate video to:',
("Chinese", "French", "German", "Greek", "Hindi", "Italian",
"Japanese", "Korean", "Portuguese", "Russian", "Spanish", "Turkish"),
placeholder="Select a language...")
translate_button = st.button("Translate Video") # Button to start translation process
if translate_button: # Upon button activation
st.divider() # Divider
video_id = extract.video_id(video_url) # Acquire video ID to get transcript
vid = YouTube(video_url) # Create YouTube object of video
video = vid.streams.get_highest_resolution() # Get video information on highest offered resolution
video = np.asarray(video) # Turn video in numpyarray
st.subheader(vid.streams[0].title) # Display video title
argostranslate.package.update_package_index() # Update Argos package index
packages = argostranslate.package.get_available_packages() # Install Argos Translate package
in_language = "en" # Set input language as English
out_language = "es" # Default output language to Spanish
if language == "Chinese":
out_language = "zh" # Set output language as Chinese
if language == "French":
out_language = "fr" # Set output language as French
if language == "German":
out_language = "de" # Set output language as German
if language == "Greek":
out_language = "el" # Set output language as Greek
if language == "Hindi":
out_language = "hi" # Set output language as Hindi
if language == "Italian":
out_language = "it" # Set output language as Italian
if language == "Japanese":
out_language = "ja" # Set output language as Japanese
if language == "Korean":
out_language = "ko" # Set output language as Korean
if language == "Portuguese":
out_language = "pt" # Set output language as Portuguese
if language == "Russian":
out_language = "ru" # Set output language as Russian
if language == "Spanish":
out_language = "es" # Set output language as Spanish
if language == "Turkish":
out_language = "tr" # Set output language as Turkish
package_to_install = next( # Initialize languages to install
filter(lambda x: x.from_code == in_language # Install input language (English)
and x.to_code == out_language, packages)) # Install output language
argostranslate.package.install_from_path(package_to_install.download()) # Install languages
transcript = YouTubeTranscriptApi.get_transcript(video_id) # Acquire video transcript
timestamps = [] # Initialize timestamp list
original_transcript = [] # Initialize original transcript list
translated_transcript = [] # Initialize translation list
for phrase in transcript: # For every phrase in the transcript
timestamps.append(phrase['start']) # Add timestamp to timestamp list
original_transcript.append(phrase['text']) # Add phrase to transcript list
translated_text = argostranslate.translate.translate(phrase['text'], in_language, out_language) # Perform translation
translated_transcript.append(translated_text) # Add translated phrase to list
# Construct dataframe of timestamps, original phrases and translated phrases
data = {'Timestamp': timestamps,
'Original Transcript': original_transcript,
'Translated Transcript': translated_transcript}
df = pd.DataFrame(data) # Create a pandas dataframe with transcript data
st.dataframe(df) # Print out table of the dataframe to the user
full_translated_transcript = ' '.join(translated_transcript) # Piece phrases together into a full transcript
tts = gTTS(text=full_translated_transcript, lang=out_language) # Perform TTS on full transcript
translated_audio = 'translated_transcript_audio.mp3' # Set filename of TTS audio
tts.save(translated_audio) # Save TTS audio to file
st.audio("translated_transcript_audio.mp3") # Display audio player for TTS audio