File size: 839 Bytes
092b169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
from datasets import load_dataset
import torch
import streamlit as st

@st.cache_resource
def load_speech_model():
    synthesiser = pipeline("text-to-speech", "microsoft/speecht5_tts")

    embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
    speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)

    return synthesiser, speaker_embedding

def speech_elements():
    synthesiser, speaker_embedding = load_speech_model()

    text = st.text_area('Enter English text here')
    st.write(f'You wrote {len(text)} characters.')

    if st.button('Speech'):
        speech = synthesiser(text, forward_params={"speaker_embeddings": speaker_embedding})

        st.audio(speech['audio'], sample_rate=speech['sampling_rate'])


speech_elements()