mimbres commited on
Commit
82c7bd7
·
verified ·
1 Parent(s): 6705e2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -45
app.py CHANGED
@@ -12,13 +12,15 @@ from ctypes import ArgumentError
12
  from html_helper import *
13
  from model_helper import *
14
 
15
- # from pytube import YouTube
16
- from pytubefix import YouTube
17
  import torchaudio
18
  import glob
19
  import gradio as gr
 
 
20
 
21
-
 
 
22
 
23
  # @title Load Checkpoint
24
  model_name = 'YPTF.MoE+Multi (noPS)' # @param ["YMT3+", "YPTF+Single (noPS)", "YPTF+Multi (PS)", "YPTF.MoE+Multi (noPS)", "YPTF.MoE+Multi (PS)"]
@@ -65,44 +67,27 @@ def prepare_media(source_path_or_url: os.PathLike,
65
  if source_type == 'audio_filepath':
66
  audio_file = source_path_or_url
67
  elif source_type == 'youtube_url':
68
- # proxy_handler = {"http": "http://108.61.175.7:31802", "https":"http://190.187.201.26:8080", "https":"http://38.54.71.67:80"}
69
- # yt = YouTube(source_path_or_url, proxies=proxy_handler) #use_oauth=True, allow_oauth_cache=False)
70
- # audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
71
- # mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
72
- # audio_file = mp4_file[:-3] + 'mp3'
73
- # subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
74
- # os.remove(mp4_file)
75
  # Download from youtube
76
- try:
77
- # Try PyTube first
78
- # proxy_handler = {"http": "http://127.0.0.1:1087", "https":"http://127.0.0.1:1087"}
79
- # yt = YouTube(source_path_or_url, proxies=proxy_handler)
80
- yt = YouTube(source_path_or_url)
81
- audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
82
- mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
83
- audio_file = mp4_file[:-3] + 'mp3'
84
- subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
85
- os.remove(mp4_file)
86
- except Exception as e:
87
- try:
88
- # Try alternative
89
- print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
90
- audio_file = './downloaded/yt_audio'
91
- # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
92
- # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
93
- # '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
94
- # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
95
- # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
96
- # '--force-overwrites', '--username', '[email protected]', '--password', '',
97
- # '-v'])
98
- subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
99
- '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
100
- '--force-overwrites', '--cache-dir', 'amt/src/extras/auth2',
101
- '-v'])
102
- audio_file += '.mp3'
103
- except Exception as e:
104
- print(f"Alternative downloader failed, error: {e}. Please try again later!")
105
- return None
106
  else:
107
  raise ValueError(source_type)
108
 
@@ -142,15 +127,15 @@ def play_video(youtube_url):
142
  return None
143
  return create_html_youtube_player(youtube_url)
144
 
145
-
 
146
 
147
  AUDIO_EXAMPLES = glob.glob('examples/*.*', recursive=True)
148
- YOUTUBE_EXAMPLES = ["https://www.youtube.com/watch?v=vMboypSkj3c",
149
- "https://youtu.be/5vJBhdjvVcE?si=s3NFG_SlVju0Iklg",
150
  "https://youtu.be/vRd5KEjX8vw?si=b-qw633ZjaX6Uxy5",
151
- "https://youtu.be/EOJ0wH6h3rE?si=a99k6BnSajvNmXcn",
152
- "https://youtu.be/7mjQooXt28o?si=qqmMxCxwqBlLPDI2",
153
  "https://youtu.be/bnS-HK_lTHA?si=PQLVAab3QHMbv0S3https://youtu.be/zJB0nnOc7bM?si=EA1DN8nHWJcpQWp_",
 
154
  "https://youtu.be/mIWYTg55h10?si=WkbtKfL6NlNquvT8"]
155
 
156
  theme = gr.Theme.from_hub("gradio/dracula_revamped")
@@ -243,6 +228,8 @@ with gr.Blocks(theme=theme, css=css) as demo:
243
  with gr.Column(scale=4):
244
  # Submit button
245
  transcribe_video_button = gr.Button("Transcribe", variant="primary")
 
 
246
  with gr.Column(scale=1):
247
  # Transcribe
248
  output_tab2 = gr.HTML(render=True)
@@ -250,5 +237,10 @@ with gr.Blocks(theme=theme, css=css) as demo:
250
  transcribe_video_button.click(process_video, inputs=youtube_url, outputs=output_tab2)
251
  # Play
252
  play_video_button.click(play_video, inputs=youtube_url, outputs=youtube_player)
 
 
 
 
 
253
 
254
  demo.launch(debug=True)
 
12
  from html_helper import *
13
  from model_helper import *
14
 
 
 
15
  import torchaudio
16
  import glob
17
  import gradio as gr
18
+ from gradio_log import Log
19
+ from pathlib import Path
20
 
21
+ # gradio_log
22
+ log_file = 'amt/log.txt'
23
+ Path(log_file).touch()
24
 
25
  # @title Load Checkpoint
26
  model_name = 'YPTF.MoE+Multi (noPS)' # @param ["YMT3+", "YPTF+Single (noPS)", "YPTF+Multi (PS)", "YPTF.MoE+Multi (noPS)", "YPTF.MoE+Multi (PS)"]
 
67
  if source_type == 'audio_filepath':
68
  audio_file = source_path_or_url
69
  elif source_type == 'youtube_url':
 
 
 
 
 
 
 
70
  # Download from youtube
71
+ with open(log_file, 'w') as lf:
72
+ audio_file = './downloaded/yt_audio'
73
+ command = ['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
74
+ '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
75
+ '--force-overwrites', '--username', 'oauth2', '--password', '', '-v']
76
+ if simulate:
77
+ command = command + ['-s']
78
+ process = subprocess.Popen(command,
79
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
80
+
81
+ for line in iter(process.stdout.readline, ''):
82
+ # Filter out unnecessary messages
83
+ if "www.google.com/device" in line:
84
+ hl_text = line.replace("https://www.google.com/device", "\033[93mhttps://www.google.com/device\x1b[0m").split()
85
+ hl_text[-1] = "\x1b[31;1m" + hl_text[-1] + "\x1b[0m"
86
+ lf.write(' '.join(hl_text[])); lf.flush()
87
+ process.stdout.close()
88
+ process.wait()
89
+
90
+ audio_file += '.mp3'
 
 
 
 
 
 
 
 
 
 
91
  else:
92
  raise ValueError(source_type)
93
 
 
127
  return None
128
  return create_html_youtube_player(youtube_url)
129
 
130
+ def oauth_google():
131
+ return create_html_oauth()
132
 
133
  AUDIO_EXAMPLES = glob.glob('examples/*.*', recursive=True)
134
+ YOUTUBE_EXAMPLES = ["https://youtu.be/5vJBhdjvVcE?si=s3NFG_SlVju0Iklg",
135
+ "https://www.youtube.com/watch?v=vMboypSkj3c",
136
  "https://youtu.be/vRd5KEjX8vw?si=b-qw633ZjaX6Uxy5",
 
 
137
  "https://youtu.be/bnS-HK_lTHA?si=PQLVAab3QHMbv0S3https://youtu.be/zJB0nnOc7bM?si=EA1DN8nHWJcpQWp_",
138
+ "https://youtu.be/7mjQooXt28o?si=qqmMxCxwqBlLPDI2",
139
  "https://youtu.be/mIWYTg55h10?si=WkbtKfL6NlNquvT8"]
140
 
141
  theme = gr.Theme.from_hub("gradio/dracula_revamped")
 
228
  with gr.Column(scale=4):
229
  # Submit button
230
  transcribe_video_button = gr.Button("Transcribe", variant="primary")
231
+ oauth_button = gr.Button("google.com/device", variant="primary")
232
+
233
  with gr.Column(scale=1):
234
  # Transcribe
235
  output_tab2 = gr.HTML(render=True)
 
237
  transcribe_video_button.click(process_video, inputs=youtube_url, outputs=output_tab2)
238
  # Play
239
  play_video_button.click(play_video, inputs=youtube_url, outputs=youtube_player)
240
+ # Authetification
241
+ oauth_button.click(oauth_google, outputs=youtube_player)
242
+
243
+ with gr.Column(scale=1):
244
+ Log(log_file, dark=True, xterm_font_size=12, elem_id='mylog')
245
 
246
  demo.launch(debug=True)