Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ from elevenlabs.client import ElevenLabs
|
|
3 |
from elevenlabs.core.api_error import ApiError
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
|
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
@@ -10,19 +12,30 @@ load_dotenv()
|
|
10 |
# Initialize the client with your API key
|
11 |
client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def isolate_audio(audio_file):
|
14 |
if audio_file is None:
|
15 |
return None, "Please upload an audio file."
|
16 |
|
|
|
|
|
|
|
17 |
try:
|
18 |
# Perform audio isolation
|
19 |
-
|
|
|
20 |
|
21 |
# Save the isolated audio to a temporary file
|
22 |
-
|
23 |
-
with open(output_file_path, "wb") as output_file:
|
24 |
for chunk in isolated_audio_iterator:
|
25 |
-
|
|
|
26 |
|
27 |
return output_file_path, "Audio isolation completed successfully."
|
28 |
except ApiError as e:
|
@@ -45,4 +58,5 @@ iface = gr.Interface(
|
|
45 |
)
|
46 |
|
47 |
# Launch the app
|
48 |
-
iface.launch()
|
|
|
|
3 |
from elevenlabs.core.api_error import ApiError
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
+
from pydub import AudioSegment
|
7 |
+
import tempfile
|
8 |
|
9 |
# Load environment variables
|
10 |
load_dotenv()
|
|
|
12 |
# Initialize the client with your API key
|
13 |
client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
|
14 |
|
15 |
+
def is_valid_audio(file_path):
|
16 |
+
try:
|
17 |
+
AudioSegment.from_file(file_path)
|
18 |
+
return True
|
19 |
+
except:
|
20 |
+
return False
|
21 |
+
|
22 |
def isolate_audio(audio_file):
|
23 |
if audio_file is None:
|
24 |
return None, "Please upload an audio file."
|
25 |
|
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(audio_file, "rb") as file:
|
32 |
+
isolated_audio_iterator = client.audio_isolation.audio_isolation(audio=file)
|
33 |
|
34 |
# Save the isolated audio to a temporary file
|
35 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
|
|
36 |
for chunk in isolated_audio_iterator:
|
37 |
+
temp_file.write(chunk)
|
38 |
+
output_file_path = temp_file.name
|
39 |
|
40 |
return output_file_path, "Audio isolation completed successfully."
|
41 |
except ApiError as e:
|
|
|
58 |
)
|
59 |
|
60 |
# Launch the app
|
61 |
+
iface.launch()
|
62 |
+
|