|
import random |
|
import numpy as np |
|
from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError, save |
|
import huggingface_hub |
|
from huggingface_hub import Repository |
|
import os |
|
from huggingface_hub import HfApi |
|
import gradio as gr |
|
|
|
|
|
DATASET_REPO_URL = "https://huggingface.co/datasets/laxsvips/audiofiles" |
|
DATA_FILENAME = "audio.wav" |
|
DATA_FILE = os.path.join("data", DATA_FILENAME) |
|
|
|
api = HfApi() |
|
|
|
HF_TOKEN = os.environ.get("HF_TOKEN") |
|
repo = Repository( |
|
local_dir="data", |
|
clone_from=DATASET_REPO_URL, |
|
use_auth_token=HF_TOKEN |
|
) |
|
|
|
def pad_buffer(audio): |
|
|
|
buffer_size = len(audio) |
|
element_size = np.dtype(np.int16).itemsize |
|
if buffer_size % element_size != 0: |
|
audio = audio + b'\0' * (element_size - (buffer_size % element_size)) |
|
return audio |
|
|
|
def generate_voice(text): |
|
try: |
|
audio = generate( |
|
text, |
|
voice="Arnold", |
|
model="eleven_monolingual_v1" |
|
) |
|
save(audio,'data/audio.wav') |
|
|
|
commit_url = repo.push_to_hub() |
|
return_url = "failure" |
|
if commit_url: |
|
return_url = DATASET_REPO_URL+"/"+ DATA_FILENAME |
|
|
|
return (return_url) |
|
|
|
except UnauthenticatedRateLimitError as e: |
|
raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.") |
|
except Exception as e: |
|
raise gr.Error(e) |