Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 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 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 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 |
-
|
|
|
|
| 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,
|
| 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"),
|