Spaces:
Sleeping
Sleeping
import os | |
from dotenv import load_dotenv | |
import gradio as gr | |
import requests | |
import pathlib | |
load_dotenv() | |
KEY_ELEVENLABS = os.getenv('ELEVENLABS_KEY') | |
print(KEY_ELEVENLABS) | |
def generate_audio(text_input: str, creator: str) -> str: | |
CHUNK_SIZE = 1024 | |
headers = { | |
"Accept": "application/json", | |
"xi-api-key": KEY_ELEVENLABS | |
} | |
voice_id = '' | |
match creator: | |
case 'Roomie': | |
voice_id = '2Onew6n5JwT9uEbmTSrO' | |
case 'Xavy': | |
voice_id = 'cYBsY94mzMC7VpGoVMgr' | |
case 'Bella': | |
voice_id = 'X9j5sAaRD6aEgBblOUOG' | |
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream" | |
payload = { | |
"text": text_input, | |
"model_id": "eleven_monolingual_v1", | |
"voice_settings": { | |
"stability": 0.5, | |
"similarity_boost": 0.8, | |
"style": 0.0, | |
"use_speaker_boost": False | |
} | |
} | |
response = requests.post(url, headers=headers, json=payload, stream=True) | |
print(response) | |
with open('output.wav', 'wb') as f: | |
for chunk in response.iter_content(chunk_size=CHUNK_SIZE): | |
if chunk: | |
f.write(chunk) | |
return f'{pathlib.Path().resolve()}\output.mp3' | |
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() | |