Skip to content

Instantly share code, notes, and snippets.

@blacksmithop
Created March 6, 2023 09:30
Show Gist options
  • Save blacksmithop/e290db0e2308a0d76cefdda29295b662 to your computer and use it in GitHub Desktop.
Save blacksmithop/e290db0e2308a0d76cefdda29295b662 to your computer and use it in GitHub Desktop.
Text to Speech demo (with pyttsx3 & streanlit)
pyttsx3
streamlit
import streamlit as st
from tts import TextToSpeech
txt2speech = TextToSpeech()
st.title('Text To Speech Example')
conversion_text = st.text_input('Text to convert', 'The quick brown fox jumps over the lazy dog')
if st.button('Convert'):
txt2speech.convert(text=conversion_text)
with open('hello.mp3', 'rb') as audio_file:
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/mp3')
import pyttsx3
class TextToSpeech:
def __init__(self):
self.engine = pyttsx3.init()
self.engine.setProperty('rate', 125)
voices = self.engine.getProperty('voices')
self.engine.setProperty('voice', voices[1].id) # female voice (1)
def convert(self, text:str, filename: str="hello.mp3"):
self.engine.save_to_file(text, filename)
self.engine.runAndWait()
print(f"Saved audio to {filename}")
if __name__ == "__main__":
tts = TextToSpeech()
tts.convert(text='The quick brown fox jumps over the lazy dog.', filename='hello.mp3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment