bluenevus commited on
Commit
b8cd6c2
·
verified ·
1 Parent(s): d398f2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -53
app.py CHANGED
@@ -9,7 +9,7 @@ from dash import Dash, dcc, html, Input, Output, State, callback, callback_conte
9
  import dash_bootstrap_components as dbc
10
  from pydub import AudioSegment
11
  import requests
12
- from pytube import YouTube
13
  import mimetypes
14
  import urllib.parse
15
 
@@ -84,36 +84,22 @@ def process_media(file_path, is_url=False):
84
  try:
85
  if is_url:
86
  logger.info(f"Processing URL: {file_path}")
87
- if 'youtube.com' in file_path or 'youtu.be' in file_path:
88
- try:
89
- yt = YouTube(file_path)
90
- stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
91
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
92
- stream.download(output_path=os.path.dirname(temp_file.name), filename=os.path.basename(temp_file.name))
93
- logger.info(f"YouTube video downloaded: {temp_file.name}")
94
- except Exception as e:
95
- logger.error(f"Error downloading YouTube video: {str(e)}")
96
- return f"Error downloading YouTube video: {str(e)}", False
97
- else:
98
- try:
99
- response = requests.get(file_path)
100
- response.raise_for_status()
101
- content_type = response.headers.get('content-type', '')
102
- logger.info(f"URL content type: {content_type}")
103
-
104
- # Determine file extension from URL or content type
105
- url_path = urllib.parse.urlparse(file_path).path
106
- ext = os.path.splitext(url_path)[1]
107
- if not ext:
108
- ext = mimetypes.guess_extension(content_type) or ''
109
-
110
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=ext)
111
- temp_file.write(response.content)
112
- temp_file.close()
113
- logger.info(f"URL content downloaded: {temp_file.name}")
114
- except Exception as e:
115
- logger.error(f"Error downloading URL content: {str(e)}")
116
- return f"Error downloading URL content: {str(e)}", False
117
  else:
118
  logger.info("Processing uploaded file")
119
  temp_file = tempfile.NamedTemporaryFile(delete=False)
@@ -121,24 +107,24 @@ def process_media(file_path, is_url=False):
121
  temp_file.close()
122
  logger.info(f"Uploaded file saved: {temp_file.name}")
123
 
124
- file_extension = os.path.splitext(temp_file.name)[1].lower()
125
- logger.info(f"Detected file extension: {file_extension}")
126
-
127
- if file_extension in VIDEO_FORMATS:
128
- logger.info("Processing video file")
129
- video = VideoFileClip(temp_file.name)
130
- audio = video.audio
131
- wav_path = temp_file.name + ".wav"
132
- audio.write_audiofile(wav_path)
133
- video.close()
134
- elif file_extension in AUDIO_FORMATS or not file_extension:
135
- logger.info("Processing audio file")
136
- audio = AudioSegment.from_file(temp_file.name, format=file_extension[1:] if file_extension else None)
137
- wav_path = temp_file.name + ".wav"
138
- audio.export(wav_path, format="wav")
139
- else:
140
- logger.error(f"Unsupported file format: {file_extension}")
141
- return f"Unsupported file format: {file_extension}. Please upload a supported audio or video file.", False
142
 
143
  logger.info(f"Audio extracted to WAV: {wav_path}")
144
 
@@ -184,15 +170,14 @@ def update_output(contents, n_clicks, filename, url):
184
  if not ctx.triggered:
185
  return "No file uploaded or URL processed.", "", "", True
186
 
187
- trigger_id = ctx.triggered[0]['prop_id'].split('.')[0]
 
188
 
189
  if contents is not None:
190
- # Process file upload
191
  content_type, content_string = contents.split(',')
192
  decoded = base64.b64decode(content_string)
193
  status_message, success = process_media(decoded)
194
  elif url:
195
- # Process URL
196
  status_message, success = process_media(url, is_url=True)
197
  else:
198
  return "No file uploaded or URL processed.", "", "", True
@@ -201,7 +186,7 @@ def update_output(contents, n_clicks, filename, url):
201
  preview = transcription_text[:1000] + "..." if len(transcription_text) > 1000 else transcription_text
202
  return f"Media processed successfully.", status_message, preview, False
203
  else:
204
- return "Processing failed.", status_message, "", True
205
 
206
  @app.callback(
207
  Output("download-transcription", "data"),
 
9
  import dash_bootstrap_components as dbc
10
  from pydub import AudioSegment
11
  import requests
12
+ import yt_dlp
13
  import mimetypes
14
  import urllib.parse
15
 
 
84
  try:
85
  if is_url:
86
  logger.info(f"Processing URL: {file_path}")
87
+ try:
88
+ ydl_opts = {
89
+ 'format': 'bestaudio/best',
90
+ 'postprocessors': [{
91
+ 'key': 'FFmpegExtractAudio',
92
+ 'preferredcodec': 'wav',
93
+ }],
94
+ 'outtmpl': '%(id)s.%(ext)s',
95
+ }
96
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
97
+ info = ydl.extract_info(file_path, download=True)
98
+ wav_path = f"{info['id']}.wav"
99
+ logger.info(f"Audio downloaded: {wav_path}")
100
+ except Exception as e:
101
+ logger.error(f"Error downloading audio from URL: {str(e)}")
102
+ return f"Error downloading audio from URL: {str(e)}", False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  else:
104
  logger.info("Processing uploaded file")
105
  temp_file = tempfile.NamedTemporaryFile(delete=False)
 
107
  temp_file.close()
108
  logger.info(f"Uploaded file saved: {temp_file.name}")
109
 
110
+ file_extension = os.path.splitext(temp_file.name)[1].lower()
111
+ logger.info(f"Detected file extension: {file_extension}")
112
+
113
+ if file_extension in VIDEO_FORMATS:
114
+ logger.info("Processing video file")
115
+ video = VideoFileClip(temp_file.name)
116
+ audio = video.audio
117
+ wav_path = temp_file.name + ".wav"
118
+ audio.write_audiofile(wav_path)
119
+ video.close()
120
+ elif file_extension in AUDIO_FORMATS:
121
+ logger.info("Processing audio file")
122
+ audio = AudioSegment.from_file(temp_file.name, format=file_extension[1:])
123
+ wav_path = temp_file.name + ".wav"
124
+ audio.export(wav_path, format="wav")
125
+ else:
126
+ logger.error(f"Unsupported file format: {file_extension}")
127
+ return f"Unsupported file format: {file_extension}. Please upload a supported audio or video file.", False
128
 
129
  logger.info(f"Audio extracted to WAV: {wav_path}")
130
 
 
170
  if not ctx.triggered:
171
  return "No file uploaded or URL processed.", "", "", True
172
 
173
+ # Clear the preview pane
174
+ transcription_preview = ""
175
 
176
  if contents is not None:
 
177
  content_type, content_string = contents.split(',')
178
  decoded = base64.b64decode(content_string)
179
  status_message, success = process_media(decoded)
180
  elif url:
 
181
  status_message, success = process_media(url, is_url=True)
182
  else:
183
  return "No file uploaded or URL processed.", "", "", True
 
186
  preview = transcription_text[:1000] + "..." if len(transcription_text) > 1000 else transcription_text
187
  return f"Media processed successfully.", status_message, preview, False
188
  else:
189
+ return "Processing failed.", status_message, transcription_preview, True
190
 
191
  @app.callback(
192
  Output("download-transcription", "data"),