File size: 1,885 Bytes
1d6b3e5
 
9845260
07cadae
482aeab
07cadae
8df9053
 
6fe06f9
8df9053
9845260
6fe217e
6fe06f9
6b035eb
239228e
8df9053
9845260
99330d0
239228e
 
 
 
 
1d6b3e5
 
 
 
 
 
 
 
 
654a25e
1d6b3e5
 
0fe8fdc
 
 
1d6b3e5
6fe06f9
3651848
6fe06f9
a9cedc1
 
 
 
 
 
 
 
685fda7
a9cedc1
99330d0
1d6b3e5
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
from datasets import load_dataset


DATASET_REPO_URL = "https://huggingface.co/datasets/laxsvips/audiofiles"
DATA_FILENAME = "audio.mp3"
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):
    # Pad buffer to multiple of 2 bytes
    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.mp3')   
        # save(audio,'audio.wav') 
        # commit_url = repo.push_to_hub()
        # dataset = load_dataset("audiofolder", data_dir="./data")
        audio_dataset = Dataset.from_dict({"audio": ["data/audio.mp3"]}).cast_column("audio", Audio())
        commit_url = audio_dataset.push_to_hub("laxsvips/audiofiles")

        return commit_url
        # return_url = "failure"
        # if commit_url:
        # return_url = DATASET_REPO_URL+"/"+ DATA_FILENAME
       
        # return (return_url)
        # return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
    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)