Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,9 @@ import time
|
|
4 |
from moviepy.editor import VideoFileClip
|
5 |
from faster_whisper import WhisperModel
|
6 |
from pytube import YouTube
|
|
|
7 |
|
8 |
-
#
|
9 |
def convert_mp4_to_mp3(video_file_path, output_dir):
|
10 |
video = VideoFileClip(video_file_path)
|
11 |
audio = video.audio
|
@@ -15,7 +16,7 @@ def convert_mp4_to_mp3(video_file_path, output_dir):
|
|
15 |
video.close()
|
16 |
return output_path
|
17 |
|
18 |
-
# Whisper
|
19 |
def transcribe_audio(model_size, audio_file):
|
20 |
model = WhisperModel(model_size, device="cpu", compute_type="int8")
|
21 |
start_time = time.time()
|
@@ -39,17 +40,24 @@ def transcribe_audio(model_size, audio_file):
|
|
39 |
|
40 |
return f"{detected_language}\n\nTranscription:\n{result_text}\n\nElapsed time: {elapsed_time:.2f} seconds"
|
41 |
|
42 |
-
# YouTube URL
|
43 |
def download_youtube_video(url, output_dir):
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
def process_video(model_size, video_file=None, video_url=None):
|
51 |
if video_url and not video_file:
|
52 |
-
video_file_path = download_youtube_video(video_url, '/tmp')
|
|
|
|
|
53 |
elif video_file and not video_url:
|
54 |
video_file_path = video_file.name
|
55 |
else:
|
@@ -60,7 +68,7 @@ def process_video(model_size, video_file=None, video_url=None):
|
|
60 |
transcription = transcribe_audio(model_size, mp3_file_path)
|
61 |
return transcription
|
62 |
|
63 |
-
# Gradio
|
64 |
iface = gr.Interface(
|
65 |
fn=process_video,
|
66 |
inputs=[
|
|
|
4 |
from moviepy.editor import VideoFileClip
|
5 |
from faster_whisper import WhisperModel
|
6 |
from pytube import YouTube
|
7 |
+
from pytube.exceptions import VideoUnavailable, PytubeError
|
8 |
|
9 |
+
# Conver Video to audio
|
10 |
def convert_mp4_to_mp3(video_file_path, output_dir):
|
11 |
video = VideoFileClip(video_file_path)
|
12 |
audio = video.audio
|
|
|
16 |
video.close()
|
17 |
return output_path
|
18 |
|
19 |
+
# using Whisper Model
|
20 |
def transcribe_audio(model_size, audio_file):
|
21 |
model = WhisperModel(model_size, device="cpu", compute_type="int8")
|
22 |
start_time = time.time()
|
|
|
40 |
|
41 |
return f"{detected_language}\n\nTranscription:\n{result_text}\n\nElapsed time: {elapsed_time:.2f} seconds"
|
42 |
|
43 |
+
# YouTube URL to download video
|
44 |
def download_youtube_video(url, output_dir):
|
45 |
+
try:
|
46 |
+
yt = YouTube(url)
|
47 |
+
stream = yt.streams.filter(file_extension='mp4').first()
|
48 |
+
output_path = stream.download(output_dir)
|
49 |
+
return output_path
|
50 |
+
except VideoUnavailable:
|
51 |
+
return None, "Video unavailable. Please check the URL."
|
52 |
+
except PytubeError as e:
|
53 |
+
return None, f"An error occurred: {e}"
|
54 |
|
55 |
+
# function for process video
|
56 |
def process_video(model_size, video_file=None, video_url=None):
|
57 |
if video_url and not video_file:
|
58 |
+
video_file_path, error = download_youtube_video(video_url, '/tmp')
|
59 |
+
if error:
|
60 |
+
return error
|
61 |
elif video_file and not video_url:
|
62 |
video_file_path = video_file.name
|
63 |
else:
|
|
|
68 |
transcription = transcribe_audio(model_size, mp3_file_path)
|
69 |
return transcription
|
70 |
|
71 |
+
# Gradio interface
|
72 |
iface = gr.Interface(
|
73 |
fn=process_video,
|
74 |
inputs=[
|