-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice_handling.py
36 lines (32 loc) · 1.18 KB
/
voice_handling.py
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
from elevenlabs import generate, play, set_api_key, stream, save
from utils import format_output
import json
import os
import streamlit as st
@st.cache_data(show_spinner=True)
def play_dialogues(story_metadata, elevenlabs_api_key):
set_api_key(elevenlabs_api_key)
character_voices = format_output(story_metadata, elevenlabs_api_key)
if len(story_metadata['Dialogues']) <= 1:
try:
dialogues = story_metadata['Dialogues'][0]
except:
dialogues = story_metadata['Dialogues']
else:
dialogues = story_metadata['Dialogues']
audio_files = []
# print(dialogues)
for idx, role in enumerate(dialogues):
try:
character = role['Character'].strip()
dialogue = role['Dialogue'].strip()
except:
character = dialogues['Character']
dialogue = dialogues['Dialogue']
voice_path = f'./voices/voice_{idx}_{character}.wav'
voice = character_voices[character]
audio = generate(text = dialogue, voice = voice)
save(audio, voice_path)
# audio_files.append(voice_path)
audio_files.append(voice_path)
return audio_files, character_voices