artificialguybr commited on
Commit
72f809e
·
verified ·
1 Parent(s): bdd072a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -19
app.py CHANGED
@@ -41,20 +41,28 @@ def download_youtube_audio(url):
41
  }],
42
  'outtmpl': output_path
43
  }
44
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
45
- ydl.download([url])
46
-
47
- if os.path.exists(output_path):
48
- print(f"Audio download completed. File saved at: {output_path}")
49
- print(f"File size: {os.path.getsize(output_path)} bytes")
50
- else:
51
- print(f"Error: File {output_path} not found after download.")
52
-
53
- return output_path
 
 
 
 
54
 
55
  @spaces.GPU(duration=60)
56
  def transcribe_audio(file_path):
57
  print(f"Starting transcription of file: {file_path}")
 
 
 
 
58
  if file_path.endswith(('.mp4', '.avi', '.mov', '.flv')):
59
  print("Video file detected. Extracting audio...")
60
  try:
@@ -65,8 +73,8 @@ def transcribe_audio(file_path):
65
  file_path = audio_path
66
  except Exception as e:
67
  print(f"Error extracting audio from video: {e}")
68
- raise
69
-
70
  output_file = generate_unique_filename('.json')
71
  command = [
72
  "insanely-fast-whisper",
@@ -80,25 +88,33 @@ def transcribe_audio(file_path):
80
  print(f"Executing command: {' '.join(command)}")
81
  try:
82
  result = subprocess.run(command, check=True, capture_output=True, text=True)
 
 
83
  except subprocess.CalledProcessError as e:
84
  print(f"Error running insanely-fast-whisper: {e}")
85
- raise
86
-
 
 
 
 
87
  try:
88
  with open(output_file, "r") as f:
89
  transcription = json.load(f)
90
  except json.JSONDecodeError as e:
91
  print(f"Error decoding JSON: {e}")
92
- raise
93
-
 
 
94
  if "text" in transcription:
95
  result = transcription["text"]
96
  else:
97
  result = " ".join([chunk["text"] for chunk in transcription.get("chunks", [])])
98
-
99
  cleanup_file(file_path)
100
  cleanup_file(output_file)
101
-
102
  return result
103
 
104
  @spaces.GPU(duration=60)
@@ -121,16 +137,23 @@ def process_youtube(url):
121
  return "Please enter a YouTube URL.", None
122
  try:
123
  audio_file = download_youtube_audio(url)
 
 
124
  transcription = transcribe_audio(audio_file)
 
 
125
  return transcription, None
126
  except Exception as e:
127
  return f"Processing error: {str(e)}", None
128
  finally:
129
- cleanup_file(audio_file)
 
130
 
131
  def process_uploaded_video(video_path):
132
  try:
133
  transcription = transcribe_audio(video_path)
 
 
134
  return transcription, None
135
  except Exception as e:
136
  return f"Processing error: {str(e)}", None
 
41
  }],
42
  'outtmpl': output_path
43
  }
44
+ try:
45
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
46
+ ydl.download([url])
47
+
48
+ if os.path.exists(output_path):
49
+ print(f"Audio download completed. File saved at: {output_path}")
50
+ print(f"File size: {os.path.getsize(output_path)} bytes")
51
+ return output_path
52
+ else:
53
+ print(f"Error: File {output_path} not found after download.")
54
+ return None
55
+ except Exception as e:
56
+ print(f"Error downloading YouTube audio: {e}")
57
+ return None
58
 
59
  @spaces.GPU(duration=60)
60
  def transcribe_audio(file_path):
61
  print(f"Starting transcription of file: {file_path}")
62
+ if not os.path.exists(file_path):
63
+ print(f"Error: File {file_path} does not exist.")
64
+ return None
65
+
66
  if file_path.endswith(('.mp4', '.avi', '.mov', '.flv')):
67
  print("Video file detected. Extracting audio...")
68
  try:
 
73
  file_path = audio_path
74
  except Exception as e:
75
  print(f"Error extracting audio from video: {e}")
76
+ return None
77
+
78
  output_file = generate_unique_filename('.json')
79
  command = [
80
  "insanely-fast-whisper",
 
88
  print(f"Executing command: {' '.join(command)}")
89
  try:
90
  result = subprocess.run(command, check=True, capture_output=True, text=True)
91
+ print(f"Standard output: {result.stdout}")
92
+ print(f"Error output: {result.stderr}")
93
  except subprocess.CalledProcessError as e:
94
  print(f"Error running insanely-fast-whisper: {e}")
95
+ print(f"Standard output: {e.stdout}")
96
+ print(f"Error output: {e.stderr}")
97
+ cleanup_file(file_path)
98
+ cleanup_file(output_file)
99
+ return None
100
+
101
  try:
102
  with open(output_file, "r") as f:
103
  transcription = json.load(f)
104
  except json.JSONDecodeError as e:
105
  print(f"Error decoding JSON: {e}")
106
+ cleanup_file(file_path)
107
+ cleanup_file(output_file)
108
+ return None
109
+
110
  if "text" in transcription:
111
  result = transcription["text"]
112
  else:
113
  result = " ".join([chunk["text"] for chunk in transcription.get("chunks", [])])
114
+
115
  cleanup_file(file_path)
116
  cleanup_file(output_file)
117
+
118
  return result
119
 
120
  @spaces.GPU(duration=60)
 
137
  return "Please enter a YouTube URL.", None
138
  try:
139
  audio_file = download_youtube_audio(url)
140
+ if audio_file is None:
141
+ return "Error downloading YouTube audio.", None
142
  transcription = transcribe_audio(audio_file)
143
+ if transcription is None:
144
+ return "Error transcribing audio.", None
145
  return transcription, None
146
  except Exception as e:
147
  return f"Processing error: {str(e)}", None
148
  finally:
149
+ if audio_file:
150
+ cleanup_file(audio_file)
151
 
152
  def process_uploaded_video(video_path):
153
  try:
154
  transcription = transcribe_audio(video_path)
155
+ if transcription is None:
156
+ return "Error transcribing video.", None
157
  return transcription, None
158
  except Exception as e:
159
  return f"Processing error: {str(e)}", None