mimbres commited on
Commit
576e40c
·
verified ·
1 Parent(s): 3c67cd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -45
app.py CHANGED
@@ -18,8 +18,6 @@ from gradio_log import Log
18
 
19
  # gradio_log
20
  log_file = 'amt/log.txt'
21
- with open(log_file, 'w'):
22
- pass # delete
23
 
24
  # @title Load Checkpoint
25
  model_name = 'YPTF.MoE+Multi (noPS)' # @param ["YMT3+", "YPTF+Single (noPS)", "YPTF+Multi (PS)", "YPTF.MoE+Multi (noPS)", "YPTF.MoE+Multi (PS)"]
@@ -60,49 +58,53 @@ model = load_model_checkpoint(args=args)
60
 
61
  def prepare_media(source_path_or_url: os.PathLike,
62
  source_type: Literal['audio_filepath', 'youtube_url'],
63
- delete_video: bool = True) -> Dict:
 
64
  """prepare media from source path or youtube, and return audio info"""
65
  # Get audio_file
66
  if source_type == 'audio_filepath':
67
  audio_file = source_path_or_url
68
  elif source_type == 'youtube_url':
69
- # Download from youtube
 
 
 
 
 
 
 
 
 
 
70
  try:
71
- # Try PyTube first
72
- yt = YouTube(source_path_or_url)
73
- audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
74
- mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
75
- audio_file = mp4_file[:-3] + 'mp3'
76
- subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
77
- os.remove(mp4_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  except Exception as e:
79
- try:
80
- with open(log_file, 'w') as lf:
81
- # Try alternative
82
- print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
83
- audio_file = './downloaded/yt_audio'
84
- # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
85
- # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
86
- # '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
87
- # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
88
- # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
89
- # '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
90
- process = subprocess.Popen(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
91
- '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
92
- '--force-overwrites', '--username', 'oauth2', '--password', '',
93
- '-v'],
94
- stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
95
-
96
- for line in iter(process.stdout.readline, ''):
97
- lf.write(line)
98
- lf.flush()
99
- process.stdout.close()
100
- process.wait()
101
-
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
 
@@ -140,8 +142,11 @@ def play_video(youtube_url):
140
  return None
141
  return create_html_youtube_player(youtube_url)
142
 
143
- def display_log():
144
- Log(log_file, tail=2, dark=True, xterm_font_size=12, elem_id='mylog') # gradio-log
 
 
 
145
 
146
 
147
  AUDIO_EXAMPLES = glob.glob('examples/*.*', recursive=True)
@@ -256,9 +261,7 @@ with gr.Blocks(theme=theme, css=css) as demo:
256
  transcribe_video_button.click(process_video, inputs=youtube_url, outputs=output_tab2)
257
  # Play
258
  play_video_button.click(play_video, inputs=youtube_url, outputs=youtube_player)
259
- display_log()
260
- # with gr.Column(scale=1):
261
- # # Logger
262
- # transcribe_video_button.click(display_log, inputs=None, outputs=None)
263
-
264
  demo.launch(debug=True)
 
18
 
19
  # gradio_log
20
  log_file = 'amt/log.txt'
 
 
21
 
22
  # @title Load Checkpoint
23
  model_name = 'YPTF.MoE+Multi (noPS)' # @param ["YMT3+", "YPTF+Single (noPS)", "YPTF+Multi (PS)", "YPTF.MoE+Multi (noPS)", "YPTF.MoE+Multi (PS)"]
 
58
 
59
  def prepare_media(source_path_or_url: os.PathLike,
60
  source_type: Literal['audio_filepath', 'youtube_url'],
61
+ delete_video: bool = True,
62
+ simulate = False) -> Dict:
63
  """prepare media from source path or youtube, and return audio info"""
64
  # Get audio_file
65
  if source_type == 'audio_filepath':
66
  audio_file = source_path_or_url
67
  elif source_type == 'youtube_url':
68
+ # # Download from youtube
69
+ # try:
70
+ # # Try PyTube first
71
+ # yt = YouTube(source_path_or_url)
72
+ # audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
73
+ # mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
74
+ # audio_file = mp4_file[:-3] + 'mp3'
75
+ # subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
76
+ # os.remove(mp4_file)
77
+ # except Exception as e:
78
+ # try:
79
  try:
80
+ with open(log_file, 'w') as lf:
81
+ # Try alternative
82
+ print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
83
+ audio_file = './downloaded/yt_audio'
84
+ # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
85
+ # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
86
+ # '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
87
+ # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
88
+ # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
89
+ # '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
90
+ command = ['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
91
+ '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
92
+ '--force-overwrites', '--username', 'oauth2', '--password', '', '-v']
93
+ if simulate:
94
+ command = command + ['-s']
95
+ process = subprocess.Popen(command,
96
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
97
+
98
+ for line in iter(process.stdout.readline, ''):
99
+ lf.write(line)
100
+ lf.flush()
101
+ process.stdout.close()
102
+ process.wait()
103
+
104
+ audio_file += '.mp3'
105
  except Exception as e:
106
+ print(f"Alternative downloader failed, error: {e}. Please try again later!")
107
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  else:
109
  raise ValueError(source_type)
110
 
 
142
  return None
143
  return create_html_youtube_player(youtube_url)
144
 
145
+ def init_youtube_account():
146
+ prepare_media(""https://www.youtube.com/watch?v=vMboypSkj3c", 'youtube_url', simulate=True)
147
+
148
+ # def display_log():
149
+ # Log(log_file, tail=2, dark=True, xterm_font_size=12, elem_id='mylog') # gradio-log
150
 
151
 
152
  AUDIO_EXAMPLES = glob.glob('examples/*.*', recursive=True)
 
261
  transcribe_video_button.click(process_video, inputs=youtube_url, outputs=output_tab2)
262
  # Play
263
  play_video_button.click(play_video, inputs=youtube_url, outputs=youtube_player)
264
+ with gr.Colunmn(scale=1):
265
+ init_youtube_account()
266
+ Log(log_file, tail=2, dark=True, xterm_font_size=12, elem_id='mylog')
 
 
267
  demo.launch(debug=True)