ruslanmv commited on
Commit
d652e80
·
verified ·
1 Parent(s): ecabf05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -47
app.py CHANGED
@@ -5,6 +5,7 @@
5
 
6
  # importing all necessary libraries
7
  import httpcore
 
8
  import pathlib
9
  import sys, os
10
  from gtts import gTTS
@@ -32,13 +33,13 @@ def download_video(url):
32
  def validate_youtube(url):
33
  #This creates a youtube objet
34
  try:
35
- yt = YouTube(url)
36
  except Exception:
37
  print("Hi there URL seems invalid")
38
  return True
39
  #This will return the length of the video in sec as an int
40
  video_length = yt.length
41
- if video_length > 600:
42
  print("Your video is larger than 10 minutes")
43
  return True
44
  else:
@@ -51,7 +52,7 @@ def validate_url(url):
51
  print("Hi there URL seems invalid ")
52
  return True
53
  else:
54
- return False
55
 
56
 
57
  def cleanup():
@@ -62,16 +63,16 @@ def cleanup():
62
  junks = []
63
  for files in types:
64
  junks.extend(glob.glob(files))
65
- try:
66
  # Deleting those files
67
  for junk in junks:
68
  print("Deleting",junk)
69
  # Setting the path for the file to delete
70
  file = pathlib.Path(junk)
71
  # Calling the unlink method on the path
72
- file.unlink()
73
  except Exception:
74
- print("I cannot delete the file because it is being used by another process")
75
 
76
  def getSize(filename):
77
  st = os.stat(filename)
@@ -82,33 +83,33 @@ def clean_transcript(transcript_list):
82
  script = ""
83
  for text in transcript_list:
84
  t = text["text"]
85
- if( (t != '[music]') and \
86
- (t != '[Music]') and \
87
- (t != '[музыка]') and \
88
- (t != '[Музыка]') and \
89
- (t != '[musik]') and \
90
- (t != '[Musik]') and \
91
- (t != '[musica]') and \
92
- (t != '[Musica]') and \
93
- (t != '[música]') and \
94
- (t != '[Música]') and \
95
  (t != '[音楽]') and \
96
- (t != '[音乐]')
97
- ) :
98
  script += t + " "
99
  return script
100
-
101
-
102
  def get_transcript(url,desired_language):
103
  id_you= url[url.index("=")+1:]
104
- try:
105
  # retrieve the available transcripts
106
  transcript_list = YouTubeTranscriptApi.list_transcripts(id_you)
107
 
108
  except Exception:
109
  print('TranscriptsDisabled:')
110
  is_translated = False
111
- return " ", " ", is_translated
112
 
113
  lista=[]
114
  transcript_translation_languages=[]
@@ -130,12 +131,12 @@ def get_transcript(url,desired_language):
130
  matrix=matrix.tolist()
131
  is_manually=False
132
  is_automatic=False
133
- for lista in matrix:
134
  #print(lista)
135
  language_code=lista[0]
136
  is_generated=lista[1]
137
  is_translatable=lista[2]
138
- if not is_generated and is_translatable :
139
  print("Script found manually generated")
140
  is_manually=True
141
  language_code_man=language_code
@@ -143,12 +144,12 @@ def get_transcript(url,desired_language):
143
  print("Script found automatic generated")
144
  is_automatic=True
145
  language_code_au=language_code
146
-
147
  if is_manually:
148
  # we try filter for manually created transcripts
149
  print('We extract manually created transcripts')
150
- transcript = transcript_list.find_manually_created_transcript([language_code])
151
-
152
  elif is_automatic:
153
  print('We extract generated transcript')
154
  # or automatically generated ones, but not translated
@@ -168,13 +169,13 @@ def get_transcript(url,desired_language):
168
  transcript_translated=transcript_translated.fetch()
169
  translated=clean_transcript(transcript_translated)
170
  is_translated = True
171
- script_translated = ""
172
  if is_translated :
173
  script_translated = translated
174
 
175
  transcript=transcript.fetch()
176
  script = clean_transcript(transcript)
177
-
178
  return script, script_translated, is_translated
179
 
180
  # Set environment variables
@@ -225,7 +226,7 @@ def video_to_translate(url,initial_language,final_language):
225
  elif final_language == "Vietnamese":
226
  lang='vi'
227
  elif final_language == "Japanese":
228
- lang='ja'
229
  # Initial directory
230
  home_dir= os.getenv('home_dir')
231
  print('Initial directory:',home_dir)
@@ -271,15 +272,15 @@ def video_to_translate(url,initial_language,final_language):
271
  text=text+text_chunk+" "
272
  text=str(text)
273
  print(type(text))
274
-
275
  else:
276
  try:
277
  text = r.recognize_google(audio_data, language = lang_in)
278
  except Exception:
279
- print("This video cannot be recognized")
280
- cleanup()
281
- return "./demo/tryagain.mp4"
282
-
283
  #print(text)
284
  print("Destination language ",lang)
285
 
@@ -293,20 +294,20 @@ def video_to_translate(url,initial_language,final_language):
293
  print("This text cannot be translated")
294
  cleanup()
295
  return "./demo/tryagain.mp4"
296
-
297
  #translation.text
298
  trans=translation.text
299
 
300
- myobj = gTTS(text=trans, lang=lang, slow=False)
301
- myobj.save("audio.wav")
302
  # loading audio file
303
  audioclip = AudioFileClip("audio.wav")
304
-
305
  # adding audio to the video clip
306
  new_audioclip = CompositeAudioClip([audioclip])
307
  videoclip.audio = new_audioclip
308
  new_video="video_translated_"+lang+".mp4"
309
-
310
  # Return back to main directory
311
  os.chdir(home_dir)
312
  print('Final directory',os.getcwd())
@@ -326,7 +327,7 @@ gr.Interface(
326
  fn=video_to_translate,
327
  inputs=[url, initial_language, final_language],
328
  outputs='video',
329
- #verbose=True, # This parameter has been removed from gr.Interface
330
  title='Video Youtube Translator',
331
  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.',
332
  article='''<div>
@@ -335,11 +336,6 @@ gr.Interface(
335
  </p>
336
  </div>''',
337
  examples=[
338
- ["https://www.youtube.com/watch?v=uLVRZE8OAI4", "English", "Spanish"],
339
- ["https://www.youtube.com/watch?v=fkGCLIQx1MI", "English", "Russian"],
340
- ["https://www.youtube.com/watch?v=6Q6hFtitthQ", "Italian", "English"],
341
- ["https://www.youtube.com/watch?v=s5XvjAC7ai8", "Russian", "English"],
342
- ["https://www.youtube.com/watch?v=qzzweIQoIOU", "Japanese", "English"],
343
- ["https://www.youtube.com/watch?v=nOGZvu6tJFE", "German", "Spanish"]
344
  ]
345
  ).launch()
 
5
 
6
  # importing all necessary libraries
7
  import httpcore
8
+ #setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy') # This line is problematic and should be removed or corrected based on the intended use.
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=[]
 
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())
 
327
  fn=video_to_translate,
328
  inputs=[url, initial_language, final_language],
329
  outputs='video',
330
+ #verbose=True, # Removed verbose as it's no longer a valid parameter
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='''<div>
 
336
  </p>
337
  </div>''',
338
  examples=[
339
+ ["https://www.youtube.com/watch?v=uLVRZE8OAI4", "English", "Spanish"]
 
 
 
 
 
340
  ]
341
  ).launch()