Update app.py
Browse files
app.py
CHANGED
@@ -24,6 +24,11 @@ except ImportError as e:
|
|
24 |
logger.error("Please ensure moviepy is installed correctly")
|
25 |
raise
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
# Initialize the Dash app
|
28 |
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
29 |
|
@@ -72,38 +77,46 @@ app.layout = dbc.Container([
|
|
72 |
|
73 |
def process_media(file_path, is_url=False):
|
74 |
global generated_file, transcription_text
|
75 |
-
|
|
|
76 |
try:
|
77 |
if is_url:
|
78 |
if 'youtube.com' in file_path or 'youtu.be' in file_path:
|
79 |
yt = YouTube(file_path)
|
80 |
-
stream = yt.streams.filter(
|
81 |
-
|
82 |
-
stream.download(output_path=os.path.dirname(
|
83 |
else:
|
84 |
response = requests.get(file_path)
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
else:
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
|
93 |
-
file_extension = os.path.splitext(
|
94 |
|
95 |
-
if file_extension in
|
96 |
-
video = VideoFileClip(
|
97 |
audio = video.audio
|
98 |
-
wav_path =
|
99 |
audio.write_audiofile(wav_path)
|
100 |
video.close()
|
101 |
-
elif file_extension in
|
102 |
-
audio = AudioSegment.from_file(
|
103 |
-
wav_path =
|
104 |
audio.export(wav_path, format="wav")
|
105 |
else:
|
106 |
-
return "Unsupported file format. Please upload
|
107 |
|
108 |
with open(wav_path, "rb") as audio_file:
|
109 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
@@ -126,11 +139,11 @@ def process_media(file_path, is_url=False):
|
|
126 |
logger.error(f"Error during processing: {str(e)}")
|
127 |
return f"An error occurred: {str(e)}", False
|
128 |
finally:
|
129 |
-
if
|
130 |
-
os.unlink(
|
131 |
-
if
|
132 |
os.unlink(wav_path)
|
133 |
-
|
134 |
@app.callback(
|
135 |
[Output('output-media-upload', 'children'),
|
136 |
Output('transcription-status', 'children'),
|
|
|
24 |
logger.error("Please ensure moviepy is installed correctly")
|
25 |
raise
|
26 |
|
27 |
+
# Supported file formats
|
28 |
+
AUDIO_FORMATS = ['.wav', '.mp3', '.ogg', '.flac', '.aac', '.m4a', '.wma']
|
29 |
+
VIDEO_FORMATS = ['.mp4', '.avi', '.mov', '.flv', '.wmv', '.mkv', '.webm']
|
30 |
+
SUPPORTED_FORMATS = AUDIO_FORMATS + VIDEO_FORMATS
|
31 |
+
|
32 |
# Initialize the Dash app
|
33 |
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
34 |
|
|
|
77 |
|
78 |
def process_media(file_path, is_url=False):
|
79 |
global generated_file, transcription_text
|
80 |
+
temp_file = None
|
81 |
+
wav_path = None
|
82 |
try:
|
83 |
if is_url:
|
84 |
if 'youtube.com' in file_path or 'youtu.be' in file_path:
|
85 |
yt = YouTube(file_path)
|
86 |
+
stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
|
87 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
88 |
+
stream.download(output_path=os.path.dirname(temp_file.name), filename=os.path.basename(temp_file.name))
|
89 |
else:
|
90 |
response = requests.get(file_path)
|
91 |
+
content_type = response.headers.get('content-type', '')
|
92 |
+
if 'audio' in content_type:
|
93 |
+
suffix = '.mp3'
|
94 |
+
elif 'video' in content_type:
|
95 |
+
suffix = '.mp4'
|
96 |
+
else:
|
97 |
+
suffix = ''
|
98 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
99 |
+
temp_file.write(response.content)
|
100 |
+
temp_file.close()
|
101 |
else:
|
102 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
103 |
+
temp_file.write(file_path)
|
104 |
+
temp_file.close()
|
105 |
|
106 |
+
file_extension = os.path.splitext(temp_file.name)[1].lower()
|
107 |
|
108 |
+
if file_extension in VIDEO_FORMATS:
|
109 |
+
video = VideoFileClip(temp_file.name)
|
110 |
audio = video.audio
|
111 |
+
wav_path = temp_file.name + ".wav"
|
112 |
audio.write_audiofile(wav_path)
|
113 |
video.close()
|
114 |
+
elif file_extension in AUDIO_FORMATS:
|
115 |
+
audio = AudioSegment.from_file(temp_file.name)
|
116 |
+
wav_path = temp_file.name + ".wav"
|
117 |
audio.export(wav_path, format="wav")
|
118 |
else:
|
119 |
+
return f"Unsupported file format: {file_extension}. Please upload a supported audio or video file.", False
|
120 |
|
121 |
with open(wav_path, "rb") as audio_file:
|
122 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
|
|
139 |
logger.error(f"Error during processing: {str(e)}")
|
140 |
return f"An error occurred: {str(e)}", False
|
141 |
finally:
|
142 |
+
if temp_file and os.path.exists(temp_file.name):
|
143 |
+
os.unlink(temp_file.name)
|
144 |
+
if wav_path and os.path.exists(wav_path):
|
145 |
os.unlink(wav_path)
|
146 |
+
|
147 |
@app.callback(
|
148 |
[Output('output-media-upload', 'children'),
|
149 |
Output('transcription-status', 'children'),
|