EdgarDataScientist commited on
Commit
105a910
·
verified ·
1 Parent(s): 5ca9307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -2,11 +2,10 @@ import gradio as gr
2
  from moviepy.editor import VideoFileClip
3
  from speechbrain.pretrained import EncoderClassifier
4
  import torchaudio
5
- from pytubefix import YouTube
6
- from pytubefix.cli import on_progress
7
  import requests
8
  import os
9
  import torch
 
10
 
11
  CLASSIFIER = "Jzuluaga/accent-id-commonaccent_xlsr-en-english"
12
 
@@ -15,15 +14,20 @@ def get_default_device():
15
  return torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
17
  def download_video(url):
18
- """Download video from YouTube or direct MP4 URL using pytubefix."""
19
  try:
20
  if "youtube.com" in url or "youtu.be" in url:
21
- yt = YouTube(url, on_progress_callback=on_progress)
22
- stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
23
- if not stream:
24
- raise ValueError("No suitable video stream found.")
25
- video_path = stream.download()
26
- return video_path
 
 
 
 
 
27
  else:
28
  # Direct MP4 file download
29
  local_filename = "temp_video.mp4"
@@ -70,20 +74,19 @@ def process_video(url):
70
  except Exception as e:
71
  return f"Error: {e}", ""
72
  finally:
73
- # Clean up temporary files
74
  for f in [video_path, audio_path]:
75
  if f and os.path.exists(f):
76
  os.remove(f)
77
 
78
  iface = gr.Interface(
79
  fn=process_video,
80
- inputs=gr.Textbox(label="Enter Public Video URL (YouTube, Loom, direct MP4)"),
81
  outputs=[
82
  gr.Textbox(label="Detected Accent"),
83
  gr.Textbox(label="Confidence Score")
84
  ],
85
  title="English Accent Classifier",
86
- description="Paste a public video URL to detect the English accent and confidence score."
87
  )
88
 
89
  if __name__ == "__main__":
 
2
  from moviepy.editor import VideoFileClip
3
  from speechbrain.pretrained import EncoderClassifier
4
  import torchaudio
 
 
5
  import requests
6
  import os
7
  import torch
8
+ import yt_dlp
9
 
10
  CLASSIFIER = "Jzuluaga/accent-id-commonaccent_xlsr-en-english"
11
 
 
14
  return torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
 
16
  def download_video(url):
17
+ """Download video from YouTube or direct MP4 URL using yt_dlp or requests."""
18
  try:
19
  if "youtube.com" in url or "youtu.be" in url:
20
+ output_path = "temp_video.%(ext)s"
21
+ ydl_opts = {
22
+ 'format': 'best[ext=mp4]/best',
23
+ 'outtmpl': output_path,
24
+ 'quiet': True,
25
+ 'noplaylist': True,
26
+ }
27
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
28
+ info_dict = ydl.extract_info(url, download=True)
29
+ downloaded_path = output_path.replace("%(ext)s", info_dict['ext'])
30
+ return downloaded_path
31
  else:
32
  # Direct MP4 file download
33
  local_filename = "temp_video.mp4"
 
74
  except Exception as e:
75
  return f"Error: {e}", ""
76
  finally:
 
77
  for f in [video_path, audio_path]:
78
  if f and os.path.exists(f):
79
  os.remove(f)
80
 
81
  iface = gr.Interface(
82
  fn=process_video,
83
+ inputs=gr.Textbox(label="Enter Public Video URL (YouTube or direct MP4 link)"),
84
  outputs=[
85
  gr.Textbox(label="Detected Accent"),
86
  gr.Textbox(label="Confidence Score")
87
  ],
88
  title="English Accent Classifier",
89
+ description="Paste a public video URL (YouTube or MP4) to detect the English accent and confidence score."
90
  )
91
 
92
  if __name__ == "__main__":