Update app.py
Browse files
app.py
CHANGED
@@ -14,15 +14,23 @@ headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
14 |
|
15 |
def preprocess_audio_with_ffmpeg(input_path, output_path):
|
16 |
"""Preprocess audio using FFmpeg."""
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def save_audio_to_tempfile(audio_data, sample_rate):
|
28 |
"""Save raw audio data to a temporary WAV file."""
|
@@ -53,7 +61,8 @@ def query(audio_input):
|
|
53 |
# Preprocess the audio using FFmpeg
|
54 |
with NamedTemporaryFile(suffix=".wav", delete=False) as processed_temp_file:
|
55 |
processed_audio_path = processed_temp_file.name
|
56 |
-
preprocess_audio_with_ffmpeg(audio_path, processed_audio_path)
|
|
|
57 |
print(f"Processed audio saved at: {processed_audio_path}")
|
58 |
|
59 |
# Read the processed audio file
|
|
|
14 |
|
15 |
def preprocess_audio_with_ffmpeg(input_path, output_path):
|
16 |
"""Preprocess audio using FFmpeg."""
|
17 |
+
try:
|
18 |
+
command = [
|
19 |
+
"ffmpeg",
|
20 |
+
"-i", input_path, # Input file
|
21 |
+
"-ar", "16000", # Resample to 16 kHz
|
22 |
+
"-ac", "1", # Convert to mono
|
23 |
+
"-y", # Overwrite output file if it exists
|
24 |
+
output_path # Output file
|
25 |
+
]
|
26 |
+
subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
27 |
+
return True
|
28 |
+
except subprocess.CalledProcessError as e:
|
29 |
+
print(f"FFmpeg error: {e.stderr.decode('utf-8')}")
|
30 |
+
return False
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error during FFmpeg processing: {str(e)}")
|
33 |
+
return False
|
34 |
|
35 |
def save_audio_to_tempfile(audio_data, sample_rate):
|
36 |
"""Save raw audio data to a temporary WAV file."""
|
|
|
61 |
# Preprocess the audio using FFmpeg
|
62 |
with NamedTemporaryFile(suffix=".wav", delete=False) as processed_temp_file:
|
63 |
processed_audio_path = processed_temp_file.name
|
64 |
+
if not preprocess_audio_with_ffmpeg(audio_path, processed_audio_path):
|
65 |
+
return "Error: Failed to preprocess audio. Please check the file format.", None, None
|
66 |
print(f"Processed audio saved at: {processed_audio_path}")
|
67 |
|
68 |
# Read the processed audio file
|