| |
| import streamlit as st |
| import os |
| import subprocess |
| import time |
| import wave |
| import piper_installer |
|
|
| |
| piper_installer.run_piper_installer() |
| |
| config_dict = {"piper_folder ": "/home/user/piper_installation/piper", |
| "out_folder": "output_files", "output_wav":"out_file", |
| "hf_models": "/home/user/app/models_config"} |
|
|
| piper_folder = config_dict['piper_folder '] |
| out_file = config_dict['output_wav'] |
| out_folder = config_dict['out_folder'] |
| hf_models = config_dict['hf_models'] |
| |
| os.chmod(hf_models, 0o755) |
|
|
|
|
| |
| def create_txt_file(text_content, file_name): |
| file_name = os.path.join(piper_folder, file_name) |
| with open(file_name, "w") as f: |
| f.write(text_content) |
| return file_name |
|
|
| |
| |
| |
|
|
| |
| def run_piper_tts(text): |
| command = "cat" + " " + text + " | " + " " "./piper" + " " + "--model "+ model_file + " " + "-f " + "./" + out_folder + "/" + out_file |
| print(command) |
| subprocess.run(command, shell=True, cwd=piper_folder) |
|
|
|
|
| |
|
|
| def get_model_files(models_folder): |
| try: |
| return [f for f in os.listdir(models_folder) if f.endswith(".onnx")] |
| except Exception as e: |
| st.sidebar.error("Failed to fetch models from the directory") |
| |
| |
| models = get_model_files(hf_models) |
| model_file = st.sidebar.selectbox("select a.onnx Modle", options = models, format_func = lambda x: x if x else "No model found") |
| model_file = os.path.join(hf_models, model_file) |
| os.chmod(model_file, 0o755) |
| |
| |
| txt_file = st.sidebar.file_uploader("Upload a .txt file for conversion", type=["txt"]) |
|
|
| |
| st.title("π£οΈ Piper Text to Speech App") |
| st.write("Enter the text below to synthesize speech.") |
|
|
| text_input = st.text_area("Enter text to convert to speech:", height=200) |
| if not text_input.strip(): |
| st.warning("Please enter text to convert to speech.") |
| else: |
| text_file = create_txt_file(text_input, "content.txt") |
|
|
| |
| if txt_file is not None: |
| if st.sidebar.button("π Convert Uploaded File to Audio"): |
| |
| file_name = "uploaded_content.txt" |
| uploaded_file_path = os.path.join(piper_folder, file_name) |
| with open(uploaded_file_path, "wb") as f: |
| f.write(txt_file.getbuffer()) |
|
|
| |
| run_piper_tts(uploaded_file_path) |
| st.success(f" π Speech synthesized from file: {uploaded_file_path}") |
| |
| col1, col2, col3, col4 = st.columns(4) |
| with col1: |
| text_convert = st.button("πText to Audio") |
| if model_file and text_input.strip(): |
| run_piper_tts(text_file) |
| st.success(f" π Speech synthesized to:{piper_folder}\\{out_folder}\\{out_file}") |
| with col4: |
| play_button = st.button("π Make Audio") |
|
|
|
|
| if play_button: |
| wav_path = os.path.join(piper_folder, out_folder, out_file) |
| if os.path.exists(wav_path): |
| st.info(f"Audio file ready: {wav_path}") |
|
|
| |
| with open(wav_path, "rb") as file: |
| st.download_button( |
| label="β¬ Download Audio File", |
| data=file, |
| file_name="output_audio.wav", |
| mime="audio/wav" |
| ) |
|
|
| |
| st.audio(wav_path, format='audio/wav') |
|
|
| with wave.open(wav_path, 'rb') as wav_file: |
| duration = wav_file.getnframes() / wav_file.getframerate() |
|
|
| |
| progress_bar = st.progress(0) |
|
|
| |
| steps = 100 |
| sleep_time = duration / steps |
| for step in range(steps): |
| time.sleep(sleep_time) |
| progress_bar.progress(step + 1) |
|
|
| progress_bar.empty() |
| st.success("π Playback simulation completed!") |
| else: |
| st.error("Audio file not found!") |
|
|