File size: 1,982 Bytes
914c7b4
 
ff92946
914c7b4
 
ff92946
 
 
 
 
914c7b4
 
 
 
 
 
ff92946
914c7b4
 
 
 
 
 
 
 
 
 
 
ff92946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33c11bc
914c7b4
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import gradio as gr
import uuid
import requests
import pathlib
from elevenlabs.client import ElevenLabs, AsyncElevenLabs
from elevenlabs import play, save, Voice, stream
from dotenv import load_dotenv
from datetime import timedelta
from gcloud import storage


load_dotenv()
KEY_ELEVENLABS = os.getenv('ELEVENLABS_KEY')


async def generate_audio(text_input: str, creator: str) -> str:

    voice_id = ''

    match creator:
        case 'Roomie':
            voice_id = '2Onew6n5JwT9uEbmTSrO'
        case 'Xavy':
            voice_id = 'cYBsY94mzMC7VpGoVMgr'
        case 'Bella':
            voice_id = 'X9j5sAaRD6aEgBblOUOG'

    CLIENT_ELEVENLABS = ElevenLabs(api_key=KEY_ELEVENLABS)
    VOICE_CREATOR = Voice(voice_id=voice_id)
    CREDENTIALS_GCP = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
    NAME_BUCKET = os.getenv("NAME_BUCKET")
    unique_id = str(uuid.uuid4())

    STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)

    audio = CLIENT_ELEVENLABS.generate(
        text=text_input,
        voice=VOICE_CREATOR,
        model="eleven_multilingual_v2"
    )

    source_audio_file_name = f'./audios/file_audio_{unique_id}.wav'

    try:
        save(audio, source_audio_file_name)
    except Exception as e:
        print(e)

    destination_blob_name_audio = unique_id + '.wav'

    bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
    blob = bucket.blob(destination_blob_name_audio)
    try:
        blob.upload_from_filename(source_audio_file_name)
    except Exception as e:
        print(e)

    try:
        url_expiration = timedelta(minutes=15)
        signed_url_audio = blob.generate_signed_url(expiration=url_expiration)
    except Exception as e:
        print(e)

    return gr.Audio(value=source_audio_file_name)


app = gr.Interface(
    fn=generate_audio,
    inputs=[gr.Textbox(label='Text to Speach'), gr.Dropdown(
        ['Roomie', 'Xavy', 'Bella'], label="Coice your creator")],
    outputs=['audio']
)

app.launch()