Spaces:
Sleeping
Sleeping
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() | |