Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from faster_whisper import WhisperModel
|
|
| 6 |
from pytube import YouTube
|
| 7 |
from pytube.exceptions import VideoUnavailable, PytubeError
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def convert_mp4_to_mp3(video_file_path, output_dir):
|
| 11 |
video = VideoFileClip(video_file_path)
|
| 12 |
audio = video.audio
|
|
@@ -16,7 +16,7 @@ def convert_mp4_to_mp3(video_file_path, output_dir):
|
|
| 16 |
video.close()
|
| 17 |
return output_path
|
| 18 |
|
| 19 |
-
#
|
| 20 |
def transcribe_audio(model_size, audio_file):
|
| 21 |
model = WhisperModel(model_size, device="cpu", compute_type="int8")
|
| 22 |
start_time = time.time()
|
|
@@ -40,35 +40,41 @@ def transcribe_audio(model_size, audio_file):
|
|
| 40 |
|
| 41 |
return f"{detected_language}\n\nTranscription:\n{result_text}\n\nElapsed time: {elapsed_time:.2f} seconds"
|
| 42 |
|
| 43 |
-
# YouTube URL
|
| 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 |
-
#
|
| 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:
|
| 64 |
return "Please upload a video file or provide a video URL, but not both."
|
| 65 |
|
| 66 |
save_path = "/tmp"
|
| 67 |
mp3_file_path = convert_mp4_to_mp3(video_file_path, save_path)
|
|
|
|
| 68 |
transcription = transcribe_audio(model_size, mp3_file_path)
|
|
|
|
| 69 |
return transcription
|
| 70 |
|
| 71 |
-
# Gradio
|
| 72 |
iface = gr.Interface(
|
| 73 |
fn=process_video,
|
| 74 |
inputs=[
|
|
|
|
| 6 |
from pytube import YouTube
|
| 7 |
from pytube.exceptions import VideoUnavailable, PytubeError
|
| 8 |
|
| 9 |
+
# λΉλμ€λ₯Ό MP3λ‘ λ³ννλ ν¨μ
|
| 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 |
+
# Whisper λͺ¨λΈμ μ¬μ©νμ¬ MP3 νμΌμ ν
μ€νΈλ‘ λ³ννλ ν¨μ
|
| 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μμ λΉλμ€λ₯Ό λ€μ΄λ‘λνλ ν¨μ
|
| 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, None
|
| 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 |
+
# Gradio μΈν°νμ΄μ€μμ μ¬μ©ν λ©μΈ ν¨μ
|
| 56 |
def process_video(model_size, video_file=None, video_url=None):
|
| 57 |
if video_url and not video_file:
|
| 58 |
+
print(f"Downloading video from URL: {video_url}")
|
| 59 |
video_file_path, error = download_youtube_video(video_url, '/tmp')
|
| 60 |
if error:
|
| 61 |
+
print(f"Error downloading video: {error}")
|
| 62 |
return error
|
| 63 |
+
print(f"Downloaded video to: {video_file_path}")
|
| 64 |
elif video_file and not video_url:
|
| 65 |
video_file_path = video_file.name
|
| 66 |
+
print(f"Using uploaded video file: {video_file_path}")
|
| 67 |
else:
|
| 68 |
return "Please upload a video file or provide a video URL, but not both."
|
| 69 |
|
| 70 |
save_path = "/tmp"
|
| 71 |
mp3_file_path = convert_mp4_to_mp3(video_file_path, save_path)
|
| 72 |
+
print(f"Converted video to MP3: {mp3_file_path}")
|
| 73 |
transcription = transcribe_audio(model_size, mp3_file_path)
|
| 74 |
+
print(f"Transcription complete")
|
| 75 |
return transcription
|
| 76 |
|
| 77 |
+
# Gradio μΈν°νμ΄μ€ μ μ
|
| 78 |
iface = gr.Interface(
|
| 79 |
fn=process_video,
|
| 80 |
inputs=[
|