jarvis / utils /functions.py
megamined's picture
Initial commit
b36a86c
from dotenv import find_dotenv, load_dotenv
import requests
import os
import shutil
import subprocess
from pathlib import Path
from typing import Iterator, Optional
load_dotenv(find_dotenv())
def get_voices():
url = "https://api.elevenlabs.io/v1/voices"
api_key = os.getenv("ELEVEN_API_KEY")
headers = {"Accept": "application/json", "xi-api-key": api_key}
response = requests.get(url, headers=headers)
print(response.text)
return response.text
def is_installed(lib_name: str) -> bool:
lib = shutil.which(lib_name)
if lib is None:
return False
global_path = Path(lib)
# else check if path is valid and has the correct access rights
return global_path.exists() and os.access(global_path, os.X_OK)
def play(audio: bytes, notebook: bool = False) -> None:
if notebook:
from IPython.display import Audio, display
display(Audio(audio, rate=44100, autoplay=True))
else:
if not is_installed("ffplay"):
raise ValueError("ffplay from ffmpeg not found, necessary to play audio.")
args = ["ffplay", "-autoexit", "-", "-nodisp"]
proc = subprocess.Popen(
args=args,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
)
_, err = proc.communicate(input=audio)
proc.poll()
def save(audio: bytes, filename: str) -> None:
with open(filename, "wb") as f:
f.write(audio)
def stream(audio_stream: Iterator[bytes]) -> bytes:
if not is_installed("mpv"):
raise ValueError("mpv not found, necessary to stream audio.")
mpv_command = ["mpv", "--no-cache", "--no-terminal", "--", "fd://0"]
mpv_process = subprocess.Popen(
mpv_command,
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
audio = b""
for chunk in audio_stream:
if chunk is not None:
mpv_process.stdin.write(chunk) # type: ignore
mpv_process.stdin.flush() # type: ignore
audio += chunk
if mpv_process.stdin:
mpv_process.stdin.close()
mpv_process.wait()
return audio