File size: 3,474 Bytes
1e08949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import streamlit as st
import subprocess
import tempfile
import sys
import os
from os.path import exists
import requests
import tarfile
from PIL import Image

# Set base path
BASE_PATH = os.getcwd() # /home/user/app
BASE_PATH_MODEL = os.path.join(BASE_PATH, "Model")

# Piper TTS download url
URL_PIPER_DOWNLOAD = "https://github.com/rhasspy/piper/releases/download/v1.2.0/piper_amd64.tar.gz"

ONNX = "https://huggingface.co/Rikels/piper-dutch/resolve/main/anna.onnx"

TMP_PIPER_FILENAME = os.path.join(BASE_PATH, "piper.tgz")

##########################
# CHECK OR INSTALL PIPER #
##########################
if os.path.exists(os.path.join(BASE_PATH,"piper")) == False:

    # Piper not downloaded and extracted yet, let's do this first.
    response = requests.get(URL_PIPER_DOWNLOAD)

    if response.status_code == 200:
        with open(TMP_PIPER_FILENAME, 'wb') as f:
            f.write(response.content)

        with tarfile.open(TMP_PIPER_FILENAME, 'r:gz') as tar:
            tar.extractall(BASE_PATH)

    else:
        st.markdown(f"Failed to download Piper TTS from {URL_PIPER_DOWNLOAD} (Status code: {response.status_code})")


#####################################################
# CHECK OR DOWNLOAD: All Thorsten-Voice model files #
#####################################################

# Create "Model" path if not existing
if os.path.exists(BASE_PATH_MODEL) == False:
    os.makedirs(BASE_PATH_MODEL)

    # --- Download "NEUTRAL" TTS model --- #
    response = requests.get(ONNX) 
    if response.status_code == 200:
        with open(os.path.join(BASE_PATH_MODEL, "anna.onnx"), 'wb') as f:
            f.write(response.content)

    response = requests.get(ONNX + ".json") 
    if response.status_code == 200:
        with open(os.path.join(BASE_PATH_MODEL, "anna.onnx.json"), 'wb') as f:
            f.write(response.content)


###########################
# MODEL DOWNLOAD FINISHED #
###########################

hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            header {visibility: hidden;}
            footer {visibility: hidden;}
            .st-emotion-cache-1y4p8pa {padding-top: 0rem;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

st.title('Guude! Thorsten-Voice clone hier 👋')
st.header('Clone van Thorsten-voice's App (TTS)')

#st.subheader('Hoi?')
st.markdown('Mehr Infos auf der [Projektwebseite](https://www.Thorsten-Voice.de) und auf meinem [Youtube Kanal](https://www.youtube.com/c/thorstenmueller)')

with st.form("my_form"):
  
   text = st.text_area("Wat wil je dit model laten zeggen?",max_chars=500)
   submitted = st.form_submit_button("Zeg dan!")
   
   if submitted:
    with st.spinner("ekkes wachten..."):
        filename = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)

        # Set Piper TTS command based on choice
        PIPER_CMD = os.path.join(BASE_PATH,"piper","piper")
        SPEAKER_ID = "0"
        MODEL = "anna.onnx"

        cmd = "echo '" + text + "' | " + BASE_PATH + "/piper/piper --model " + os.path.join(BASE_PATH_MODEL, MODEL) + " --speaker " + SPEAKER_ID + " --output_file " + filename.name

        result = subprocess.run(cmd, shell=True)
        audio_file = open(filename.name, 'rb')
        audio_bytes = audio_file.read()
        st.audio(audio_bytes,format="audio/wav")
try:
    st.download_button('Downloaden', audio_bytes, file_name='Thorsten-Voice-clone.wav')
except:
    pass