Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import subprocess
|
3 |
+
import tempfile
|
4 |
+
import sys
|
5 |
+
import os
|
6 |
+
from os.path import exists
|
7 |
+
import requests
|
8 |
+
import tarfile
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
+
# Set base path
|
12 |
+
BASE_PATH = os.getcwd() # /home/user/app
|
13 |
+
BASE_PATH_MODEL = os.path.join(BASE_PATH, "Model")
|
14 |
+
|
15 |
+
# Piper TTS download url
|
16 |
+
URL_PIPER_DOWNLOAD = "https://github.com/rhasspy/piper/releases/download/v1.2.0/piper_amd64.tar.gz"
|
17 |
+
|
18 |
+
ONNX = "https://huggingface.co/Rikels/piper-dutch/resolve/main/anna.onnx"
|
19 |
+
|
20 |
+
TMP_PIPER_FILENAME = os.path.join(BASE_PATH, "piper.tgz")
|
21 |
+
|
22 |
+
##########################
|
23 |
+
# CHECK OR INSTALL PIPER #
|
24 |
+
##########################
|
25 |
+
if os.path.exists(os.path.join(BASE_PATH,"piper")) == False:
|
26 |
+
|
27 |
+
# Piper not downloaded and extracted yet, let's do this first.
|
28 |
+
response = requests.get(URL_PIPER_DOWNLOAD)
|
29 |
+
|
30 |
+
if response.status_code == 200:
|
31 |
+
with open(TMP_PIPER_FILENAME, 'wb') as f:
|
32 |
+
f.write(response.content)
|
33 |
+
|
34 |
+
with tarfile.open(TMP_PIPER_FILENAME, 'r:gz') as tar:
|
35 |
+
tar.extractall(BASE_PATH)
|
36 |
+
|
37 |
+
else:
|
38 |
+
st.markdown(f"Failed to download Piper TTS from {URL_PIPER_DOWNLOAD} (Status code: {response.status_code})")
|
39 |
+
|
40 |
+
|
41 |
+
#####################################################
|
42 |
+
# CHECK OR DOWNLOAD: All Thorsten-Voice model files #
|
43 |
+
#####################################################
|
44 |
+
|
45 |
+
# Create "Model" path if not existing
|
46 |
+
if os.path.exists(BASE_PATH_MODEL) == False:
|
47 |
+
os.makedirs(BASE_PATH_MODEL)
|
48 |
+
|
49 |
+
# --- Download "NEUTRAL" TTS model --- #
|
50 |
+
response = requests.get(ONNX)
|
51 |
+
if response.status_code == 200:
|
52 |
+
with open(os.path.join(BASE_PATH_MODEL, "anna.onnx"), 'wb') as f:
|
53 |
+
f.write(response.content)
|
54 |
+
|
55 |
+
response = requests.get(ONNX + ".json")
|
56 |
+
if response.status_code == 200:
|
57 |
+
with open(os.path.join(BASE_PATH_MODEL, "anna.onnx.json"), 'wb') as f:
|
58 |
+
f.write(response.content)
|
59 |
+
|
60 |
+
|
61 |
+
###########################
|
62 |
+
# MODEL DOWNLOAD FINISHED #
|
63 |
+
###########################
|
64 |
+
|
65 |
+
hide_streamlit_style = """
|
66 |
+
<style>
|
67 |
+
#MainMenu {visibility: hidden;}
|
68 |
+
header {visibility: hidden;}
|
69 |
+
footer {visibility: hidden;}
|
70 |
+
.st-emotion-cache-1y4p8pa {padding-top: 0rem;}
|
71 |
+
</style>
|
72 |
+
"""
|
73 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
74 |
+
|
75 |
+
st.title('Guude! Thorsten-Voice clone hier 👋')
|
76 |
+
st.header('Clone van Thorsten-voice's App (TTS)')
|
77 |
+
|
78 |
+
#st.subheader('Hoi?')
|
79 |
+
st.markdown('Mehr Infos auf der [Projektwebseite](https://www.Thorsten-Voice.de) und auf meinem [Youtube Kanal](https://www.youtube.com/c/thorstenmueller)')
|
80 |
+
|
81 |
+
with st.form("my_form"):
|
82 |
+
|
83 |
+
text = st.text_area("Wat wil je dit model laten zeggen?",max_chars=500)
|
84 |
+
submitted = st.form_submit_button("Zeg dan!")
|
85 |
+
|
86 |
+
if submitted:
|
87 |
+
with st.spinner("ekkes wachten..."):
|
88 |
+
filename = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
89 |
+
|
90 |
+
# Set Piper TTS command based on choice
|
91 |
+
PIPER_CMD = os.path.join(BASE_PATH,"piper","piper")
|
92 |
+
SPEAKER_ID = "0"
|
93 |
+
MODEL = "anna.onnx"
|
94 |
+
|
95 |
+
cmd = "echo '" + text + "' | " + BASE_PATH + "/piper/piper --model " + os.path.join(BASE_PATH_MODEL, MODEL) + " --speaker " + SPEAKER_ID + " --output_file " + filename.name
|
96 |
+
|
97 |
+
result = subprocess.run(cmd, shell=True)
|
98 |
+
audio_file = open(filename.name, 'rb')
|
99 |
+
audio_bytes = audio_file.read()
|
100 |
+
st.audio(audio_bytes,format="audio/wav")
|
101 |
+
try:
|
102 |
+
st.download_button('Downloaden', audio_bytes, file_name='Thorsten-Voice-clone.wav')
|
103 |
+
except:
|
104 |
+
pass
|