import streamlit as st import subprocess import tempfile import sys import os from os.path import exists import requests from huggingface_hub import HfFolder import tarfile from PIL import Image HF_TOKEN = os.environ.get("HF_TOKEN") # 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" # TTS model files URL_TTS_ONNX = "https://huggingface.co/aslanovaf/tts_vits_azerbaijani/resolve/main/last.onnx" TMP_PIPER_FILENAME = os.path.join(BASE_PATH, "piper.tgz") headers = {"Authorization": f"Bearer {HF_TOKEN}"} ########################## # 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: TTS model files # ##################################################### # Create "Model" path if not existing if os.path.exists(BASE_PATH_MODEL) == False: os.makedirs(BASE_PATH_MODEL) # --- Download TTS model --- # response = requests.get(URL_TTS_ONNX, headers=headers) if response.status_code == 200: with open(os.path.join(BASE_PATH_MODEL, "last.onnx"), 'wb') as f: f.write(response.content) else: st.markdown(f"Failed to download TTS from {URL_TTS_ONNX} (Status code: {response.status_code})") response = requests.get((URL_TTS_ONNX + ".json"), headers=headers) if response.status_code == 200: with open(os.path.join(BASE_PATH_MODEL, "last.onnx.json"), 'wb') as f: f.write(response.content) else: st.markdown(f"Failed to download TTS json from {URL_TTS_ONNX}.json (Status code: {response.status_code})") ########################### # MODEL DOWNLOAD FINISHED # ########################### hide_streamlit_style = """ """ st.markdown(hide_streamlit_style, unsafe_allow_html=True) # st.title('Salam!') # st.header('Azərbaycan dilində mətni səsə çevirən model (TTS)') # st.markdown('.') st.title('Hello!') st.header('A text-to-speech (TTS) model in Azerbaijani language') with st.form("my_form"): option = st.selectbox( 'Choose the model.', ['Model 1']) text = st.text_area("Text to generate audio from:",max_chars=500) submitted = st.form_submit_button("Submit") if submitted: with st.spinner("Please, wait... :)"): 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 = "last.onnx" match option: case "Model 1": MODEL = "last.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('Download audio', audio_bytes, file_name='TTS-Voice.wav') except: pass