jpjp9292 commited on
Commit
83ebf54
Β·
verified Β·
1 Parent(s): 9e780c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
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
- # 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,7 +16,7 @@ def convert_mp4_to_mp3(video_file_path, output_dir):
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,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 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:
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 interface
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=[