Spaces:
Paused
Paused
rick
commited on
add audio isolation feature
Browse files- core/audio_isolation.py +31 -9
core/audio_isolation.py
CHANGED
|
@@ -8,6 +8,7 @@
|
|
| 8 |
|
| 9 |
from typing import Optional
|
| 10 |
from typing import Union
|
|
|
|
| 11 |
from typing import List
|
| 12 |
from typing import Dict
|
| 13 |
from typing import Any
|
|
@@ -15,19 +16,40 @@ import requests
|
|
| 15 |
import json
|
| 16 |
import os
|
| 17 |
import tempfile
|
|
|
|
| 18 |
|
| 19 |
from dotenv import load_dotenv
|
| 20 |
from elevenlabs import ElevenLabs
|
| 21 |
|
| 22 |
-
def isolate_audio(fichier_audio: str
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
with open(fichier_audio, 'rb') as audio_file:
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
if response.status_code == 200:
|
| 29 |
-
with open("audio_isole.mp3", "wb") as output_file:
|
| 30 |
-
output_file.write(response.content)
|
| 31 |
-
print("L'audio a été isolé avec succès et enregistré sous 'audio_isole.mp3'.")
|
| 32 |
-
else:
|
| 33 |
-
print(f"Erreur lors de l'isolement de l'audio: {response.status_code}, {response.text}")
|
|
|
|
| 8 |
|
| 9 |
from typing import Optional
|
| 10 |
from typing import Union
|
| 11 |
+
from typing import IO
|
| 12 |
from typing import List
|
| 13 |
from typing import Dict
|
| 14 |
from typing import Any
|
|
|
|
| 16 |
import json
|
| 17 |
import os
|
| 18 |
import tempfile
|
| 19 |
+
from io import BytesIO
|
| 20 |
|
| 21 |
from dotenv import load_dotenv
|
| 22 |
from elevenlabs import ElevenLabs
|
| 23 |
|
| 24 |
+
def isolate_audio(fichier_audio: str):
|
| 25 |
+
load_dotenv()
|
| 26 |
+
client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
|
| 27 |
+
buffer = BytesIO()
|
| 28 |
|
| 29 |
with open(fichier_audio, 'rb') as audio_file:
|
| 30 |
+
isolated_audio_iterator = client.audio_isolation.audio_isolation(audio=audio_file)
|
| 31 |
+
|
| 32 |
+
for chunk in isolated_audio_iterator:
|
| 33 |
+
buffer.write(chunk)
|
| 34 |
+
|
| 35 |
+
buffer.seek(0)
|
| 36 |
+
return buffer
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
"""
|
| 40 |
+
with tempfile.NamedTemporaryFile(delete_on_close=False) as fp_out:
|
| 41 |
+
for chunk in isolated_audio_iterator:
|
| 42 |
+
fp_out.write(chunk)
|
| 43 |
+
print(f"L'audio a été isolé avec succès et enregistré sous \'{fp_out.name}\'.")
|
| 44 |
+
fp_out.seek(0)
|
| 45 |
+
return fp_out.name
|
| 46 |
+
"""
|
| 47 |
+
# else:
|
| 48 |
+
# print(f"Erreur lors de l'isolement de l'audio: {isolated_audio_iterator.#status_code}, {isolated_audio_iterator.text}")
|
| 49 |
+
# return None
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
buffer = isolate_audio("audio.mp3")
|
| 53 |
+
with open("audio_isole.mp3", "wb") as output_file:
|
| 54 |
+
output_file.write(buffer.read())
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|