jpjp9292 commited on
Commit
dfa9b1d
β€’
1 Parent(s): 0ebef1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -31
app.py CHANGED
@@ -3,8 +3,6 @@ import gradio as gr
3
  import time
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
  # λΉ„λ””μ˜€λ₯Ό MP3둜 λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜
10
  def convert_mp4_to_mp3(video_file_path, output_dir):
@@ -40,38 +38,19 @@ 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, 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 μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
@@ -79,12 +58,11 @@ iface = gr.Interface(
79
  fn=process_video,
80
  inputs=[
81
  gr.Dropdown(["tiny", "base", "small", "medium", "large"], label="Model Size"),
82
- gr.File(label="Upload Video File"),
83
- gr.Textbox(label="Video URL")
84
  ],
85
  outputs="text",
86
  title="Video to Text Converter using Whisper",
87
- description="Upload a video file or provide a video URL, select the Whisper model size, and get the transcribed text.",
88
  live=True
89
  )
90
 
 
3
  import time
4
  from moviepy.editor import VideoFileClip
5
  from faster_whisper import WhisperModel
 
 
6
 
7
  # λΉ„λ””μ˜€λ₯Ό MP3둜 λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜
8
  def convert_mp4_to_mp3(video_file_path, output_dir):
 
38
 
39
  return f"{detected_language}\n\nTranscription:\n{result_text}\n\nElapsed time: {elapsed_time:.2f} seconds"
40
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Gradio μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μ‚¬μš©ν•  메인 ν•¨μˆ˜
42
+ def process_video(model_size, video_file=None):
43
+ if not video_file:
44
+ return "Please upload a video file."
45
+
46
+ video_file_path = video_file.name
47
+ print(f"Using uploaded video file: {video_file_path}")
 
 
 
 
 
 
 
48
 
49
  save_path = "/tmp"
50
  mp3_file_path = convert_mp4_to_mp3(video_file_path, save_path)
51
  print(f"Converted video to MP3: {mp3_file_path}")
52
  transcription = transcribe_audio(model_size, mp3_file_path)
53
+ print("Transcription complete")
54
  return transcription
55
 
56
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
 
58
  fn=process_video,
59
  inputs=[
60
  gr.Dropdown(["tiny", "base", "small", "medium", "large"], label="Model Size"),
61
+ gr.File(label="Upload Video File")
 
62
  ],
63
  outputs="text",
64
  title="Video to Text Converter using Whisper",
65
+ description="Upload a video file, select the Whisper model size, and get the transcribed text.",
66
  live=True
67
  )
68