Update app.py
Browse files
app.py
CHANGED
|
@@ -10,27 +10,16 @@ from googletrans import Translator
|
|
| 10 |
pipe = pipeline(model="sanchit-gandhi/whisper-small-hi")
|
| 11 |
|
| 12 |
def download_from_youtube(url):
|
| 13 |
-
|
| 14 |
-
Downloads the video from the given YouTube URL and returns the path to the audio file.
|
| 15 |
-
"""
|
| 16 |
-
streams = YouTube(url).streams.filter(only_audio=True, file_extension='mp4')
|
| 17 |
fpath = streams.first().download()
|
| 18 |
return fpath
|
| 19 |
|
| 20 |
def get_timestamp(seconds):
|
| 21 |
-
"""
|
| 22 |
-
Creates %M:%S timestamp from seconds.
|
| 23 |
-
"""
|
| 24 |
minutes = int(seconds / 60)
|
| 25 |
seconds = int(seconds % 60)
|
| 26 |
-
return f"{str(minutes).zfill(2)}:{str(seconds).zfill(2)}"
|
| 27 |
|
| 28 |
def create_segments(audio_fpath, seconds_max):
|
| 29 |
-
"""
|
| 30 |
-
Divides the audio file into 30s segments and returns the paths to the segments and the start times of the segments.
|
| 31 |
-
:param audio_fpath: Path to the audio file.
|
| 32 |
-
:param seconds_max: Maximum number of seconds to consider. If the audio file is longer than this, it will be truncated.
|
| 33 |
-
"""
|
| 34 |
if not os.path.exists("segmented_audios"):
|
| 35 |
os.makedirs("segmented_audios")
|
| 36 |
|
|
@@ -68,10 +57,6 @@ def create_segments(audio_fpath, seconds_max):
|
|
| 68 |
return segment_paths, segment_start_times
|
| 69 |
|
| 70 |
def get_translation(text):
|
| 71 |
-
"""
|
| 72 |
-
Translates the given Swedish text to English.
|
| 73 |
-
"""
|
| 74 |
-
# TODO: Make API call to Google Translate to get English translation
|
| 75 |
#translator = Translator()
|
| 76 |
#result = translator.translate(text)
|
| 77 |
#result = translator.translate(text, src='fi', dest='fr')
|
|
@@ -80,13 +65,6 @@ def get_translation(text):
|
|
| 80 |
return "Under Development..."
|
| 81 |
|
| 82 |
def transcribe(audio, url, seconds_max):
|
| 83 |
-
"""
|
| 84 |
-
Transcribes a YouTube video if a url is specified and returns the transcription.
|
| 85 |
-
If not url is specified, it transcribes the audio file as passed by Gradio.
|
| 86 |
-
:param audio: Audio file as passed by Gradio. Only used if no url is specified.
|
| 87 |
-
:param url: YouTube URL to transcribe.
|
| 88 |
-
:param seconds_max: Maximum number of seconds to consider. If the audio file is longer than this, it will be truncated.
|
| 89 |
-
"""
|
| 90 |
if url:
|
| 91 |
fpath = download_from_youtube(url)
|
| 92 |
segment_paths, segment_start_times = create_segments(fpath, seconds_max)
|
|
|
|
| 10 |
pipe = pipeline(model="sanchit-gandhi/whisper-small-hi")
|
| 11 |
|
| 12 |
def download_from_youtube(url):
|
| 13 |
+
streams = YouTube(url).streams.filter(only_audio=True, file_extension='mp4') #Downloads the video from the given YouTube URL and returns the path to the audio file.
|
|
|
|
|
|
|
|
|
|
| 14 |
fpath = streams.first().download()
|
| 15 |
return fpath
|
| 16 |
|
| 17 |
def get_timestamp(seconds):
|
|
|
|
|
|
|
|
|
|
| 18 |
minutes = int(seconds / 60)
|
| 19 |
seconds = int(seconds % 60)
|
| 20 |
+
return f"{str(minutes).zfill(2)}:{str(seconds).zfill(2)}" #Creates %M:%S timestamp from seconds.
|
| 21 |
|
| 22 |
def create_segments(audio_fpath, seconds_max):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
if not os.path.exists("segmented_audios"):
|
| 24 |
os.makedirs("segmented_audios")
|
| 25 |
|
|
|
|
| 57 |
return segment_paths, segment_start_times
|
| 58 |
|
| 59 |
def get_translation(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
#translator = Translator()
|
| 61 |
#result = translator.translate(text)
|
| 62 |
#result = translator.translate(text, src='fi', dest='fr')
|
|
|
|
| 65 |
return "Under Development..."
|
| 66 |
|
| 67 |
def transcribe(audio, url, seconds_max):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if url:
|
| 69 |
fpath = download_from_youtube(url)
|
| 70 |
segment_paths, segment_start_times = create_segments(fpath, seconds_max)
|