Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
|
| 6 |
# importing all necessary libraries
|
| 7 |
import httpcore
|
| 8 |
-
|
| 9 |
import pathlib
|
| 10 |
import sys, os
|
| 11 |
from gtts import gTTS
|
|
@@ -33,13 +33,13 @@ def download_video(url):
|
|
| 33 |
def validate_youtube(url):
|
| 34 |
#This creates a youtube objet
|
| 35 |
try:
|
| 36 |
-
yt = YouTube(url)
|
| 37 |
except Exception:
|
| 38 |
print("Hi there URL seems invalid")
|
| 39 |
return True
|
| 40 |
#This will return the length of the video in sec as an int
|
| 41 |
video_length = yt.length
|
| 42 |
-
if
|
| 43 |
print("Your video is larger than 10 minutes")
|
| 44 |
return True
|
| 45 |
else:
|
|
@@ -52,7 +52,7 @@ def validate_url(url):
|
|
| 52 |
print("Hi there URL seems invalid ")
|
| 53 |
return True
|
| 54 |
else:
|
| 55 |
-
return False
|
| 56 |
|
| 57 |
|
| 58 |
def cleanup():
|
|
@@ -63,16 +63,16 @@ def cleanup():
|
|
| 63 |
junks = []
|
| 64 |
for files in types:
|
| 65 |
junks.extend(glob.glob(files))
|
| 66 |
-
try:
|
| 67 |
# Deleting those files
|
| 68 |
for junk in junks:
|
| 69 |
print("Deleting",junk)
|
| 70 |
# Setting the path for the file to delete
|
| 71 |
file = pathlib.Path(junk)
|
| 72 |
# Calling the unlink method on the path
|
| 73 |
-
file.unlink()
|
| 74 |
except Exception:
|
| 75 |
-
print("I cannot delete the file because it is being used by another process")
|
| 76 |
|
| 77 |
def getSize(filename):
|
| 78 |
st = os.stat(filename)
|
|
@@ -83,33 +83,33 @@ def clean_transcript(transcript_list):
|
|
| 83 |
script = ""
|
| 84 |
for text in transcript_list:
|
| 85 |
t = text["text"]
|
| 86 |
-
if( (t != '[music]') and
|
| 87 |
-
(t != '[Music]') and
|
| 88 |
-
(t != '[музыка]') and
|
| 89 |
-
(t != '[Музыка]') and
|
| 90 |
-
(t != '[musik]') and
|
| 91 |
-
(t != '[Musik]') and
|
| 92 |
-
(t != '[musica]') and
|
| 93 |
-
(t != '[Musica]') and
|
| 94 |
-
(t != '[música]') and
|
| 95 |
-
(t != '[Música]') and
|
| 96 |
(t != '[音楽]') and \
|
| 97 |
-
(t != '[音乐]')
|
| 98 |
-
|
| 99 |
script += t + " "
|
| 100 |
return script
|
| 101 |
-
|
| 102 |
-
|
| 103 |
def get_transcript(url,desired_language):
|
| 104 |
id_you= url[url.index("=")+1:]
|
| 105 |
-
try:
|
| 106 |
# retrieve the available transcripts
|
| 107 |
transcript_list = YouTubeTranscriptApi.list_transcripts(id_you)
|
| 108 |
|
| 109 |
except Exception:
|
| 110 |
print('TranscriptsDisabled:')
|
| 111 |
is_translated = False
|
| 112 |
-
return " ", " ", is_translated
|
| 113 |
|
| 114 |
lista=[]
|
| 115 |
transcript_translation_languages=[]
|
|
@@ -120,7 +120,7 @@ def get_transcript(url,desired_language):
|
|
| 120 |
transcript.is_generated,
|
| 121 |
transcript.is_translatable,
|
| 122 |
transcript_translation_languages.append(transcript.translation_languages),
|
| 123 |
-
|
| 124 |
print(lista)
|
| 125 |
n_size=int(len(lista)/4)
|
| 126 |
print("There are {} avialable scripts".format(n_size))
|
|
@@ -131,12 +131,12 @@ def get_transcript(url,desired_language):
|
|
| 131 |
matrix=matrix.tolist()
|
| 132 |
is_manually=False
|
| 133 |
is_automatic=False
|
| 134 |
-
for lista in matrix:
|
| 135 |
#print(lista)
|
| 136 |
language_code=lista[0]
|
| 137 |
is_generated=lista[1]
|
| 138 |
is_translatable=lista[2]
|
| 139 |
-
if not is_generated and is_translatable :
|
| 140 |
print("Script found manually generated")
|
| 141 |
is_manually=True
|
| 142 |
language_code_man=language_code
|
|
@@ -144,12 +144,12 @@ def get_transcript(url,desired_language):
|
|
| 144 |
print("Script found automatic generated")
|
| 145 |
is_automatic=True
|
| 146 |
language_code_au=language_code
|
| 147 |
-
|
| 148 |
if is_manually:
|
| 149 |
# we try filter for manually created transcripts
|
| 150 |
print('We extract manually created transcripts')
|
| 151 |
-
transcript = transcript_list.find_manually_created_transcript([language_code])
|
| 152 |
-
|
| 153 |
elif is_automatic:
|
| 154 |
print('We extract generated transcript')
|
| 155 |
# or automatically generated ones, but not translated
|
|
@@ -169,13 +169,13 @@ def get_transcript(url,desired_language):
|
|
| 169 |
transcript_translated=transcript_translated.fetch()
|
| 170 |
translated=clean_transcript(transcript_translated)
|
| 171 |
is_translated = True
|
| 172 |
-
script_translated = ""
|
| 173 |
if is_translated :
|
| 174 |
script_translated = translated
|
| 175 |
|
| 176 |
transcript=transcript.fetch()
|
| 177 |
script = clean_transcript(transcript)
|
| 178 |
-
|
| 179 |
return script, script_translated, is_translated
|
| 180 |
|
| 181 |
# Set environment variables
|
|
@@ -226,7 +226,7 @@ def video_to_translate(url,initial_language,final_language):
|
|
| 226 |
elif final_language == "Vietnamese":
|
| 227 |
lang='vi'
|
| 228 |
elif final_language == "Japanese":
|
| 229 |
-
lang='ja'
|
| 230 |
# Initial directory
|
| 231 |
home_dir= os.getenv('home_dir')
|
| 232 |
print('Initial directory:',home_dir)
|
|
@@ -272,15 +272,15 @@ def video_to_translate(url,initial_language,final_language):
|
|
| 272 |
text=text+text_chunk+" "
|
| 273 |
text=str(text)
|
| 274 |
print(type(text))
|
| 275 |
-
|
| 276 |
else:
|
| 277 |
try:
|
| 278 |
text = r.recognize_google(audio_data, language = lang_in)
|
| 279 |
except Exception:
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
#print(text)
|
| 285 |
print("Destination language ",lang)
|
| 286 |
|
|
@@ -294,20 +294,20 @@ def video_to_translate(url,initial_language,final_language):
|
|
| 294 |
print("This text cannot be translated")
|
| 295 |
cleanup()
|
| 296 |
return "./demo/tryagain.mp4"
|
| 297 |
-
|
| 298 |
#translation.text
|
| 299 |
trans=translation.text
|
| 300 |
|
| 301 |
-
myobj = gTTS(text=trans, lang=lang, slow=False)
|
| 302 |
-
myobj.save("audio.wav")
|
| 303 |
# loading audio file
|
| 304 |
audioclip = AudioFileClip("audio.wav")
|
| 305 |
-
|
| 306 |
# adding audio to the video clip
|
| 307 |
new_audioclip = CompositeAudioClip([audioclip])
|
| 308 |
videoclip.audio = new_audioclip
|
| 309 |
new_video="video_translated_"+lang+".mp4"
|
| 310 |
-
|
| 311 |
# Return back to main directory
|
| 312 |
os.chdir(home_dir)
|
| 313 |
print('Final directory',os.getcwd())
|
|
@@ -319,24 +319,31 @@ def video_to_translate(url,initial_language,final_language):
|
|
| 319 |
|
| 320 |
return new_video
|
| 321 |
|
| 322 |
-
initial_language = gr.Dropdown(["English",
|
| 323 |
-
final_language = gr.Dropdown(["Russian",
|
| 324 |
-
url =
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# importing all necessary libraries
|
| 7 |
import httpcore
|
| 8 |
+
setattr(httpcore, 'SyncHTTPTransport', Any)
|
| 9 |
import pathlib
|
| 10 |
import sys, os
|
| 11 |
from gtts import gTTS
|
|
|
|
| 33 |
def validate_youtube(url):
|
| 34 |
#This creates a youtube objet
|
| 35 |
try:
|
| 36 |
+
yt = YouTube(url)
|
| 37 |
except Exception:
|
| 38 |
print("Hi there URL seems invalid")
|
| 39 |
return True
|
| 40 |
#This will return the length of the video in sec as an int
|
| 41 |
video_length = yt.length
|
| 42 |
+
if video_length > 600:
|
| 43 |
print("Your video is larger than 10 minutes")
|
| 44 |
return True
|
| 45 |
else:
|
|
|
|
| 52 |
print("Hi there URL seems invalid ")
|
| 53 |
return True
|
| 54 |
else:
|
| 55 |
+
return False
|
| 56 |
|
| 57 |
|
| 58 |
def cleanup():
|
|
|
|
| 63 |
junks = []
|
| 64 |
for files in types:
|
| 65 |
junks.extend(glob.glob(files))
|
| 66 |
+
try:
|
| 67 |
# Deleting those files
|
| 68 |
for junk in junks:
|
| 69 |
print("Deleting",junk)
|
| 70 |
# Setting the path for the file to delete
|
| 71 |
file = pathlib.Path(junk)
|
| 72 |
# Calling the unlink method on the path
|
| 73 |
+
file.unlink()
|
| 74 |
except Exception:
|
| 75 |
+
print("I cannot delete the file because it is being used by another process")
|
| 76 |
|
| 77 |
def getSize(filename):
|
| 78 |
st = os.stat(filename)
|
|
|
|
| 83 |
script = ""
|
| 84 |
for text in transcript_list:
|
| 85 |
t = text["text"]
|
| 86 |
+
if( (t != '[music]') and \
|
| 87 |
+
(t != '[Music]') and \
|
| 88 |
+
(t != '[музыка]') and \
|
| 89 |
+
(t != '[Музыка]') and \
|
| 90 |
+
(t != '[musik]') and \
|
| 91 |
+
(t != '[Musik]') and \
|
| 92 |
+
(t != '[musica]') and \
|
| 93 |
+
(t != '[Musica]') and \
|
| 94 |
+
(t != '[música]') and \
|
| 95 |
+
(t != '[Música]') and \
|
| 96 |
(t != '[音楽]') and \
|
| 97 |
+
(t != '[音乐]')
|
| 98 |
+
) :
|
| 99 |
script += t + " "
|
| 100 |
return script
|
| 101 |
+
|
| 102 |
+
|
| 103 |
def get_transcript(url,desired_language):
|
| 104 |
id_you= url[url.index("=")+1:]
|
| 105 |
+
try:
|
| 106 |
# retrieve the available transcripts
|
| 107 |
transcript_list = YouTubeTranscriptApi.list_transcripts(id_you)
|
| 108 |
|
| 109 |
except Exception:
|
| 110 |
print('TranscriptsDisabled:')
|
| 111 |
is_translated = False
|
| 112 |
+
return " ", " ", is_translated
|
| 113 |
|
| 114 |
lista=[]
|
| 115 |
transcript_translation_languages=[]
|
|
|
|
| 120 |
transcript.is_generated,
|
| 121 |
transcript.is_translatable,
|
| 122 |
transcript_translation_languages.append(transcript.translation_languages),
|
| 123 |
+
])
|
| 124 |
print(lista)
|
| 125 |
n_size=int(len(lista)/4)
|
| 126 |
print("There are {} avialable scripts".format(n_size))
|
|
|
|
| 131 |
matrix=matrix.tolist()
|
| 132 |
is_manually=False
|
| 133 |
is_automatic=False
|
| 134 |
+
for lista in matrix:
|
| 135 |
#print(lista)
|
| 136 |
language_code=lista[0]
|
| 137 |
is_generated=lista[1]
|
| 138 |
is_translatable=lista[2]
|
| 139 |
+
if not is_generated and is_translatable :
|
| 140 |
print("Script found manually generated")
|
| 141 |
is_manually=True
|
| 142 |
language_code_man=language_code
|
|
|
|
| 144 |
print("Script found automatic generated")
|
| 145 |
is_automatic=True
|
| 146 |
language_code_au=language_code
|
| 147 |
+
|
| 148 |
if is_manually:
|
| 149 |
# we try filter for manually created transcripts
|
| 150 |
print('We extract manually created transcripts')
|
| 151 |
+
transcript = transcript_list.find_manually_created_transcript([language_code])
|
| 152 |
+
|
| 153 |
elif is_automatic:
|
| 154 |
print('We extract generated transcript')
|
| 155 |
# or automatically generated ones, but not translated
|
|
|
|
| 169 |
transcript_translated=transcript_translated.fetch()
|
| 170 |
translated=clean_transcript(transcript_translated)
|
| 171 |
is_translated = True
|
| 172 |
+
script_translated = ""
|
| 173 |
if is_translated :
|
| 174 |
script_translated = translated
|
| 175 |
|
| 176 |
transcript=transcript.fetch()
|
| 177 |
script = clean_transcript(transcript)
|
| 178 |
+
|
| 179 |
return script, script_translated, is_translated
|
| 180 |
|
| 181 |
# Set environment variables
|
|
|
|
| 226 |
elif final_language == "Vietnamese":
|
| 227 |
lang='vi'
|
| 228 |
elif final_language == "Japanese":
|
| 229 |
+
lang='ja'
|
| 230 |
# Initial directory
|
| 231 |
home_dir= os.getenv('home_dir')
|
| 232 |
print('Initial directory:',home_dir)
|
|
|
|
| 272 |
text=text+text_chunk+" "
|
| 273 |
text=str(text)
|
| 274 |
print(type(text))
|
| 275 |
+
|
| 276 |
else:
|
| 277 |
try:
|
| 278 |
text = r.recognize_google(audio_data, language = lang_in)
|
| 279 |
except Exception:
|
| 280 |
+
print("This video cannot be recognized")
|
| 281 |
+
cleanup()
|
| 282 |
+
return "./demo/tryagain.mp4"
|
| 283 |
+
|
| 284 |
#print(text)
|
| 285 |
print("Destination language ",lang)
|
| 286 |
|
|
|
|
| 294 |
print("This text cannot be translated")
|
| 295 |
cleanup()
|
| 296 |
return "./demo/tryagain.mp4"
|
| 297 |
+
|
| 298 |
#translation.text
|
| 299 |
trans=translation.text
|
| 300 |
|
| 301 |
+
myobj = gTTS(text=trans, lang=lang, slow=False)
|
| 302 |
+
myobj.save("audio.wav")
|
| 303 |
# loading audio file
|
| 304 |
audioclip = AudioFileClip("audio.wav")
|
| 305 |
+
|
| 306 |
# adding audio to the video clip
|
| 307 |
new_audioclip = CompositeAudioClip([audioclip])
|
| 308 |
videoclip.audio = new_audioclip
|
| 309 |
new_video="video_translated_"+lang+".mp4"
|
| 310 |
+
|
| 311 |
# Return back to main directory
|
| 312 |
os.chdir(home_dir)
|
| 313 |
print('Final directory',os.getcwd())
|
|
|
|
| 319 |
|
| 320 |
return new_video
|
| 321 |
|
| 322 |
+
initial_language = gr.inputs.Dropdown(["English","Italian","Japanese","Russian","Spanish","German"])
|
| 323 |
+
final_language = gr.inputs.Dropdown([ "Russian","Italian","Spanish","German","English","Japanese"])
|
| 324 |
+
url =gr.inputs.Textbox(label = "Enter the YouTube URL below:")
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
gr.Interface(fn = video_to_translate,
|
| 328 |
+
inputs = [url,initial_language,final_language],
|
| 329 |
+
outputs = 'video',
|
| 330 |
+
verbose = True,
|
| 331 |
+
title = 'Video Youtube Translator',
|
| 332 |
+
description = 'A simple application that translates Youtube small videos from English, Italian, Japanese, Russian, Spanish, and German to Italian, Spanish, Russian, English and Japanese. Wait one minute to process.',
|
| 333 |
+
article =
|
| 334 |
+
'''<div>
|
| 335 |
+
<p style="text-align: center"> All you need to do is to paste the Youtube link and hit submit,, then wait for compiling. After that click on Play/Pause for listing to the video. The video is saved in an mp4 format.
|
| 336 |
+
The lenght video limit is 10 minutes. For more information visit <a href="https://ruslanmv.com/">ruslanmv.com</a>
|
| 337 |
+
</p>
|
| 338 |
+
</div>''',
|
| 339 |
+
|
| 340 |
+
examples = [
|
| 341 |
+
["https://www.youtube.com/watch?v=uLVRZE8OAI4", "English","Spanish"],
|
| 342 |
+
["https://www.youtube.com/watch?v=fkGCLIQx1MI", "English","Russian"],
|
| 343 |
+
["https://www.youtube.com/watch?v=6Q6hFtitthQ", "Italian","English"],
|
| 344 |
+
["https://www.youtube.com/watch?v=s5XvjAC7ai8", "Russian","English"],
|
| 345 |
+
["https://www.youtube.com/watch?v=qzzweIQoIOU", "Japanese","English"],
|
| 346 |
+
["https://www.youtube.com/watch?v=nOGZvu6tJFE", "German","Spanish"]
|
| 347 |
+
|
| 348 |
+
]
|
| 349 |
+
).launch()
|