Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import os
|
|
5 |
from dotenv import load_dotenv
|
6 |
from pydub import AudioSegment
|
7 |
import tempfile
|
|
|
8 |
|
9 |
# Load environment variables
|
10 |
load_dotenv()
|
@@ -26,16 +27,22 @@ def isolate_audio(audio_file):
|
|
26 |
if not is_valid_audio(audio_file):
|
27 |
return None, "The uploaded file is not a valid audio file. Please ensure it is a playable audio file."
|
28 |
|
|
|
|
|
|
|
|
|
29 |
try:
|
|
|
|
|
|
|
30 |
# Perform audio isolation
|
31 |
-
with open(
|
32 |
isolated_audio_iterator = client.audio_isolation.audio_isolation(audio=file)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
output_file_path = temp_file.name
|
39 |
|
40 |
return output_file_path, "Audio isolation completed successfully."
|
41 |
except ApiError as e:
|
@@ -44,6 +51,9 @@ def isolate_audio(audio_file):
|
|
44 |
except Exception as e:
|
45 |
error_message = f"An unexpected error occurred: {str(e)}"
|
46 |
return None, error_message
|
|
|
|
|
|
|
47 |
|
48 |
# Create Gradio interface
|
49 |
iface = gr.Interface(
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
from pydub import AudioSegment
|
7 |
import tempfile
|
8 |
+
import shutil
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
|
|
27 |
if not is_valid_audio(audio_file):
|
28 |
return None, "The uploaded file is not a valid audio file. Please ensure it is a playable audio file."
|
29 |
|
30 |
+
temp_dir = tempfile.mkdtemp()
|
31 |
+
input_file_path = os.path.join(temp_dir, "input_audio.mp3")
|
32 |
+
output_file_path = os.path.join(temp_dir, "isolated_audio.mp3")
|
33 |
+
|
34 |
try:
|
35 |
+
# Copy the input file to our temporary directory
|
36 |
+
shutil.copy2(audio_file, input_file_path)
|
37 |
+
|
38 |
# Perform audio isolation
|
39 |
+
with open(input_file_path, "rb") as file:
|
40 |
isolated_audio_iterator = client.audio_isolation.audio_isolation(audio=file)
|
41 |
|
42 |
+
# Save the isolated audio to the output file
|
43 |
+
with open(output_file_path, "wb") as output_file:
|
44 |
+
for chunk in isolated_audio_iterator:
|
45 |
+
output_file.write(chunk)
|
|
|
46 |
|
47 |
return output_file_path, "Audio isolation completed successfully."
|
48 |
except ApiError as e:
|
|
|
51 |
except Exception as e:
|
52 |
error_message = f"An unexpected error occurred: {str(e)}"
|
53 |
return None, error_message
|
54 |
+
finally:
|
55 |
+
# Clean up temporary files
|
56 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
57 |
|
58 |
# Create Gradio interface
|
59 |
iface = gr.Interface(
|